module SafetyCone::Filter

Module for Filtering requests and raise notices and take measures

Public Class Methods

included(base) click to toggle source

Method to include to base class. This lets declare a before filter here

# File lib/safety_cone/filter.rb, line 7
def self.included(base)
  if Rails.version.to_i >= 5
    base.before_action :safety_cone_filter
  else
    base.before_filter :safety_cone_filter
  end
end

Public Instance Methods

safety_cone_filter() click to toggle source

Filter method that does the SafetyCone action based on the configuration.

# File lib/safety_cone/filter.rb, line 17
def safety_cone_filter
  if cone = fetch_cone
    if cone.type == 'notice'
      flash.now["safetycone_#{notice_type(cone.type)}"] = cone.message
    else
      flash["safetycone_#{notice_type(cone.type)}"] = cone.message
    end

    redirect_to safety_redirect if cone.type == 'block'
  end
end

Private Instance Methods

fetch_cone() click to toggle source

Fetches a configuration based on current request

# File lib/safety_cone/filter.rb, line 32
def fetch_cone
  paths = SafetyCone.paths

  if path = paths[request_action]
    key = request_action
  elsif cone = paths[request_method]
    key = request_method
  else
    return false
  end

  path = Path.new(key, path)
  path.fetch

  %w[notice block].include?(path.type) ? path : false
end
notice_type(type) click to toggle source

Returns type of notice

# File lib/safety_cone/filter.rb, line 55
def notice_type(type)
  type == 'notice' ? 'notice' : 'alert'
end
request_action() click to toggle source

Returns the current controller_action as a symbol

# File lib/safety_cone/filter.rb, line 65
def request_action
  "#{controller_name}_#{action_name}".to_sym
end
request_method() click to toggle source

Returns the current request action as a symbol

# File lib/safety_cone/filter.rb, line 60
def request_method
  request.method.to_sym
end
safety_redirect() click to toggle source

Method to redirect a request

# File lib/safety_cone/filter.rb, line 50
def safety_redirect
  request.env['HTTP_REFERER'] || root_path
end