module RecordSelect::FormBuilder

Public Instance Methods

record_select(association, options = {}) click to toggle source
# File lib/record_select/form_builder.rb, line 3
def record_select(association, options = {})
  reflection = @object.class.reflect_on_association(association)
  form_name = form_name_for_association(reflection)
  current = @object.send(association)
  options[:id] ||= "#{@object_name.gsub(/[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")}_#{association}"

  if [:has_one, :belongs_to].include? reflection.macro
    @template.record_select_field(form_name, current || reflection.klass.new, options)
  else
    options[:controller] ||= reflection.klass.to_s.pluralize.underscore
    @template.record_multi_select_field(form_name, current, options)
  end
end

Private Instance Methods

form_name_for_association(reflection) click to toggle source
# File lib/record_select/form_builder.rb, line 19
def form_name_for_association(reflection)
  key_name = (reflection.options[:foreign_key] || reflection.association_foreign_key)
  key_name += "s" unless [:has_one, :belongs_to].include? reflection.macro
  form_name = "#{@object_name}[#{key_name}]"
end