module ActiveRecord::Acts::Select::ClassMethods
Acts_as_select allows you to automatically use the columns from an activerecord reference table with a descriptive name and primary key ids It generates XXXX_select method for each permitted column, which maps that column and its primary key to an array suitable for use in an option_select or as a container for options_for_select.
Public Instance Methods
acts_as_select(*opts)
click to toggle source
# File lib/active_record/acts/select.rb, line 35 def acts_as_select(*opts) allowed_columns(*opts).each do |field| (class << self;self;end).class_eval do define_method "#{field}_select" do if respond_to?(:scoped) scoped(:select=>"#{field}, #{primary_key}") else select(field,primary_key) end.map{|x| [x.send(field),x.send(primary_key)]} end define_method "#{field}_list" do (respond_to?(:scoped) ? scoped(:select=>field) : select(field)).map{|x| x.send(field)} end define_method "#{field}_objects" do respond_to?(:scoped) ? scoped(:select=>"#{field}, #{primary_key}") : select("#{field}, #{primary_key}") end end end end
Private Instance Methods
allowed_columns(*opts)
click to toggle source
# File lib/active_record/acts/select.rb, line 27 def allowed_columns(*opts) opts=!opts.empty? && opts.first.is_a?(Hash) ? default_opts.merge(opts.first) : default_opts opts[:include]=[opts[:include]].flatten.map(&:to_s) opts[:exclude]=[opts[:exclude]].flatten.map(&:to_s) column_names & (opts[:include]-opts[:exclude]) end
default_opts()
click to toggle source
# File lib/active_record/acts/select.rb, line 20 def default_opts { :include=>column_names, :exclude=>[] } end