Browse files

Fix bug when simple_fields_for is used with a hask like model

Thanks to @rafaelfranca and @spohlenz fixing in simple_form and Rails!

See:
plataformatec/simple_form@14b6263
rails/rails#1778

ref #568
  • Loading branch information...
1 parent aac8d75 commit 131f3508e7e435671010fbc105f0ba4d894ea407 @justinfrench committed Jul 19, 2011
Showing with 30 additions and 2 deletions.
  1. +1 −1 lib/formtastic/helpers/form_helper.rb
  2. +14 −1 spec/builder/semantic_fields_for_spec.rb
  3. +15 −0 spec/spec_helper.rb
View
2 lib/formtastic/helpers/form_helper.rb
@@ -166,7 +166,7 @@ def semantic_form_for(record_or_name_or_array, *args, &proc)
#
# @see #semantic_form_for
def semantic_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] ||= @@builder
@@builder.custom_namespace = options[:namespace].to_s # TODO needed?
View
15 spec/builder/semantic_fields_for_spec.rb
@@ -22,6 +22,9 @@
semantic_fields_for(:author, @new_post.author) do |nested_builder|
nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
end
+ semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
+ nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
+ end
end
it 'should respond to input' do
@@ -34,6 +37,9 @@
semantic_fields_for(:author, @new_post.author) do |nested_builder|
nested_builder.respond_to?(:input).should be_true
end
+ semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
+ nested_builder.respond_to?(:input).should be_true
+ end
end
end
@@ -46,6 +52,14 @@
end
end
+ it 'yields an instance of FormHelper.builder with hash-like model' do
+ semantic_form_for(:user) do |builder|
+ builder.semantic_fields_for(:author, @hash_backed_author) do |nested_builder|
+ nested_builder.class.should == Formtastic::Helpers::FormHelper.builder
+ end
+ end
+ end
+
it 'nests the object name' do
semantic_form_for(@new_post) do |builder|
builder.semantic_fields_for(@bob) do |nested_builder|
@@ -113,4 +127,3 @@
end
end
-
View
15 spec/spec_helper.rb
@@ -93,6 +93,7 @@ def id
def persisted?
end
end
+
module ::Namespaced
class Post
extend ActiveModel::Naming if defined?(ActiveModel::Naming)
@@ -105,6 +106,7 @@ def persisted?
end
end
end
+
class ::Author
extend ActiveModel::Naming if defined?(ActiveModel::Naming)
include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
@@ -115,10 +117,21 @@ def to_label
def persisted?
end
end
+
+ class ::HashBackedAuthor < Hash
+ extend ActiveModel::Naming if defined?(ActiveModel::Naming)
+ include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
+ def persisted?; false; end
+ def name
+ 'hash backed author'
+ end
+ end
+
class ::Continent
extend ActiveModel::Naming if defined?(ActiveModel::Naming)
include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
end
+
class ::PostModel
extend ActiveModel::Naming if defined?(ActiveModel::Naming)
include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
@@ -209,6 +222,8 @@ def new_author_path; "/authors/new"; end
::Author.stub!(:to_key).and_return(nil)
::Author.stub!(:persisted?).and_return(nil)
+ @hash_backed_author = HashBackedAuthor.new
+
# Sometimes we need a mock @post object and some Authors for belongs_to
@new_post = mock('post')
@new_post.stub!(:class).and_return(::Post)

0 comments on commit 131f350

Please sign in to comment.