diff --git a/lib/simple_form.rb b/lib/simple_form.rb index fa8dd4626..be5fd28d3 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -91,13 +91,14 @@ module SimpleForm mattr_accessor :required_by_default @@required_by_default = true - # Tell browsers whether to use default HTML5 validations. + # Tell browsers whether to use default HTML5 validations (novalidate option). mattr_accessor :browser_validations @@browser_validations = true - # Determines whether HTML5 types (:email, :url, :search, :tel) and attributes (e.g. required) are used - # or not. True by default. - # Having this on in non-HTML5 compliant sites can cause odd behavior in HTML5-aware browsers such as Chrome. + # Determines whether HTML5 types (:email, :url, :search, :tel) and attributes + # (e.g. required) are used or not. True by default. + # Having this on in non-HTML5 compliant sites can cause odd behavior in + # HTML5-aware browsers such as Chrome. mattr_accessor :html5 @@html5 = true diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index b20fa3d39..ac57819e1 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -164,13 +164,13 @@ def association(association, options={}, &block) html_options[:multiple] = true unless html_options.key?(:multiple) end - :"#{reflection.name.to_s.singularize}_ids" - end + # Force the association to be preloaded for performance. + if options[:preload] != false && object.respond_to?(association) + target = object.send(association) + target.to_a if target.respond_to?(:to_a) + end - # Force the association to be preloaded for performance. - if options[:preload] != false && object.respond_to?(association) - target = object.send(association) - target.to_a if target.respond_to?(:to_a) + :"#{reflection.name.to_s.singularize}_ids" end input(attribute, options.merge(:reflection => reflection)) diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index ae2a9536a..fc9b5e4a7 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -585,17 +585,27 @@ def with_association_for(object, *args) end end - test 'builder caches given association' do - value = @user.company + test 'builder preloads collection association' do + value = @user.tags value.expects(:to_a).returns(value) - with_association_for @user, :company + with_association_for @user, :tags + assert_select 'form select.select#user_tag_ids' + assert_select 'form select option[value=1]', 'Tag 1' + assert_select 'form select option[value=2]', 'Tag 2' + assert_select 'form select option[value=3]', 'Tag 3' + end + + test 'builder does not preload collection association if preload false' do + value = @user.company + value.expects(:to_a).never + with_association_for @user, :company, :preload => false assert_select 'form select.select#user_company_id' assert_select 'form select option[value=1]', 'Company 1' assert_select 'form select option[value=2]', 'Company 2' assert_select 'form select option[value=3]', 'Company 3' end - test 'builder does not cache given association if preload false' do + test 'builder does not preload non collection association' do value = @user.company value.expects(:to_a).never with_association_for @user, :company, :preload => false diff --git a/test/support/models.rb b/test/support/models.rb index 04b4e243a..45ad2e54e 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -38,10 +38,11 @@ class User extend ActiveModel::Naming include ActiveModel::Conversion - attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :description, :created_at, :updated_at, - :credit_limit, :age, :password, :delivery_time, :born_at, :special_company_id, :country, :url, :tag_ids, - :avatar, :home_picture, :email, :status, :residence_country, :phone_number, :post_count, :lock_version, - :amount, :attempts + attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :age, + :description, :created_at, :updated_at, :credit_limit, :password, :url, + :delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids, + :avatar, :home_picture, :email, :status, :residence_country, :phone_number, + :post_count, :lock_version, :amount, :attempts def initialize(options={}) options.each do |key, value|