Module: SettingsDB::ActsAsSetting::ClassMethods
- Defined in:
- lib/settingsdb/acts_as_setting.rb
Instance Method Summary collapse
-
#acts_as_setting(options = {}) ⇒ Object
This method causes the model to import the SettingsDB behavior A SettingsDB enabled modle requires 3 fields: name, namespace, and value This method also takes options to override the default names of these fields.
Instance Method Details
#acts_as_setting(options = {}) ⇒ Object
This method causes the model to import the SettingsDB behavior A SettingsDB enabled modle requires 3 fields: name, namespace, and value This method also takes options to override the default names of these fields.
Options
- :setting_namespace_field
-
Override namespace field-name (default:
:namespace) - :setting_name_field
-
Override name field-name (default:
:name) - :setting_value_field
-
Override value field-name (default:
:value)
Examples
To use the default field names in your model, no options are needed:
class Setting < ActiveRecord::Base
acts_as_setting
end
If your model needs to rename a setting field’s name pass them as options to acts_as_settings. Here the Configuration model keeps the namespace value in the :scope field, and the setting name value in the :key field:
class Configuration < ActiveRecord::Base
acts_as_setting :setting_namespace_field => :scope, :setting_name_field => :key
end
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/settingsdb/acts_as_setting.rb', line 33 def acts_as_setting( = {}) cattr_accessor :setting_namespace_field, :setting_name_field, :setting_value_field self.setting_name_field = ([:setting_name_field] || :name).to_sym self.setting_namespace_field = ([:setting_namespace_field] || :namespace).to_sym self.setting_value_field = ([:setting_value_field] || :value).to_sym class_eval(" include SettingsDB::Settings\n serialize :\#{setting_value_field}\n before_destroy :remove_from_cache\n\n", __FILE__, __LINE__ + 1) end |