This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
analytical.rb
83 lines (74 loc) · 2.66 KB
/
analytical.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require File.dirname(__FILE__)+'/analytical/rails/engine'
require File.dirname(__FILE__)+'/analytical/modules/base'
Dir.glob(File.dirname(__FILE__)+'/analytical/**/*.rb').each do |f|
require f
end
module Analytical
def analytical(options={})
self.analytical_options = options
config_options = { :modules => [] }
File.open("#{::Rails.root}/config/analytical.yml") do |f|
file_options = YAML::load(ERB.new(f.read).result).symbolize_keys
env = (::Rails.env || :production).to_sym
file_options = file_options[env] if file_options.has_key?(env)
file_options.each do |k, v|
if v.respond_to?(:symbolize_keys)
# module configuration
config_options[k.to_sym] = v.symbolize_keys
config_options[:modules] << k.to_sym unless options && options[:modules]
else
# regular option
config_options[k.to_sym] = v
end
end if file_options
end if File.exists?("#{::Rails.root}/config/analytical.yml")
self.analytical_options = self.analytical_options.reverse_merge config_options
end
module InstanceMethods
def analytical
@analytical ||= begin
options = self.class.analytical_options.merge({
:ssl => request.ssl?,
:controller => self,
})
if options[:modules] && (options[:modules].is_a?(Hash) || options[:modules].is_a?(Proc))
items = options[:modules].is_a?(Hash) ? options[:modules] : options[:modules].call(self)
modules = []
items.each do |k,v|
options[k.to_sym] = v.symbolize_keys
modules << k.to_sym
end
options[:modules] = modules
end
if options[:disable_if] && options[:disable_if].call(self)
options[:modules] = []
end
options[:session] = session if options[:use_session_store]
if analytical_is_robot?(request.user_agent, options[:user_agent_whitelist] || [])
options[:modules] = []
end
options[:modules] = options[:filter_modules].call(self, options[:modules]) if options[:filter_modules]
options[:javascript_helpers] ||= true if options[:javascript_helpers].nil?
Analytical::Api.new options
end
end
end
module HelperMethods
def analytical
controller.analytical
end
end
end
if defined?(ActionController::Base)
ActionController::Base.class_eval do
extend Analytical
include Analytical::InstanceMethods
include Analytical::BotDetector
helper Analytical::HelperMethods
if ::Rails::VERSION::MAJOR < 3
class_inheritable_accessor :analytical_options
else
class_attribute :analytical_options
end
end
end