Module: DeLynnBerry::Dropdown::ClassMethods

Defined in:
lib/acts_as_dropdown.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute dropdown_text_attr.



29
30
31
# File 'lib/acts_as_dropdown.rb', line 29

def dropdown_text_attr
  @dropdown_text_attr
end

Returns the value of attribute dropdown_value_attr.



29
30
31
# File 'lib/acts_as_dropdown.rb', line 29

def dropdown_value_attr
  @dropdown_value_attr
end

#find_argumentsObject

Returns the value of attribute find_arguments.



29
30
31
# File 'lib/acts_as_dropdown.rb', line 29

def find_arguments
  @find_arguments
end

#include_blankObject

Returns the value of attribute include_blank.



29
30
31
# File 'lib/acts_as_dropdown.rb', line 29

def include_blank
  @include_blank
end

Instance Method Details

#acts_as_dropdown(*args) ⇒ Object

Specify this act if you want to your model be used easily with the select form helper. By default the plugin assumes you want to use the class’ primary key for the option value and the name attribute for the option text.

The acts_as_dropdown class method operates much like the ActiveRecord#find method when it comes to customization. You can alter the :text and :value attributes that are used. You can also alter what items are collected from the database by passing in any of the regular ActiveRecord#find options (i.e. :conditions, :order, :group, :limit, :offset, etc.)

Examples:

class State < ActiveRecord::Base
  acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
end

State.to_dropdown   # => [["AL", 1], ["AK", 2], ["AZ", 3]]

class State < ActiveRecord::Base
  acts_as_dropdown :conditions => "id < 4", :order => "name DESC"
end

State.to_dropdown   # => [["Arizona", 3], ["Alaska", 2], ["Alabama", 1]]

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]]

Configuration options

  • text - This is the class attribute (database column) that will be used as the text/label for

    the option tag (defaults to 'name').
    
  • value - This is the class attribute (database column) that will be used to fill in the option’s

    value parameter (defaults to the class' primary_key).
    
  • include_blank - Specify true if you’d like to have a blank item added to the beginning of your list, or

    a string that will be placed in the value attribute of the option group.
    

All of ActiveRecord#find options are available as well:

  • :conditions: An SQL fragment like “administrator = 1” or [ “user_name = ?”, username ].

  • :order: A SQL fragment like “created_at DESC, name”.

  • :group: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.

  • :limit: An integer determining the limit on the number of rows that should be returned.

  • :offset: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.

  • :joins: An SQL fragment for additional joins like “LEFT JOIN comments ON comments.post_id = id”. (Rarely needed).

  • :include: Names associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer to already defined associations. See eager loading under Associations.

  • :select: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not include the joined columns.



85
86
87
88
89
90
91
92
93
94
# File 'lib/acts_as_dropdown.rb', line 85

def acts_as_dropdown(*args)
  options = {:text => 'name', :value => self.primary_key}
  options.merge!(args.pop) unless args.empty?
  options.merge!(:order => options[:value]) unless options.has_key?(:order)

  self.dropdown_text_attr   = options.delete(:text)
  self.dropdown_value_attr  = options.delete(:value)
  self.include_blank        = options.delete(:include_blank)
  self.find_arguments       = options
end

#to_options_for_select(*args) ⇒ Object Also known as: to_dropdown

Examples:

class State < ActiveRecord::Base
  acts_as_dropdown :text => "abbreviation", :conditions => "id < 4"
end

State.to_dropdown   # => [["AL", 1], ["AK", 2], ["AZ", 3]]

class State < ActiveRecord::Base
  acts_as_dropdown :conditions => "id < 4", :order => "name DESC"
end

State.to_dropdown   # => [["Arizona", 3], ["Alaska", 2], ["Alabama", 1]]

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]]

See DeLynnBerry::Dropdown::ClassMethods#acts_as_dropdown for additional configuration options



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/acts_as_dropdown.rb', line 121

def to_options_for_select(*args)
  options = args.empty? ? {} : args.pop
  text    = options.delete(:text)
  value   = options.delete(:value)
  blank   = options.delete(:include_blank)
  options.merge!(:order => value) if (!value.nil? && self.dropdown_value_attr != value) && options.has_key?(:order) == false

  items = find(:all, options.empty? ? self.find_arguments : options).to_dropdown(text   || self.dropdown_text_attr,
                                                                                 value  || self.dropdown_value_attr)

  if args.empty? && self.include_blank
    items.insert(0, self.include_blank.kind_of?(String) ? [self.include_blank, ""] : ["", ""])
  elsif blank
    items.insert(0, blank.kind_of?(String) ? [blank, ""] : ["", ""])
  end
  items
end