Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed the issue with foo_ids= assignment of references_many association

  • Loading branch information...
commit f196cfb6461756d50f94f62abc59c0054dc3cdd6 1 parent 82e65a6
@mshibuya authored
View
27 lib/rails_admin/adapters/mongoid/abstract_object.rb
@@ -3,6 +3,25 @@ module RailsAdmin
module Adapters
module Mongoid
class AbstractObject < RailsAdmin::Adapters::ActiveRecord::AbstractObject
+ def initialize(object)
+ super
+ object.associations.each do |name, association|
+ if association.macro == :references_many
+ instance_eval(<<EOS)
+ def #{name.to_s.singularize}_ids
+ #{name}.map{|item| item.id }
+ end
+
+ def #{name.to_s.singularize}_ids=(items)
+ self.#{name} = items.
+ map{|item_id| self.#{name}.klass.find(item_id) rescue nil }.
+ compact
+ end
+EOS
+ end
+ end
+ end
+
def attributes=(attributes)
object.send :attributes=, attributes
end
@@ -11,14 +30,6 @@ def destroy
object.destroy
object
end
-
- def method_missing(name, *args, &block)
- if(md = /(.+)_ids$/.match name.to_s)
- object.send(md[1].pluralize).map{|r| r.id}
- else
- super
- end
- end
end
end
end
View
8 spec/dummy_app/app/models/article.rb
@@ -0,0 +1,8 @@
+class Article
+ include Mongoid::Document
+
+ field :title, :type => String
+ field :body, :type => String
+
+ referenced_in :author
+end
View
6 spec/dummy_app/app/models/author.rb
@@ -0,0 +1,6 @@
+class Author
+ include Mongoid::Document
+
+ field :name, :type => String
+ references_many :articles
+end
View
5 spec/dummy_app/app/models/game.rb
@@ -1,5 +0,0 @@
-class Game
- include Mongoid::Document
-
- field :name, :type=>String
-end
View
8 spec/factories.rb
@@ -63,4 +63,12 @@
factory :hardball do
color('blue')
end
+
+ factory :article do
+ sequence(:title) { |n| "Article #{n}" }
+ end
+
+ factory :author do
+ sequence(:name) { |n| "Author #{n}" }
+ end
end
View
2  spec/spec_helper.rb
@@ -61,6 +61,8 @@ def password_digest(password)
Team.delete_all
User.delete_all
FieldTest.delete_all
+ Author.delete_all
+ Article.delete_all
login_as User.create(
:email => "username@example.com",
:password => "password"
View
30 spec/unit/adapters/mongoid/abstract_object_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+require 'rails_admin/adapters/mongoid/abstract_object'
+
+describe "Mongoid::AbstractObject" do
+ before(:each) do
+ @articles = FactoryGirl.create_list :article, 3
+ @author = RailsAdmin::Adapters::Mongoid::AbstractObject.new FactoryGirl.create :author
+ end
+
+ describe "references_many association" do
+ it "supports retrieval of ids through foo_ids" do
+ @author.article_ids.should == []
+ article = FactoryGirl.create :article, :author => @author
+ @author.article_ids.should == [article.id]
+ end
+
+ it "supports assignment of items through foo_ids=" do
+ @author.articles.should == []
+ @author.article_ids = @articles.map(&:id)
+ @author.reload
+ @author.articles.should == @articles
+ end
+
+ it "skips invalid id on assignment through foo_ids=" do
+ @author.article_ids = @articles.map{|item| item.id.to_s }.unshift('4f431021dcf2310db7000006')
+ @author.reload
+ @author.articles.should == @articles
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.