Module: Tunable::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/tunable/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#clear_instance_settingsObject



197
198
199
200
# File 'lib/tunable/model.rb', line 197

def clear_instance_settings
  @object_hashed_settings = nil
  @settings = nil # so settings get reloaded from DB
end

#get_main_setting(key) ⇒ Object



185
186
187
# File 'lib/tunable/model.rb', line 185

def get_main_setting(key)
  get_setting(:main, key)
end

#get_setting(context, key) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/tunable/model.rb', line 165

def get_setting(context, key)
  val = settings_context(context)[key]

  # if value is nil or no default is set, stop here
  if !val.nil? or self.class.default_settings(context)[key.to_sym].nil?
    return val
  end

  self.class.default_settings(context)[key.to_sym]
end

#main_settingsObject



193
194
195
# File 'lib/tunable/model.rb', line 193

def main_settings
  settings_context(:main)
end

#queue_setting_for_deletion(context, key) ⇒ Object



217
218
219
220
221
222
# File 'lib/tunable/model.rb', line 217

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



210
211
212
213
214
215
# File 'lib/tunable/model.rb', line 210

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



181
182
183
# File 'lib/tunable/model.rb', line 181

def remove_setting(context, key)
  set_setting(context, key, nil)
end

#set_setting(context, key, val) ⇒ Object



176
177
178
179
# File 'lib/tunable/model.rb', line 176

def set_setting(context, key, val)
  obj = { context => { key => val } }
  self.settings = obj
end

#setting_off?(context, key) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/tunable/model.rb', line 202

def setting_off?(context, key)
  get_setting(context, key) == false
end

#setting_on?(context, key) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/tunable/model.rb', line 206

def setting_on?(context, key)
  get_setting(context, key) == true
end

#settings=(hash) ⇒ Object

instance methods below



146
147
148
# File 'lib/tunable/model.rb', line 146

def settings=(hash)
  Tunable::Setting.store_many(hash, self)
end

#settings_context(context) ⇒ Object



189
190
191
# File 'lib/tunable/model.rb', line 189

def settings_context(context)
  settings_hash[context.to_sym] || {}
end

#settings_hashObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/tunable/model.rb', line 150

def settings_hash
  @object_hashed_settings ||= Hasher.flatten(settings.reload, :context, :key)

  if modified_settings.any?
    modified_settings.each do |context, values|
      values.each do |key, value|
        @object_hashed_settings[context.to_sym] ||= {}
        @object_hashed_settings[context.to_sym][key.to_sym] = value
      end
    end
  end

  @object_hashed_settings
end