1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Adding :include and :joins options to association finds.

This commit is contained in:
Carlos Antonio da Silva 2009-12-14 17:56:42 -02:00
parent e8598f27ca
commit cacbf269f3
4 changed files with 29 additions and 11 deletions

View file

@ -161,19 +161,15 @@ Simple enough right? This is going to render a :select input for choosing the :c
f.association :company, :as => :radio
f.association :roles, :as => :check_boxes
And you will get a set of radios to select the company and another set of check boxes for the roles. Some options are available for refining the collection for associations:
And you will get a set of radios to select the company and another set of check boxes for the roles. Some options are available for refining the collection for associations: :conditions, :include, :joins, :order. These options are given straight to the find method. Here is an example of how to use these options:
* conditions => given as conditions for retrieving the collection
* order => given as order for retrieving the collection
* scopes => named scopes to be called for retrieving the collection
Here is an example of how to use this options:
f.association :company, :order => 'name', :scopes => :active
f.association :company, :order => 'name'
f.association :roles, :conditions => { :active => true }
You also have the ability to call named scopes using the association:
f.association :company, :scope => [:active, :available]
The association helper just invokes input under the hood, so all options available to :select, :radio and :check_boxes are also available to association. For example, you can specify the collection by hand, all together with the prompt:
f.association :company, :collection => Company.active.all(:order => 'name'), :prompt => "Choose a Company"

View file

@ -91,6 +91,10 @@ module SimpleForm
#
# * :conditions - Given as conditions when retrieving the collection
#
# * :include - Given as include when retrieving the collection
#
# * :joins - Given as joins when retrieving the collection
#
# * :order - Given as order when retrieving the collection
#
# * :scope - Given as scopes when retrieving the collection
@ -136,7 +140,7 @@ module SimpleForm
end
options[:collection] ||= begin
find_options = options.slice(:conditions, :order)
find_options = options.slice(:conditions, :order, :include, :joins)
klass = Array(options[:scope]).inject(@reflection.klass) do |klass, scope|
klass.send(scope)
end

View file

@ -410,6 +410,22 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'form select option[value=3]'
end
test 'builder should allow passing include option to find collection' do
with_association_for @user, :company, :include => :city
assert_select 'form select.select#user_company_id'
assert_select 'form select option[value=1]'
assert_select 'form select option[value=2]'
assert_no_select 'form select option[value=3]'
end
test 'builder should allow passing joins option to find collection' do
with_association_for @user, :company, :joins => :city
assert_select 'form select.select#user_company_id'
assert_select 'form select option[value=2]'
assert_select 'form select option[value=3]'
assert_no_select 'form select option[value=1]'
end
test 'builder should allow overriding collection to association input' do
with_association_for @user, :company, :include_blank => false,
:collection => [Company.new(999, 'Teste')]

View file

@ -8,6 +8,8 @@ class Company < Struct.new(:id, :name)
all = (1..3).map{|i| Company.new(i, "Company #{i}")}
return [all.first] if options[:conditions]
return [all.last] if options[:order]
return all[0..1] if options[:include]
return all[1..2] if options[:joins]
all
end
end