Permalink
Browse files

Raise error when embeds_* was used without accept_nested_attributes_f…

…or. refs #1059
  • Loading branch information...
1 parent 9382081 commit 78e192543cf53706265f883e101dbeac3dc5ae9a @mshibuya mshibuya committed Apr 3, 2012
Showing with 97 additions and 14 deletions.
  1. +12 −2 lib/rails_admin/adapters/mongoid.rb
  2. +85 −12 spec/unit/adapters/mongoid_spec.rb
@@ -282,7 +282,15 @@ def association_foreign_inverse_of_lookup(association)
end
def association_nested_attributes_options_lookup(association)
- model.nested_attributes_options.try { |o| o[association.name.to_sym] }
+ nested = model.nested_attributes_options.try { |o| o[association.name.to_sym] }
+ if !nested && [:embeds_one, :embeds_many].include?(association.macro.to_sym)
+ raise <<-MSG.gsub(/^\s+/, '')
+ Embbeded association without accept_nested_attributes_for can't be handled by RailsAdmin,
+ because embedded model doesn't have top-level access.
+ Please add `accept_nested_attributes_for :#{association.name}' line to `#{model.to_s}' model.
+ MSG
+ end
+ nested
end
def association_as_lookup(association)
@@ -302,7 +310,9 @@ def association_inverse_of_lookup(association)
end
def association_foreign_key_lookup(association)
- association.foreign_key.to_sym rescue nil
+ unless [:embeds_one, :embeds_many].include?(association.macro.to_sym)
+ association.foreign_key.to_sym rescue nil
+ end
end
def association_type_lookup(macro)
@@ -18,6 +18,8 @@ class MongoPost
belongs_to :mongo_blog
has_and_belongs_to_many :mongo_categories
has_many :mongo_comments, :as => :commentable
+ embeds_one :mongo_note
+ accepts_nested_attributes_for :mongo_note
end
class MongoCategory
@@ -28,6 +30,8 @@ class MongoCategory
class MongoUser
include Mongoid::Document
has_one :mongo_profile
+ embeds_many :mongo_notes
+ accepts_nested_attributes_for :mongo_notes
field :name, :type => String
field :message, :type => String
field :short_text, :type => String
@@ -45,28 +49,34 @@ class MongoComment
belongs_to :commentable, :polymorphic => true
end
- @blog = RailsAdmin::AbstractModel.new(MongoBlog)
- @post = RailsAdmin::AbstractModel.new(MongoPost)
- @category = RailsAdmin::AbstractModel.new(MongoCategory)
- @user = RailsAdmin::AbstractModel.new(MongoUser)
- @profile = RailsAdmin::AbstractModel.new(MongoProfile)
- @comment = RailsAdmin::AbstractModel.new(MongoComment)
+ class MongoNote
+ include Mongoid::Document
+ embedded_in :mongo_post
+ embedded_in :mongo_user
+ end
+
+ @blog = RailsAdmin::AbstractModel.new MongoBlog
+ @post = RailsAdmin::AbstractModel.new MongoPost
+ @category = RailsAdmin::AbstractModel.new MongoCategory
+ @user = RailsAdmin::AbstractModel.new MongoUser
+ @profile = RailsAdmin::AbstractModel.new MongoProfile
+ @comment = RailsAdmin::AbstractModel.new MongoComment
end
after :all do
RailsAdmin::AbstractModel.reset_polymorphic_parents
end
it 'lists associations' do
- @post.associations.map{|a|a[:name].to_s}.should =~ ['mongo_blog', 'mongo_categories', 'mongo_comments']
+ @post.associations.map{|a| a[:name]}.should =~ [:mongo_blog, :mongo_categories, :mongo_comments, :mongo_note]
end
it 'reads correct and know types in [:belongs_to, :has_and_belongs_to_many, :has_many, :has_one]' do
(@post.associations + @blog.associations + @user.associations).map{|a|a[:type].to_s}.uniq.should =~ ['belongs_to', 'has_and_belongs_to_many', 'has_many', 'has_one']
end
it "has correct parameter of belongs_to association" do
- param = @post.associations.select{|a| a[:name] == :mongo_blog}.first
+ param = @post.associations.find{|a| a[:name] == :mongo_blog}
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_blog,
:pretty_name=>"Mongo blog",
@@ -85,7 +95,7 @@ class MongoComment
end
it "has correct parameter of has_many association" do
- param = @blog.associations.select{|a| a[:name] == :mongo_posts}.first
+ param = @blog.associations.find{|a| a[:name] == :mongo_posts}
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_posts,
:pretty_name=>"Mongo posts",
@@ -109,7 +119,7 @@ class MongoComment
end
it "has correct parameter of has_and_belongs_to_many association" do
- param = @post.associations.select{|a| a[:name] == :mongo_categories}.first
+ param = @post.associations.find{|a| a[:name] == :mongo_categories}
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_categories,
:pretty_name=>"Mongo categories",
@@ -129,7 +139,7 @@ class MongoComment
it "has correct parameter of polymorphic belongs_to association" do
RailsAdmin::Config.stub!(:models_pool).and_return(["MongoBlog", "MongoPost", "MongoCategory", "MongoUser", "MongoProfile", "MongoComment"])
- param = @comment.associations.select{|a| a[:name] == :commentable}.first
+ param = @comment.associations.find{|a| a[:name] == :commentable}
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:commentable,
:pretty_name=>"Commentable",
@@ -149,7 +159,7 @@ class MongoComment
it "has correct parameter of polymorphic inverse has_many association" do
RailsAdmin::Config.stub!(:models_pool).and_return(["MongoBlog", "MongoPost", "MongoCategory", "MongoUser", "MongoProfile", "MongoComment"])
- param = @blog.associations.select{|a| a[:name] == :mongo_comments}.first
+ param = @blog.associations.find{|a| a[:name] == :mongo_comments}
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_comments,
:pretty_name=>"Mongo comments",
@@ -166,6 +176,69 @@ class MongoComment
param[:primary_key_proc].call.should == :_id
param[:model_proc].call.should == MongoComment
end
+
+ it "has correct parameter of embeds_one association" do
+ param = @post.associations.find{|a| a[:name] == :mongo_note}
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
+ :name=>:mongo_note,
+ :pretty_name=>"Mongo note",
+ :type=>:has_one,
+ :foreign_key=>nil,
+ :foreign_type=>nil,
+ :foreign_inverse_of=>nil,
+ :as=>nil,
+ :polymorphic=>false,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>{:allow_destroy=>false, :update_only=>false}
+ }
+ param[:primary_key_proc].call.should == :_id
+ param[:model_proc].call.should == MongoNote
+ end
+
+ it "has correct parameter of embeds_many association" do
+ param = @user.associations.find{|a| a[:name] == :mongo_notes}
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
+ :name=>:mongo_notes,
+ :pretty_name=>"Mongo notes",
+ :type=>:has_many,
+ :foreign_key=>nil,
+ :foreign_type=>nil,
+ :foreign_inverse_of=>nil,
+ :as=>nil,
+ :polymorphic=>false,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>{:allow_destroy=>false, :update_only=>false}
+ }
+ param[:primary_key_proc].call.should == :_id
+ param[:model_proc].call.should == MongoNote
+ end
+
+ it "should raise error when embeds_* is used without accepts_nested_attributes_for" do
+ class MongoEmbedsOne
+ include Mongoid::Document
+ embeds_one :mongo_embedded
+ end
+
+ class MongoEmbedsMany
+ include Mongoid::Document
+ embeds_many :mongo_embeddeds
+ end
+
+ class MongoEmbedded
+ include Mongoid::Document
+ embedded_in :mongo_embeds_one
+ embedded_in :mongo_embeds_many
+ end
+
+ lambda{ RailsAdmin::AbstractModel.new(MongoEmbedsOne).associations }.should raise_error(RuntimeError,
+ "Embbeded association without accept_nested_attributes_for can't be handled by RailsAdmin,\nbecause embedded model doesn't have top-level access.\nPlease add `accept_nested_attributes_for :mongo_embedded' line to `MongoEmbedsOne' model.\n"
+ )
+ lambda{ RailsAdmin::AbstractModel.new(MongoEmbedsMany).associations }.should raise_error(RuntimeError,
+ "Embbeded association without accept_nested_attributes_for can't be handled by RailsAdmin,\nbecause embedded model doesn't have top-level access.\nPlease add `accept_nested_attributes_for :mongo_embeddeds' line to `MongoEmbedsMany' model.\n"
+ )
+ end
end
describe "#properties" do

0 comments on commit 78e1925

Please sign in to comment.