Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Raise error when embeds_* was used without accept_nested_attributes_f…

…or. refs #1059
  • Loading branch information...
commit 78e192543cf53706265f883e101dbeac3dc5ae9a 1 parent 9382081
Mitsuhiro Shibuya authored April 03, 2012
14  lib/rails_admin/adapters/mongoid.rb
@@ -282,7 +282,15 @@ def association_foreign_inverse_of_lookup(association)
282 282
       end
283 283
 
284 284
       def association_nested_attributes_options_lookup(association)
285  
-        model.nested_attributes_options.try { |o| o[association.name.to_sym] }
  285
+        nested = model.nested_attributes_options.try { |o| o[association.name.to_sym] }
  286
+        if !nested && [:embeds_one, :embeds_many].include?(association.macro.to_sym)
  287
+          raise <<-MSG.gsub(/^\s+/, '')
  288
+          Embbeded association without accept_nested_attributes_for can't be handled by RailsAdmin,
  289
+          because embedded model doesn't have top-level access.
  290
+          Please add `accept_nested_attributes_for :#{association.name}' line to `#{model.to_s}' model.
  291
+          MSG
  292
+        end
  293
+        nested
286 294
       end
287 295
 
288 296
       def association_as_lookup(association)
@@ -302,7 +310,9 @@ def association_inverse_of_lookup(association)
302 310
       end
303 311
 
304 312
       def association_foreign_key_lookup(association)
305  
-        association.foreign_key.to_sym rescue nil
  313
+        unless [:embeds_one, :embeds_many].include?(association.macro.to_sym)
  314
+          association.foreign_key.to_sym rescue nil
  315
+        end
306 316
       end
307 317
       
308 318
       def association_type_lookup(macro)
97  spec/unit/adapters/mongoid_spec.rb
@@ -18,6 +18,8 @@ class MongoPost
18 18
         belongs_to :mongo_blog
19 19
         has_and_belongs_to_many :mongo_categories
20 20
         has_many :mongo_comments, :as => :commentable
  21
+        embeds_one :mongo_note
  22
+        accepts_nested_attributes_for :mongo_note
21 23
       end
22 24
 
23 25
       class MongoCategory
@@ -28,6 +30,8 @@ class MongoCategory
28 30
       class MongoUser
29 31
         include Mongoid::Document
30 32
         has_one :mongo_profile
  33
+        embeds_many :mongo_notes
  34
+        accepts_nested_attributes_for :mongo_notes
31 35
         field :name, :type => String
32 36
         field :message, :type => String
33 37
         field :short_text, :type => String
@@ -45,12 +49,18 @@ class MongoComment
45 49
         belongs_to :commentable, :polymorphic => true
46 50
       end
47 51
 
48  
-      @blog = RailsAdmin::AbstractModel.new(MongoBlog)
49  
-      @post = RailsAdmin::AbstractModel.new(MongoPost)
50  
-      @category = RailsAdmin::AbstractModel.new(MongoCategory)
51  
-      @user = RailsAdmin::AbstractModel.new(MongoUser)
52  
-      @profile = RailsAdmin::AbstractModel.new(MongoProfile)
53  
-      @comment = RailsAdmin::AbstractModel.new(MongoComment)
  52
+      class MongoNote
  53
+        include Mongoid::Document
  54
+        embedded_in :mongo_post
  55
+        embedded_in :mongo_user
  56
+      end
  57
+
  58
+      @blog     = RailsAdmin::AbstractModel.new MongoBlog
  59
+      @post     = RailsAdmin::AbstractModel.new MongoPost
  60
+      @category = RailsAdmin::AbstractModel.new MongoCategory
  61
+      @user     = RailsAdmin::AbstractModel.new MongoUser
  62
+      @profile  = RailsAdmin::AbstractModel.new MongoProfile
  63
+      @comment  = RailsAdmin::AbstractModel.new MongoComment
54 64
     end
55 65
 
56 66
     after :all do
@@ -58,7 +68,7 @@ class MongoComment
58 68
     end
59 69
 
60 70
     it 'lists associations' do
61  
-      @post.associations.map{|a|a[:name].to_s}.should =~ ['mongo_blog', 'mongo_categories', 'mongo_comments']
  71
+      @post.associations.map{|a| a[:name]}.should =~ [:mongo_blog, :mongo_categories, :mongo_comments, :mongo_note]
62 72
     end
63 73
 
64 74
     it 'reads correct and know types in [:belongs_to, :has_and_belongs_to_many, :has_many, :has_one]' do
@@ -66,7 +76,7 @@ class MongoComment
66 76
     end
67 77
 
68 78
     it "has correct parameter of belongs_to association" do
69  
-      param = @post.associations.select{|a| a[:name] == :mongo_blog}.first
  79
+      param = @post.associations.find{|a| a[:name] == :mongo_blog}
70 80
       param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
71 81
         :name=>:mongo_blog,
72 82
         :pretty_name=>"Mongo blog",
@@ -85,7 +95,7 @@ class MongoComment
85 95
     end
86 96
 
