Module: ActiveRecord::Acts::Dropdown::ClassMethods
- Defined in:
- lib/rails/acts_as_dropdown/acts_as_dropdown.rb
Instance Method Summary collapse
-
#acts_as_dropdown(options = {}) ⇒ Object
Configuration options.
-
#to_dropdown(options = {}) ⇒ Object
Example:.
Instance Method Details
#acts_as_dropdown(options = {}) ⇒ Object
Configuration options
-
text
- This is the class attribute that will be used as the text/label for the option tag (defaults to name). -
value
- This is the class attribute that will be used to fill in the option’s value parameter (defaults to id). -
conditions
- This is the conditions string that will be used when executing thefind
method on the object (defaults to nil). -
order
- This is the order string that will be used when executing thefind
method on the object (defaults to thevalue
attribute).
70 71 72 73 74 75 76 77 |
# File 'lib/rails/acts_as_dropdown/acts_as_dropdown.rb', line 70 def acts_as_dropdown( = {}) cattr_accessor :dropdown_text_attr, :dropdown_value_attr, :conditions_string, :order_string self.dropdown_text_attr = [:text] || "name" self.dropdown_value_attr = [:value] || "id" self.conditions_string = [:conditions] || nil self.order_string = [:order] || self.dropdown_value_attr.to_s end |
#to_dropdown(options = {}) ⇒ Object
Example:
class State < ActiveRecord::Base
acts_as_dropdown
end
>> State.to_dropdown
=> [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]]
The value
, text
, conditions
, and order
can also be altered from the default configuration by using the options hash.
Example:
class State < ActiveRecord::Base
acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
end
>> State.to_dropdown
=> [["AL", 1], ["AK", 2], ["AZ", 3], ["CA", 4]]
The class method to_dropdown
can also alter the default class configuration using the same options hash.
Example:
class State < ActiveRecord::Base
acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
end
>> State.to_dropdown :text => "name", :conditions => nil
=> [["Alabama", 1], ["Alaska", 2], ["Arizona", 3], ["California", 4], ["Colorado", 5]]
110 111 112 113 |
# File 'lib/rails/acts_as_dropdown/acts_as_dropdown.rb', line 110 def to_dropdown( = {}) acts_as_dropdown() unless .empty? find(:all, :conditions => self.conditions_string, :order => self.order_string).to_dropdown(self.dropdown_text_attr.to_sym, self.dropdown_value_attr.to_sym) end |