Module: Tunable::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/tunable/model.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #clear_instance_settings ⇒ Object
- #get_main_setting(key) ⇒ Object
- #get_setting(context, key) ⇒ Object
- #main_settings ⇒ Object
- #queue_setting_for_deletion(context, key) ⇒ Object
- #queue_setting_for_update(context, key, val) ⇒ Object
- #remove_setting(context, key) ⇒ Object
- #set_setting(context, key, val) ⇒ Object
- #setting_off?(context, key) ⇒ Boolean
- #setting_on?(context, key) ⇒ Boolean
-
#settings=(hash) ⇒ Object
instance methods below.
- #settings_context(context) ⇒ Object
- #settings_hash ⇒ Object
Instance Method Details
#clear_instance_settings ⇒ Object
177 178 179 180 |
# File 'lib/tunable/model.rb', line 177 def clear_instance_settings @object_hashed_settings = nil @settings = nil # so settings get reloaded from DB end |
#get_main_setting(key) ⇒ Object
165 166 167 |
# File 'lib/tunable/model.rb', line 165 def get_main_setting(key) get_setting(:main, key) end |
#get_setting(context, key) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/tunable/model.rb', line 147 def get_setting(context, key) val = settings_context(context)[key] # if value is nil or no default is set, stop here return val if !val.nil? or self.class.default_settings(context)[key.to_sym].nil? self.class.default_settings(context)[key.to_sym] end |
#main_settings ⇒ Object
173 174 175 |
# File 'lib/tunable/model.rb', line 173 def main_settings settings_context(:main) end |
#queue_setting_for_deletion(context, key) ⇒ Object
197 198 199 200 201 202 |
# File 'lib/tunable/model.rb', line 197 def queue_setting_for_deletion(context, key) if self.class.main_settings_list.include?(key) @changed_attributes[key.to_sym] = nil if changed_attributes.include?(key.to_sym) end (deleted_settings[context.to_sym] ||= []) << key.to_sym end |
#queue_setting_for_update(context, key, val) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/tunable/model.rb', line 190 def queue_setting_for_update(context, key, val) if self.class.main_settings_list.include?(key.to_sym) @changed_attributes[key.to_sym] = val if changed_attributes.include?(key.to_sym) end (modified_settings[context.to_sym] ||= {})[key.to_sym] = val end |
#remove_setting(context, key) ⇒ Object
161 162 163 |
# File 'lib/tunable/model.rb', line 161 def remove_setting(context, key) set_setting(context, key, nil) end |
#set_setting(context, key, val) ⇒ Object
156 157 158 159 |
# File 'lib/tunable/model.rb', line 156 def set_setting(context, key, val) obj = { context => { key => val } } self.settings = obj end |
#setting_off?(context, key) ⇒ Boolean
182 183 184 |
# File 'lib/tunable/model.rb', line 182 def setting_off?(context, key) get_setting(context, key) == false end |
#setting_on?(context, key) ⇒ Boolean
186 187 188 |
# File 'lib/tunable/model.rb', line 186 def setting_on?(context, key) get_setting(context, key) == true end |
#settings=(hash) ⇒ Object
instance methods below
135 136 137 |
# File 'lib/tunable/model.rb', line 135 def settings=(hash) Tunable::Setting.store_many(hash, self) end |
#settings_context(context) ⇒ Object
169 170 171 |
# File 'lib/tunable/model.rb', line 169 def settings_context(context) settings_hash[context.to_sym] || {} end |
#settings_hash ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/tunable/model.rb', line 139 def settings_hash if modified_settings.any? puts "Settings have been changed. Hash will be incomplete." end @object_hashed_settings ||= Hasher.flatten(settings.reload, :context, :key) end |