Preload only collection associations to avoid "to_a is obsolete" warnings

This commit is contained in:
Carlos Antonio da Silva 2011-05-16 23:17:09 -03:00
parent 8fe015a30b
commit a1ea09c37f
4 changed files with 30 additions and 18 deletions

View File

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

View File

@ -164,13 +164,13 @@ module SimpleForm
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))

View File

@ -585,17 +585,27 @@ class FormBuilderTest < ActionView::TestCase
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

View File

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