Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some tests for Mongoid adapter

  • Loading branch information...
commit a7ebbfa5092ae1a96be5b894ee27c0e2c18c7b63 1 parent c773783
@mshibuya authored
View
18 lib/rails_admin/abstract_model.rb
@@ -20,6 +20,24 @@ def new(m)
rescue LoadError, NameError
nil
end
+
+ @@polymorphic_parents = {}
+
+ def polymorphic_parents(adapter, name)
+ @@polymorphic_parents[adapter.to_sym] ||= {}.tap do |hash|
+ all(adapter).each do |am|
+ am.associations.select{|r| r[:as] }.each do |association|
+ (hash[association[:as].to_sym] ||= []) << am.model
+ end
+ end
+ end
+ @@polymorphic_parents[adapter.to_sym][name.to_sym]
+ end
+
+ # For testing
+ def reset_polymorphic_parents
+ @@polymorphic_parents = {}
+ end
end
def initialize(m)
View
17 lib/rails_admin/adapters/active_record.rb
@@ -137,6 +137,8 @@ def filter_conditions(filters, fields = config.list.fields.select(&:filterable?)
[statements.join(' AND '), *values]
end
+ private
+
def build_statement(column, type, value, operator)
# this operator/value has been discarded (but kept in the dom to override the one stored in the various links of the page)
return if operator == '_discard' || value == '_discard'
@@ -205,24 +207,11 @@ def build_statement(column, type, value, operator)
end
end
- @@polymorphic_parents = nil
-
- def self.polymorphic_parents(name)
- @@polymorphic_parents ||= {}.tap do |hash|
- RailsAdmin::AbstractModel.all(:active_record).each do |am|
- am.model.reflect_on_all_associations.select{|r| r.options[:as] }.each do |reflection|
- (hash[reflection.options[:as].to_sym] ||= []) << am.model
- end
- end
- end
- @@polymorphic_parents[name.to_sym]
- end
-
def association_parent_model_lookup(association)
case association.macro
when :belongs_to
if association.options[:polymorphic]
- RailsAdmin::Adapters::ActiveRecord.polymorphic_parents(association.name) || []
+ RailsAdmin::AbstractModel.polymorphic_parents(:active_record, association.name) || []
else
association.klass
end
View
12 lib/rails_admin/adapters/mongoid.rb
@@ -158,6 +158,8 @@ def get_conditions_hash(model_config, query, filters)
conditions.any? ? { :conditions => conditions } : {}
end
+ private
+
def build_statement(column, type, value, operator)
# remove table_name
table_prefix = "#{table_name}."
@@ -233,12 +235,14 @@ def build_statement(column, type, value, operator)
end
end
- private
-
def association_parent_model_lookup(association)
case association.macro
when :referenced_in
- association.klass
+ if association.polymorphic?
+ RailsAdmin::AbstractModel.polymorphic_parents(:mongoid, association.name) || []
+ else
+ association.klass
+ end
when :references_one, :references_many, :references_and_referenced_in_many
association.inverse_klass
else
@@ -248,7 +252,7 @@ def association_parent_model_lookup(association)
def association_foreign_type_lookup(association)
if association.polymorphic?
- association.type.try(:to_sym) || :"#{association.name}_type"
+ association.inverse_type.try(:to_sym) || :"#{association.name}_type"
end
end
View
2  spec/spec_helper.rb
@@ -70,4 +70,6 @@ def password_digest(password)
config.after(:each) do
Warden.test_reset!
end
+
+ config.seed = ENV['SEED'] if ENV['SEED']
end
View
82 spec/unit/adapters/active_record_spec.rb
@@ -5,6 +5,7 @@
describe RailsAdmin::Adapters::ActiveRecord do
before :all do
+ RailsAdmin::AbstractModel.reset_polymorphic_parents
class ARBlog < ActiveRecord::Base
has_many :a_r_posts
@@ -41,6 +42,10 @@ class ARComment < ActiveRecord::Base
@comment = RailsAdmin::AbstractModel.new(ARComment)
end
+ after :all do
+ RailsAdmin::AbstractModel.reset_polymorphic_parents
+ end
+
describe '#associations' do
it 'lists associations' do
@post.associations.map{|a|a[:name].to_s}.sort.should == ['a_r_blog', 'a_r_categories', 'a_r_comments']
@@ -49,5 +54,82 @@ class ARComment < ActiveRecord::Base
it 'list associations types in supported [:belongs_to, :has_and_belongs_to_many, :has_many, :has_one]' do
(@post.associations + @blog.associations + @user.associations).map{|a|a[:type]}.uniq.map(&:to_s).sort.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] == :a_r_blog}.first
+ param.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:a_r_blog,
+ :pretty_name=>"A r blog",
+ :type=>:belongs_to,
+ :parent_key=>[:id],
+ :child_key=>:a_r_blog_id,
+ :foreign_type=>nil,
+ :as=>nil,
+ :polymorphic=>nil,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == ARPost
+ param[:parent_model_proc].call.should == ARBlog
+ end
+
+ it "has correct parameter of has_many association" do
+ param = @blog.associations.select{|a| a[:name] == :a_r_posts}.first
+ param.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:a_r_posts,
+ :pretty_name=>"A r posts",
+ :type=>:has_many,
+ :parent_key=>[:id],
+ :child_key=>:ar_blog_id,
+ :foreign_type=>nil,
+ :as=>nil,
+ :polymorphic=>nil,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == ARPost
+ param[:parent_model_proc].call.should == ARBlog
+ end
+
+ it "has correct parameter of has_and_belongs_to_many association" do
+ param = @post.associations.select{|a| a[:name] == :a_r_categories}.first
+ param.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:a_r_categories,
+ :pretty_name=>"A r categories",
+ :type=>:has_and_belongs_to_many,
+ :parent_key=>[:id],
+ :child_key=>:ar_post_id,
+ :foreign_type=>nil,
+ :as=>nil,
+ :polymorphic=>nil,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == ARCategory
+ param[:parent_model_proc].call.should == ARPost
+ end
+
+ it "has correct parameter of polymorphic belongs_to association" do
+ RailsAdmin::Config.stub!(:models_pool).and_return(["ARBlog", "ARPost", "ARCategory", "ARUser", "ARProfile", "ARComment"])
+ param = @comment.associations.select{|a| a[:name] == :commentable}.first
+ param.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:commentable,
+ :pretty_name=>"Commentable",
+ :type=>:belongs_to,
+ :parent_key=>[:id],
+ :child_key=>:commentable_id,
+ :foreign_type=>:commentable_type,
+ :as=>nil,
+ :polymorphic=>true,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == ARComment
+ param[:parent_model_proc].call.should == [ARBlog, ARPost]
+ end
end
end
View
140 spec/unit/adapters/mongoid_spec.rb
@@ -0,0 +1,140 @@
+require 'spec_helper'
+require 'rails_admin/adapters/mongoid'
+
+describe RailsAdmin::Adapters::Mongoid do
+
+ before :all do
+ RailsAdmin::AbstractModel.reset_polymorphic_parents
+
+ class MongoBlog
+ include Mongoid::Document
+ references_many :mongo_posts
+ references_many :mongo_comments, :as => :commentable
+ end
+
+ class MongoPost
+ include Mongoid::Document
+ referenced_in :mongo_blog
+ has_and_belongs_to_many :mongo_categories
+ references_many :mongo_comments, :as => :commentable
+ end
+
+ class MongoCategory
+ include Mongoid::Document
+ has_and_belongs_to_many :mongo_posts
+ end
+
+ class MongoUser
+ include Mongoid::Document
+ references_one :mongo_profile
+ end
+
+ class MongoProfile
+ include Mongoid::Document
+ referenced_in :mongo_user
+ end
+
+ class MongoComment
+ include Mongoid::Document
+ referenced_in :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)
+ end
+
+ after :all do
+ RailsAdmin::AbstractModel.reset_polymorphic_parents
+ end
+
+ describe '#associations' do
+ it 'lists associations' do
+ @post.associations.map{|a|a[:name]}.should == [:mongo_blog, :mongo_categories, :mongo_comments]
+ 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]}.uniq.sort.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.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:mongo_blog,
+ :pretty_name=>"Mongo blog",
+ :type=>:belongs_to,
+ :parent_key=>[:_id],
+ :child_key=>:mongo_blog_id,
+ :foreign_type=>nil,
+ :as=>nil,
+ :polymorphic=>false,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == MongoPost
+ param[:parent_model_proc].call.should == MongoBlog
+ end
+
+ it "has correct parameter of has_many association" do
+ param = @blog.associations.select{|a| a[:name] == :mongo_posts}.first
+ param.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:mongo_posts,
+ :pretty_name=>"Mongo posts",
+ :type=>:has_many,
+ :parent_key=>[:_id],
+ :child_key=>:mongo_blog_id,
+ :foreign_type=>nil,
+ :as=>nil,
+ :polymorphic=>false,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == MongoPost
+ param[:parent_model_proc].call.should == MongoBlog
+ end
+
+ it "has correct parameter of has_and_belongs_to_many association" do
+ param = @post.associations.select{|a| a[:name] == :mongo_categories}.first
+ param.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:mongo_categories,
+ :pretty_name=>"Mongo categories",
+ :type=>:has_and_belongs_to_many,
+ :parent_key=>[:_id],
+ :child_key=>:mongo_category_ids,
+ :foreign_type=>nil,
+ :as=>nil,
+ :polymorphic=>false,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == MongoCategory
+ param[:parent_model_proc].call.should == MongoPost
+ end
+
+ 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.reject{|k, v| [:child_model_proc, :parent_model_proc].include? k }.should == {
+ :name=>:commentable,
+ :pretty_name=>"Commentable",
+ :type=>:belongs_to,
+ :parent_key=>[:_id],
+ :child_key=>:commentable_id,
+ :foreign_type=>:commentable_type,
+ :as=>nil,
+ :polymorphic=>true,
+ :inverse_of=>nil,
+ :read_only=>nil,
+ :nested_form=>nil
+ }
+ param[:child_model_proc].call.should == MongoComment
+ param[:parent_model_proc].call.should == [MongoBlog, MongoPost]
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.