Module: Flail::Rails::ControllerMethods

Defined in:
lib/flail/rails/controller_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/flail/rails/controller_methods.rb', line 5

def self.included(base)
  base.class_eval do
    if respond_to?(:before_action)
      before_action :inject_flail_data_into_environment
    else
      before_filter :inject_flail_data_into_environment
    end
  end
end

Instance Method Details

#flail_paramsObject



26
27
28
29
30
31
32
# File 'lib/flail/rails/controller_methods.rb', line 26

def flail_params
  result = nil
  result ||= request.env['action_dispatch.parameters']
  result ||= flail_rack_params
  result ||= {}
  result
end

#flail_rack_paramsObject



34
35
36
37
# File 'lib/flail/rails/controller_methods.rb', line 34

def flail_rack_params
  req = Rack::Request.new(request.env)
  req.params if request.env['rack.input']
end

#flail_request_dataObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/flail/rails/controller_methods.rb', line 39

def flail_request_data
  {
    :parameters => flail_params.to_hash,
    :session_data => flail_session_data,
    :target_url => request.url,
    :referer_url => request.referer,
    :user_agent => request.user_agent,
    :user => flail_user_data
  }
end

#flail_session_dataObject



50
51
52
53
54
55
56
# File 'lib/flail/rails/controller_methods.rb', line 50

def flail_session_data
  if session.respond_to?(:to_hash)
    session.to_hash
  else
    session.data
  end
end

#flail_user_dataObject



58
59
60
61
62
63
64
65
66
# File 'lib/flail/rails/controller_methods.rb', line 58

def flail_user_data
  user = current_user

  user.attributes.select do |k, v|
    /^(id|name|username|email|login)$/ === k unless v.blank?
  end
rescue NoMethodError, NameError
  {}
end

#inject_flail_data_into_environmentObject

This method is inserted into the host application’s controllers as a before_filter and is used to pass the parameters, session data, user data, and URL data from the app’s controller to the flail gem via the env variable (data is always passed, even if no exception is thrown). This hook only works when a host app’s controller is called. Routing error exceptions are thrown before the controller is called so those errors are handled elsewhere and contain less information (mainly the user data is missing).



21
22
23
24
# File 'lib/flail/rails/controller_methods.rb', line 21

def inject_flail_data_into_environment
  request.env['flail.request'] ||= request
  request.env['flail.request.data'] ||= flail_request_data
end