Module: SettingAccessors::SettingScaffold::ClassMethods

Defined in:
lib/setting_accessors/setting_scaffold.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Makes accessing settings a little easier. Examples:

#Loading **the value** of a global setting named "my_setting"
Setting.my_setting

#Setting **the value** of a global setting named "my_setting"
Setting.my_setting = [1,2,3,4,5]

#Loading **the value** of an assigned setting named "cool_setting"
#+some_cool_user+ is here an instance of ActiveRecord::Base
Setting.cool_setting(some_cool_user)


137
138
139
140
141
142
143
144
145
146
147
# File 'lib/setting_accessors/setting_scaffold.rb', line 137

def method_missing(method, *args, &block)
  method_name = method.to_s

  if method_name.last == '='
    return set(method_name[0..-2], args.first)
  elsif args.size <= 1
    return get(method_name, args.first)
  end

  super
end

Instance Method Details

#[]=(name, *args) ⇒ Object

An alias for #set with a slightly different API. This allows the following usage:

Setting['my_setting', my_assignable] ||= new_value


92
93
94
95
# File 'lib/setting_accessors/setting_scaffold.rb', line 92

def []=(name, *args)
  assignable = args.size > 1 ? args.first : nil
  set(name, args.last, assignable: assignable)
end

#get(name, assignable = nil) ⇒ Object, NilClass Also known as: []

Searches for a setting in the database and returns its value

Parameters:

  • name (String, Symbol)

    The setting’s name

  • assignable (ActiveRecord::Base) (defaults to: nil)

    If given, the setting searched has to be assigned to the given record If not given, a global setting is searched

Returns:

  • (Object, NilClass)

    If a setting is found, **its value** is returned. If not, nil is returned.



36
37
38
# File 'lib/setting_accessors/setting_scaffold.rb', line 36

def get(name, assignable = nil)
  setting_record(name, assignable).try(:value)
end

#get!(name, assignable = nil) ⇒ Object

Like #get, but will raise an exception if the requested setting doesn’t exist in the database



43
44
45
46
47
# File 'lib/setting_accessors/setting_scaffold.rb', line 43

def get!(name, assignable = nil)
  setting_record!(name, assignable).value
rescue ActiveRecord::RecordNotFound
  raise ::SettingAccessors::SettingNotFoundError
end

#get_default_value(name, assignable = nil) ⇒ Object

Returns the default value for the given setting.

Returns:

  • (Object)

    the default value for the given setting



100
101
102
# File 'lib/setting_accessors/setting_scaffold.rb', line 100

def get_default_value(name, assignable = nil)
  new(name: name, assignable: assignable).default_value
end

#get_or_default(name, assignable) ⇒ Object

Tries to look the setting up using #get, if no existing setting is found, the setting’s default value is returned.

This only works for class-wise settings, meaning that an assignable has to be present.



57
58
59
60
61
# File 'lib/setting_accessors/setting_scaffold.rb', line 57

def get_or_default(name, assignable)
  get!(name, assignable)
rescue ::SettingAccessors::SettingNotFoundError
  get_default_value(name, assignable)
end

#set(name, value, assignable: nil) ⇒ Object

Creates or updates the setting with the given name

@toto: Bless the rains down in Africa!

Parameters:

  • name (String, Symbol)

    The setting’s name

  • value (Object)

    The new setting value

  • assignable (ActiveRecord::Base) (defaults to: nil)

    The optional record this setting belongs to. If not given, the setting is global.

Returns:

  • (Object)

    The newly set value



80
81
82
83
84
85
# File 'lib/setting_accessors/setting_scaffold.rb', line 80

def set(name, value, assignable: nil)
  (setting_record(name, assignable) || new(name: name, assignable: assignable)).tap do |setting|
    setting.raw_value = value
    setting.save
  end.value
end

#setting_record(name, assignable = nil) ⇒ Setting, NilClass

Looks up a setting record for the given name and assignable Unlike the other methods here, this one actually returns a Setting object instead of its value.

Returns:

  • (Setting, NilClass)

    The found setting or nil if not existing



111
112
113
# File 'lib/setting_accessors/setting_scaffold.rb', line 111

def setting_record(name, assignable = nil)
  find_by(name: name.to_s, assignable: assignable)
end

#setting_record!(name, assignable = nil) ⇒ Object

Like #setting_record, but will raise an ActiveRecord::RecordNotFound exception if the record doesn’t exist yet



119
120
121
# File 'lib/setting_accessors/setting_scaffold.rb', line 119

def setting_record!(name, assignable = nil)
  setting_record(name, assignable) || raise(ActiveRecord::RecordNotFound)
end