move account settings to a separate module
so that a plugin can define settings on account before account is fully loaded Change-Id: I41b538d152136e3b48452e8f48046a9caa882c13 Reviewed-on: https://gerrit.instructure.com/51773 Tested-by: Jenkins Reviewed-by: Ethan Vizitei <evizitei@instructure.com> Product-Review: Cody Cutrer <cody@instructure.com> QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
parent
8ed1edcbb1
commit
18b91f6b01
|
@ -148,50 +148,7 @@ class Account < ActiveRecord::Base
|
|||
result
|
||||
end
|
||||
|
||||
cattr_accessor :account_settings_options
|
||||
self.account_settings_options = {}
|
||||
|
||||
def self.add_setting(setting, opts=nil)
|
||||
if opts && opts[:inheritable]
|
||||
opts[:hash] = true
|
||||
opts[:values] = [:value, :locked]
|
||||
|
||||
self.class_eval "def #{setting}; calculate_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
|
||||
|
||||
# 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
|
||||
include ::Account::Settings
|
||||
|
||||
# these settings either are or could be easily added to
|
||||
# the account settings page
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
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}; calculate_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
|
||||
end
|
||||
|
||||
def self.included(klass)
|
||||
klass.extend(ClassMethods)
|
||||
klass.send(:cattr_accessor, :account_settings_options)
|
||||
klass.account_settings_options ||= {}
|
||||
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
|
Loading…
Reference in New Issue