Skip to content

Commit

Permalink
Preload only collection associations to avoid "to_a is obsolete" warn…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
carlosantoniodasilva committed May 17, 2011
1 parent 8fe015a commit a1ea09c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
9 changes: 5 additions & 4 deletions lib/simple_form.rb
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions lib/simple_form/form_builder.rb
Expand Up @@ -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))
Expand Down
18 changes: 14 additions & 4 deletions test/form_builder_test.rb
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions test/support/models.rb
Expand Up @@ -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|
Expand Down

0 comments on commit a1ea09c

Please sign in to comment.