Skip to content

Commit

Permalink
Fix bug when simple_fields_for is used with a hask like model and imp…
Browse files Browse the repository at this point in the history
…rove coverage.

Thanks to https://github.com/spohlenz for figuring this out in the Rails code.
See more: rails/rails#1778
  • Loading branch information
rafaelfranca committed Jul 19, 2011
1 parent b43fac9 commit 14b6263
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
simple_form (1.4.1)
simple_form (1.4.3.dev)
actionpack (~> 3.0)
activemodel (~> 3.0)

Expand Down Expand Up @@ -43,7 +43,7 @@ GEM
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
tzinfo (0.3.28)
tzinfo (0.3.29)

PLATFORMS
ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/action_view_extensions/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def simple_form_for(record, options={}, &block)
end

def simple_fields_for(record_name, record_object = nil, options = {}, &block)
options, record_object = record_object, nil if record_object.is_a?(Hash)
options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
options[:builder] ||= SimpleForm::FormBuilder

with_custom_field_error_proc do
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SimpleForm
VERSION = "1.4.2".freeze
VERSION = "1.4.3.dev".freeze
end
13 changes: 13 additions & 0 deletions test/action_view_extensions/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,19 @@ def with_collection_check_boxes(object, attribute, collection, value_method, tex
end
end

test 'fields for with a hash like model yeilds an instance of FormBuilder' do
@hash_backed_author = HashBackedAuthor.new

with_concat_form_for(:user) do |f|
f.simple_fields_for(:author, @hash_backed_author) do |author|
assert author.instance_of?(SimpleForm::FormBuilder)
author.input :name
end
end

assert_select "input[name='user[author][name]'][value='hash backed author']"
end

test 'fields for yields an instance of CustomBuilder if main builder is a CustomBuilder' do
with_custom_form_for(:user) do |f|
f.simple_fields_for(:company) do |company|
Expand Down
11 changes: 11 additions & 0 deletions test/action_view_extensions/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ class FormHelperTest < ActionView::TestCase
assert f.instance_of?(SimpleForm::FormBuilder)
end)
end

test 'fields for with a hash like model yeilds an instance of FormBuilder' do
@hash_backed_author = HashBackedAuthor.new

concat(simple_fields_for(:author, @hash_backed_author) do |f|
assert f.instance_of?(SimpleForm::FormBuilder)
f.input :name
end)

assert_select "input[name='author[name]'][value='hash backed author']"
end
end
11 changes: 11 additions & 0 deletions test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,14 @@ class OtherValidatingUser < User
:less_than_or_equal_to => Proc.new { |user| user.age + 100},
:only_integer => true
end

class HashBackedAuthor < Hash
extend ActiveModel::Naming
include ActiveModel::Conversion

def persisted?; false; end

def name
'hash backed author'
end
end

0 comments on commit 14b6263

Please sign in to comment.