Skip to content

Commit

Permalink
Fix bug when simple_fields_for is used with a hask like model
Browse files Browse the repository at this point in the history
Thanks to @rafaelfranca and @spohlenz fixing in simple_form and Rails!

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

ref formtastic#568
  • Loading branch information
justinfrench committed Jul 19, 2011
1 parent aac8d75 commit 131f350
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/formtastic/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
15 changes: 14 additions & 1 deletion spec/builder/semantic_fields_for_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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|
Expand Down Expand Up @@ -113,4 +127,3 @@
end

end

15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def id
def persisted?
end
end

module ::Namespaced
class Post
extend ActiveModel::Naming if defined?(ActiveModel::Naming)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 131f350

Please sign in to comment.