Skip to content

Commit

Permalink
Nested model's field gets ignored when inverse_of name matches with n…
Browse files Browse the repository at this point in the history
…ested_in field. Closes railsadminteam#1255
  • Loading branch information
mshibuya committed Jul 20, 2012
1 parent f8d58f7 commit 37e927c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/helpers/rails_admin/form_builder.rb
Expand Up @@ -43,7 +43,8 @@ def fieldset_for fieldset, nested_in
def field_wrapper_for field, nested_in
if field.label
# do not show nested field if the target is the origin
unless field.inverse_of.presence && field.inverse_of == nested_in
unless field.inverse_of.presence && field.inverse_of == nested_in &&
@template.instance_variable_get(:@model_config).abstract_model == field.associated_model_config.abstract_model
@template.content_tag(:div, :class => "control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", :id => "#{dom_id(field)}_field") do
label(field.method_name, field.label, :class => 'control-label') +
(field.nested_form ? field_for(field) : input_for(field))
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy_app/app/active_record/another_field_test.rb
@@ -0,0 +1,4 @@
class AnotherFieldTest < ActiveRecord::Base
has_many :nested_field_tests, :inverse_of => :another_field_test
attr_accessible :nested_field_test_ids
end
3 changes: 2 additions & 1 deletion spec/dummy_app/app/active_record/nested_field_test.rb
@@ -1,7 +1,8 @@
class NestedFieldTest < ActiveRecord::Base
attr_accessible :title, :field_test_id, :comment_attributes
attr_accessible :title, :field_test_id, :another_field_test_id, :comment_attributes

belongs_to :field_test, :inverse_of => :nested_field_tests
belongs_to :another_field_test, :inverse_of => :nested_field_tests
has_one :comment, :as => :commentable
accepts_nested_attributes_for :comment, :allow_destroy => true, :reject_if => proc { |attributes| attributes["content"].blank? }
end
6 changes: 6 additions & 0 deletions spec/dummy_app/app/mongoid/another_field_test.rb
@@ -0,0 +1,6 @@
class AnotherFieldTest
include Mongoid::Document

has_many :nested_field_tests, :inverse_of => :another_field_test
attr_accessible :nested_field_test_ids
end
3 changes: 2 additions & 1 deletion spec/dummy_app/app/mongoid/nested_field_test.rb
Expand Up @@ -3,9 +3,10 @@ class NestedFieldTest

field :title, :type => String
belongs_to :field_test, :inverse_of => :nested_field_tests
belongs_to :another_field_test, :inverse_of => :nested_field_tests
include Mongoid::Timestamps

attr_accessible :title, :field_test_id, :comment_attributes
attr_accessible :title, :field_test_id, :another_field_test_id, :comment_attributes

has_one :comment, :as => :commentable, :autosave => true
accepts_nested_attributes_for :comment, :allow_destroy => true, :reject_if => proc { |attributes| attributes["content"].blank? }
Expand Down
@@ -0,0 +1,8 @@
class CreateAnotherFieldTests < ActiveRecord::Migration
def change
create_table :another_field_tests do |t|
t.timestamps
end
add_column :nested_field_tests, :another_field_test_id, :integer
end
end
8 changes: 8 additions & 0 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Expand Up @@ -853,6 +853,14 @@ class HelpTest < Tableless
should have_selector('.fields_blueprint .remove_nested_fields')
end
end

describe "when a field which have the same name of nested_in field's" do
it "should not hide fields which is not associated with nesting parent field's model" do
visit new_path(:model_name => "field_test")
should_not have_selector('select#field_test_nested_field_tests_attributes_new_nested_field_tests_field_test_id')
should have_selector('select#field_test_nested_field_tests_attributes_new_nested_field_tests_another_field_test_id')
end
end
end

describe 'embedded model', :mongoid => true do
Expand Down

0 comments on commit 37e927c

Please sign in to comment.