forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.rb
More file actions
63 lines (56 loc) · 2.07 KB
/
Copy pathsettings.rb
File metadata and controls
63 lines (56 loc) · 2.07 KB
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
module Account::Settings
module ClassMethods
def add_setting(setting, opts=nil)
if opts && opts[:inheritable]
opts[:hash] = true
opts[:values] = [:value, :locked]
self.class_eval "def #{setting}; cached_inherited_setting(:#{setting}); end"
elsif (opts && opts[:boolean] && opts.has_key?(:default))
if opts[:default]
# if the default is true, we want a nil result to evaluate to true.
# this prevents us from having to backfill true values into a
# serialized column, which would be expensive.
self.class_eval "def #{setting}?; settings[:#{setting}] != false; end"
else
# if the default is not true, we can fall back to a straight boolean.
self.class_eval "def #{setting}?; !!settings[:#{setting}]; end"
end
end
self.account_settings_options[setting.to_sym] = opts || {}
end
def inheritable_settings
self.account_settings_options.select{|k, v| v[:inheritable]}.keys
end
end
def self.included(klass)
klass.extend(ClassMethods)
klass.send(:cattr_accessor, :account_settings_options)
klass.account_settings_options ||= {}
end
def cached_inherited_setting(setting)
self.shard.activate do
Rails.cache.fetch([setting, self.global_id].cache_key) do
calculate_inherited_setting(setting)
end
end
end
# should continue down the account chain until it reaches a locked value
# otherwise use the last explicitly set value
def calculate_inherited_setting(setting)
inherited_hash = {:locked => false, :value => self.class.account_settings_options[setting][:default]}
self.account_chain.reverse_each do |acc|
current_hash = acc.settings[setting]
next if current_hash.nil?
if !current_hash.is_a?(Hash)
current_hash = {:locked => false, :value => current_hash}
end
current_hash[:inherited] = true if (self != acc)
if current_hash[:locked]
return current_hash
else
inherited_hash = current_hash
end
end
return inherited_hash
end
end