87 97
     it "has correct parameter of has_many association" do
88  
-      param = @blog.associations.select{|a| a[:name] == :mongo_posts}.first
  98
+      param = @blog.associations.find{|a| a[:name] == :mongo_posts}
89 99
       param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
90 100
         :name=>:mongo_posts,
91 101
         :pretty_name=>"Mongo posts",
@@ -109,7 +119,7 @@ class MongoComment
109 119
     end
110 120
 
111 121
     it "has correct parameter of has_and_belongs_to_many association" do
112  
-      param = @post.associations.select{|a| a[:name] == :mongo_categories}.first
  122
+      param = @post.associations.find{|a| a[:name] == :mongo_categories}
113 123
       param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
114 124
         :name=>:mongo_categories,
115 125
         :pretty_name=>"Mongo categories",
@@ -129,7 +139,7 @@ class MongoComment
129 139
 
130 140
     it "has correct parameter of polymorphic belongs_to association" do
131 141
       RailsAdmin::Config.stub!(:models_pool).and_return(["MongoBlog", "MongoPost", "MongoCategory", "MongoUser", "MongoProfile", "MongoComment"])
132  
-      param = @comment.associations.select{|a| a[:name] == :commentable}.first
  142
+      param = @comment.associations.find{|a| a[:name] == :commentable}
133 143
       param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
134 144
         :name=>:commentable,
135 145
         :pretty_name=>"Commentable",
@@ -149,7 +159,7 @@ class MongoComment
149 159
 
150 160
     it "has correct parameter of polymorphic inverse has_many association" do
151 161
       RailsAdmin::Config.stub!(:models_pool).and_return(["MongoBlog", "MongoPost", "MongoCategory", "MongoUser", "MongoProfile", "MongoComment"])
152  
-      param = @blog.associations.select{|a| a[:name] == :mongo_comments}.first
  162
+      param = @blog.associations.find{|a| a[:name] == :mongo_comments}
153 163
       param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
154 164
         :name=>:mongo_comments,
155 165
         :pretty_name=>"Mongo comments",
@@ -166,6 +176,69 @@ class MongoComment
166 176
       param[:primary_key_proc].call.should == :_id
167 177
       param[:model_proc].call.should == MongoComment
168 178
     end
  179
+
  180
+    it "has correct parameter of embeds_one association" do
  181
+      param = @post.associations.find{|a| a[:name] == :mongo_note}
  182
+      param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
  183
+        :name=>:mongo_note,
  184
+        :pretty_name=>"Mongo note",
  185
+        :type=>:has_one,
  186
+        :foreign_key=>nil,
  187
+        :foreign_type=>nil,
  188
+        :foreign_inverse_of=>nil,
  189
+        :as=>nil,
  190
+        :polymorphic=>false,
  191
+        :inverse_of=>nil,
  192
+        :read_only=>nil,
  193
+        :nested_form=>{:allow_destroy=>false, :update_only=>false}
  194
+      }
  195
+      param[:primary_key_proc].call.should == :_id
  196
+      param[:model_proc].call.should == MongoNote
  197
+    end
  198
+
  199
+    it "has correct parameter of embeds_many association" do
  200
+      param = @user.associations.find{|a| a[:name] == :mongo_notes}
  201
+      param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
  202
+        :name=>:mongo_notes,
  203
+        :pretty_name=>"Mongo notes",
  204
+        :type=>:has_many,
  205
+        :foreign_key=>nil,
  206
+        :foreign_type=>nil,
  207
+        :foreign_inverse_of=>nil,
  208
+        :as=>nil,
  209
+        :polymorphic=>false,
  210
+        :inverse_of=>nil,
  211
+        :read_only=>nil,
  212
+        :nested_form=>{:allow_destroy=>false, :update_only=>false}
  213
+      }
  214
+      param[:primary_key_proc].call.should == :_id
  215
+      param[:model_proc].call.should == MongoNote
  216
+    end
  217
+
  218
+    it "should raise error when embeds_* is used without accepts_nested_attributes_for" do
  219
+      class MongoEmbedsOne
  220
+        include Mongoid::Document
  221
+        embeds_one :mongo_embedded
  222
+      end
  223
+
  224
+      class MongoEmbedsMany
  225
+        include Mongoid::Document
  226
+        embeds_many :mongo_embeddeds
  227
+      end
  228
+
  229
+      class MongoEmbedded
  230
+        include Mongoid::Document
  231
+        embedded_in :mongo_embeds_one
  232
+        embedded_in :mongo_embeds_many
  233
+      end
  234
+
  235
+      lambda{ RailsAdmin::AbstractModel.new(MongoEmbedsOne).associations }.should raise_error(RuntimeError,
  236
+        "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"
  237
+      )
  238
+      lambda{ RailsAdmin::AbstractModel.new(MongoEmbedsMany).associations }.should raise_error(RuntimeError,
  239
+        "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"
  240
+      )
  241
+     end
169 242
   end
170 243
 
171 244
   describe "#properties" do

0 notes on commit 78e1925

Please sign in to comment.
Something went wrong with that request. Please try again.