Module: SettingAccessors::SettingScaffold::ClassMethods

Defined in:
lib/setting_accessors/setting_scaffold.rb

Instance Method Summary collapse

Instance Method Details

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

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.



34
35
36
# File 'lib/setting_accessors/setting_scaffold.rb', line 34

def [](name, assignable = nil)
  self.setting_record(name, assignable).try(:value)
end

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

Shortcut for #create_or_update

The second argument is an optional assignable

Parameters:

  • name (String, Symbol)

    The setting’s name



105
106
107
108
# File 'lib/setting_accessors/setting_scaffold.rb', line 105

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

#create_default_setting(name, assignable = nil) ⇒ Object

Creates a new setting for the given name and assignable, using the setting’s default value stored in the config file

If the setting already exists, its value will not be overridden

Examples:

Create a global default setting ‘meaning_of_life’

Setting.create_default_setting(:meaning_of_life)

Create a default setting for all users in the system

User.all.each { |u| Setting.create_default_setting(:some_setting, u) }

Parameters:

  • name (String)

    The setting’s name

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

    An optional assignable



128
129
130
# File 'lib/setting_accessors/setting_scaffold.rb', line 128

def create_default_setting(name, assignable = nil)
  self.create_or_update(name, self.get_or_default(name, assignable), assignable)
end

#create_or_update(name, value, assignable = nil, return_value = false) ⇒ Object, Setting

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.

  • return_value (Boolean) (defaults to: false)

    If set to true, only the setting’s value is returned

Returns:

  • (Object, Setting)

    Depending on return_value either the newly created Setting record or the newly assigned value. This is due to the fact that Setting.my_setting = ‘something’ should show the same behaviour as other attribute assigns in the system while you might still want to get validation errors on custom settings.

    Please note that - if return_value is set to true, #save! is used instead of #save to ensure that validation errors are noticed by the programmer / user. As this is usually only the case when coming from method_missing, it should not happen anyways



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/setting_accessors/setting_scaffold.rb', line 83

def create_or_update(name, value, assignable = nil, return_value = false)
  setting       = self.setting_record(name, assignable)
  setting     ||= self.new(:name => name, :assignable => assignable)
  setting.set_value(value)

  if return_value
    setting.save!
    setting.value
  else
    setting.save
    setting
  end
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



135
136
137
# File 'lib/setting_accessors/setting_scaffold.rb', line 135

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

#get_or_default(name, assignable = nil) ⇒ Object

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



44
45
46
47
48
49
50
# File 'lib/setting_accessors/setting_scaffold.rb', line 44

def get_or_default(name, assignable = nil)
  if (val = self[name, assignable]).nil?
    self.new(:name => name, :assignable => assignable).default_value
  else
    val
  end
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



146
147
148
# File 'lib/setting_accessors/setting_scaffold.rb', line 146

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

#validation_errors(name, value, assignable = nil) ⇒ Array<String>

Tests, if the given value would be valid for the given setting name. This is done in this class method due to the process of setting creation through assigned records which does not allow going the “normal” way of testing whether a setting was saved correctly or not.

Returns:

  • (Array<String>)

    The validation errors for the setting’s value



159
160
161
162
163
# File 'lib/setting_accessors/setting_scaffold.rb', line 159

def validation_errors(name, value, assignable = nil)
  s = self.new(:name => name, :value => value, :assignable => assignable)
  s.valid?
  s.errors.get(:value) || []
end