Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wildchild/a01c429a80d1bb65e2ff to your computer and use it in GitHub Desktop.
Save wildchild/a01c429a80d1bb65e2ff to your computer and use it in GitHub Desktop.
From e8dbab8fce25a3b7c6d4efa14240a629aa2d14e2 Mon Sep 17 00:00:00 2001
From: Alexander Uvarov <alexander.uvarov@gmail.com>
Date: Wed, 1 Jun 2011 17:15:06 +0600
Subject: [PATCH] Allow to pass model inherited from Hash to fields_for
---
actionpack/lib/action_view/helpers/form_helper.rb | 11 +++++++----
actionpack/test/lib/controller/fake_models.rb | 13 +++++++++++++
actionpack/test/template/form_helper_test.rb | 11 +++++++++++
3 files changed, 31 insertions(+), 4 deletions(-)
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index cb1c139..beda01c 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -911,13 +911,16 @@ module ActionView
private
- def instantiate_builder(record_name, record_object, options, &block)
- case record_name
+ def instantiate_builder(record, *args, &block)
+ options = args.extract_options!
+ record_object = args.shift
+
+ case record
when String, Symbol
object = record_object
- object_name = record_name
+ object_name = record
else
- object = record_name
+ object = record
object_name = ActiveModel::Naming.param_key(object)
end
diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb
index 67baf36..00a5d5f 100644
--- a/actionpack/test/lib/controller/fake_models.rb
+++ b/actionpack/test/lib/controller/fake_models.rb
@@ -200,3 +200,16 @@ class RenderJsonTestException < Exception
return { :error => self.class.name, :message => self.to_s }.to_json
end
end
+
+class HashLike < Hash
+ extend ActiveModel::Naming
+ include ActiveModel::Conversion
+
+ def initialize(hash = {})
+ self.merge!(hash)
+ end
+
+ def title
+ self[:title]
+ end
+end
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index f2a49f7..656a874 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -1436,6 +1436,17 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, output_buffer
end
+ def test_fields_for_with_hash_given_as_object
+ object = HashLike.new(:title => "Hello World")
+
+ output_buffer = fields_for(object) do |f|
+ f.text_field(:title)
+ end
+
+ expected = '<input name="hash_like[title]" size="30" type="text" id="hash_like_title" value="Hello World" />'
+ assert_dom_equal expected, output_buffer
+ end
+
def test_nested_fields_for_with_new_records_on_a_nested_attributes_collection_association
@post.comments = [Comment.new, Comment.new]
--
1.7.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment