Class: Setler::Settings
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Setler::Settings
- Defined in:
- lib/setler/settings.rb
Direct Known Subclasses
Constant Summary collapse
- @@defaults =
{}.with_indifferent_access
Class Method Summary collapse
- .[](var) ⇒ Object
- .[]=(var, value) ⇒ Object
- .all ⇒ Object
- .destroy(var_name) ⇒ Object
-
.method_missing(method, *args, &block) ⇒ Object
Get and Set variables when the calling method is the variable name.
- .thing_scoped ⇒ Object
Class Method Details
.[](var) ⇒ Object
23 24 25 26 |
# File 'lib/setler/settings.rb', line 23 def self.[](var) the_setting = thing_scoped.find_by_var(var.to_s) the_setting.present? ? the_setting.value : @@defaults[var] end |
.[]=(var, value) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/setler/settings.rb', line 28 def self.[]=(var, value) # THIS IS BAD # thing_scoped.find_or_create_by_var(method_name[0..-2]) should work but doesnt for some reason # When @object is present, thing_scoped sets the where scope for the polymorphic association # but the find_or_create_by wasn't using the thing_type and thing_id thing_scoped.find_or_create_by_var_and_thing_type_and_thing_id( var.to_s, @object.try(:class).try(:base_class).try(:to_s), @object.try(:id) ).update_attributes({ :value => value }) end |
.all ⇒ Object
50 51 52 |
# File 'lib/setler/settings.rb', line 50 def self.all @@defaults.merge(Hash[thing_scoped.all.collect{ |s| [s.var, s.value] }]) end |
.destroy(var_name) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/setler/settings.rb', line 40 def self.destroy(var_name) var_name = var_name.to_s if setting = self.find_by_var(var_name) setting.destroy true else raise SettingNotFound, "Setting variable \"#{var_name}\" not found" end end |
.method_missing(method, *args, &block) ⇒ Object
Get and Set variables when the calling method is the variable name
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/setler/settings.rb', line 10 def self.method_missing(method, *args, &block) if respond_to?(method) super(method, *args, &block) else method_name = method.to_s if method_name.ends_with?("=") self[method_name[0..-2]] = args.first else self[method_name] end end end |
.thing_scoped ⇒ Object
54 55 56 |
# File 'lib/setler/settings.rb', line 54 def self.thing_scoped self.where(thing_type: nil, thing_id: nil) end |