Module: ActiveRecord::Acts::Toggleable::ClassMethods
- Defined in:
- lib/acts_as_toggleable/active_record/acts/toggleable.rb
Instance Method Summary collapse
-
#acts_as_toggleable(toggleable_attribute, options = {}) ⇒ Object
Public: Create a toggleable attribute on the model.
Instance Method Details
#acts_as_toggleable(toggleable_attribute, options = {}) ⇒ Object
Public: Create a toggleable attribute on the model.
toggleable_attribute - A symbol representing the name of the attribute to be created. options - The Hash options used to customize the attribute (default: {}):
:scope_name - The Symbol representing the name of the positive scope
(optional, default: #{toggleable_attribute}).
:inverse_scope_name - The Symbol representing the name of the negative scope
(optional, default: #{toggleable_attribute}).
Examples:
acts_as_toggleable :for_sale, :default => false
acts_as_toggleable :visible, :inverse_scope_name => :hidden
acts_as_toggleable :publishable, :default => false, :inverse_scope_name => :unpublishable
acts_as_toggleable :featured, :default => true, :scope_name => :features
Defines the attribute specified as well as relevant scopes.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/acts_as_toggleable/active_record/acts/toggleable.rb', line 28 def acts_as_toggleable(toggleable_attribute, = {}) defaults = { :scope_name => toggleable_attribute, :inverse_scope_name => "not_#{toggleable_attribute}" } = defaults.merge() scope [:scope_name], -> { where(toggleable_attribute => true) } scope [:inverse_scope_name], -> { where(toggleable_attribute => false) } end |