Module: ActiveRecord::Configuration::ClassMethods
- Defined in:
- lib/active_record/configuration.rb
Instance Method Summary collapse
-
#configure(&block) ⇒ Object
Configures the current ActiveRecord class to allow specific options.
Instance Method Details
#configure(&block) ⇒ Object
Configures the current ActiveRecord class to allow specific options. After being configured, a #settings method will be defined against all instances as well as a has_many :active_configuration_settings relationship for storing settings.
Example configuration:
class Category < ActiveRecord::Base
configure do
option :sort do
default 'alphabetical'
restrict 'alphabetical', 'manual'
end
option :limit do
format 'fixnum'
end
option :price_filter do
format 'float'
modifiers 'eq', 'lt', 'gt', 'lte', 'gte'
multiple true
end
end
end
The #configure block can only contain #option blocks. Within each option block may be a number of methods such as:
-
default
A default value. Cannot be used in conjunction with multiple.
-
format - A specific format, including: ‘string’, ‘fixnum’, ‘float’, ‘boolean’, ‘email’, ‘url’ or a /regular expression/. Defaults to ‘string’.
-
restrict - An array of allowed values.
-
modifiers - An array of allowed modifiers.
-
multiple - Whether or not multiple Settings can be set against the single option. Must be set to either true or false. Defaults to false.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_record/configuration.rb', line 65 def configure(&block) class_eval "\n # Includes the #settings method for reading and writing settings\n # against any instances of this class.\n include ActiveRecord::Configuration::InstanceMethods\n\n # Where the actual settings are stored against the instance.\n has_many :active_configuration_settings, :as => :configurable, :class_name => 'ActiveConfiguration::Setting'\n\n # Validates are run on settings along with other validations.\n validate :validate_settings\n\n # After being saved, outstanding setting modifications are saved.\n after_save :save_settings\n\n # Returns the configuration details of this class.\n def self.configuration\n @configuration ||= ActiveConfiguration::Base.new\n end\n EOV\n\n # Evaluates the configuration block given to #configure. Each\n # option block is then evaluated and options are setup. For more\n # details, see ActiveConfiguration::Base.\n configuration.instance_eval(&block)\nend\n" |