Skip to content
Browse files

Fix bug when simple_fields_for is used with a hask like model and imp…

…rove coverage.

Thanks to https://github.com/spohlenz for figuring this out in the Rails code.
See more: rails/rails#1778
  • Loading branch information...
1 parent b43fac9 commit 14b6263ee270af4ac972703f78cb5a2b4586033c @rafaelfranca rafaelfranca committed Jul 19, 2011
View
4 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- simple_form (1.4.1)
+ simple_form (1.4.3.dev)
actionpack (~> 3.0)
activemodel (~> 3.0)
@@ -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
View
2 lib/simple_form/action_view_extensions/form_helper.rb
@@ -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
View
2 lib/simple_form/version.rb
@@ -1,3 +1,3 @@
module SimpleForm
- VERSION = "1.4.2".freeze
+ VERSION = "1.4.3.dev".freeze
end
View
13 test/action_view_extensions/builder_test.rb
@@ -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|
View
11 test/action_view_extensions/form_helper_test.rb
@@ -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
View
11 test/support/models.rb
@@ -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.
Something went wrong with that request. Please try again.