Skip to content

Commit

Permalink
Merge pull request #388 from mattvague/42-form-builder-breaks-on-poly…
Browse files Browse the repository at this point in the history
…morphic-associations

Form builder no longer breaks on polymorphic associations. Closes #42
  • Loading branch information
mattvague committed Aug 19, 2011
2 parents 44b9b25 + d38f831 commit 9c130db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/active_admin/form_builder.rb
Expand Up @@ -20,7 +20,9 @@ def inputs(*args, &block)
# The input method returns a properly formatted string for # The input method returns a properly formatted string for
# its contents, so we want to skip the internal buffering # its contents, so we want to skip the internal buffering
# while building up its contents # while building up its contents
def input(*args) def input(method, *args)
return if polymorphic_belongs_to_association?(method)

content = with_new_form_buffer { super } content = with_new_form_buffer { super }
return content.html_safe unless @inputs_with_block return content.html_safe unless @inputs_with_block
form_buffers.last << content.html_safe form_buffers.last << content.html_safe
Expand Down Expand Up @@ -98,6 +100,13 @@ def has_many(association, options = {}, &block)


private private


# Pass in a method to check if it's a polymorphic association
def polymorphic_belongs_to_association?(method)
reflection = reflection_for(method)

reflection && reflection.macro == :belongs_to && reflection.options[:polymorphic]
end

def with_new_form_buffer def with_new_form_buffer
form_buffers << "".html_safe form_buffers << "".html_safe
return_value = yield return_value = yield
Expand Down
17 changes: 17 additions & 0 deletions spec/unit/form_builder_spec.rb
Expand Up @@ -64,6 +64,23 @@ def build_form(options = {}, &block)
end end
end end


context "when polymorphic relationship" do

let(:body) do
comment = ActiveAdmin::Comment.new

active_admin_form_for comment, :url => "admins/comments" do |f|
f.inputs :resource
end

end

it "should not generate any field" do
body.should have_tag("form", :attributes => { :method => 'post' })
body.should_not have_tag("select")
end
end

describe "passing in options" do describe "passing in options" do
let :body do let :body do
build_form :html => { :multipart => true } do |f| build_form :html => { :multipart => true } do |f|
Expand Down

0 comments on commit 9c130db

Please sign in to comment.