Permalink
Browse files

merge latest master into mongoid branch, fix specs

  • Loading branch information...
1 parent 0b6ec58 commit 662cb546f972999b80778098b69a958555e51d73 @bbenezech bbenezech committed Mar 5, 2012
View
39 lib/rails_admin/adapters/mongoid.rb
@@ -58,10 +58,9 @@ def associations
:name => association.name.to_sym,
:pretty_name => association.name.to_s.tr('_', ' ').capitalize,
:type => association_type_lookup(association.macro),
- :parent_model_proc => Proc.new { association_parent_model_lookup(association) },
- :parent_key => association_parent_key_lookup(association),
- :child_model_proc => Proc.new { association_child_model_lookup(association) },
- :child_key => association_child_key_lookup(association),
+ :model_proc => Proc.new { association_model_proc_lookup(association) },
+ :primary_key_proc => Proc.new { association_primary_key_lookup(association) },
+ :foreign_key => association_foreign_key_lookup(association),
:foreign_type => association_foreign_type_lookup(association),
:as => association_as_lookup(association),
:polymorphic => association_polymorphic_lookup(association),
@@ -250,18 +249,11 @@ def build_statement(column, type, value, operator)
end
end
- def association_parent_model_lookup(association)
- case association.macro
- when :referenced_in
- 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
+ def association_model_proc_lookup(association)
+ if association.polymorphic?
+ RailsAdmin::AbstractModel.polymorphic_parents(:mongoid, association.name) || []
else
- raise "Unknown association type: #{association.macro.inspect}"
+ association.klass
end
end
@@ -279,26 +271,15 @@ def association_polymorphic_lookup(association)
association.polymorphic?
end
- def association_parent_key_lookup(association)
- [:_id]
+ def association_primary_key_lookup(association)
+ :_id # todo
end
def association_inverse_of_lookup(association)
association.inverse_of.try :to_sym
end
- def association_child_model_lookup(association)
- case association.macro
- when :referenced_in, :embedded_in
- association.inverse_klass
- when :references_one, :references_many, :references_and_referenced_in_many, :embeds_one, :embeds_many
- association.klass
- else
- raise "Unknown association type: #{association.macro.inspect}"
- end
- end
-
- def association_child_key_lookup(association)
+ def association_foreign_key_lookup(association)
association.foreign_key.to_sym rescue nil
end
View
2 lib/rails_admin/config/fields/factories/serialized.rb
@@ -8,7 +8,7 @@
# @see RailsAdmin::Config::Fields::Types::Password.column_names
# @see RailsAdmin::Config::Fields.register_factory
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
- if parent.abstract_model.model.serialized_attributes.keys.include?(properties[:name].to_s)
+ if parent.abstract_model.serialized_attributes.keys.include?(properties[:name].to_s)
fields << RailsAdmin::Config::Fields::Types::Serialized.new(parent, properties[:name], properties)
true
else
View
36 spec/unit/adapters/active_record_spec.rb
@@ -61,79 +61,75 @@ class ARComment < ActiveRecord::Base
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 == {
+ param.reject{|k, v| [:primary_key_proc, :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_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
+ param[:primary_key_proc].call.should == 'id'
+ param[: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 == {
+ param.reject{|k, v| [:primary_key_proc, :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_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
+ param[:primary_key_proc].call.should == 'id'
+ param[:model_proc].call.should == ARPost
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 == {
+ param.reject{|k, v| [:primary_key_proc, :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_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
+ param[:primary_key_proc].call.should == 'id'
+ param[:model_proc].call.should == ARCategory
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 == {
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:commentable,
:pretty_name=>"Commentable",
:type=>:belongs_to,
- :parent_key=>[:id],
- :child_key=>:commentable_id,
+ :foreign_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]
+ # param[:primary_key_proc].call.should == 'id' Should not be called for polymorphic relations. Todo, Handle this niver
+ param[:model_proc].call.should == [ARBlog, ARPost]
end
end
View
36 spec/unit/adapters/mongoid_spec.rb
@@ -67,79 +67,75 @@ class MongoComment
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 == {
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_blog,
:pretty_name=>"Mongo blog",
:type=>:belongs_to,
- :parent_key=>[:_id],
- :child_key=>:mongo_blog_id,
+ :foreign_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
+ param[:primary_key_proc].call.should == :_id
+ param[: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 == {
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_posts,
:pretty_name=>"Mongo posts",
:type=>:has_many,
- :parent_key=>[:_id],
- :child_key=>:mongo_blog_id,
+ :foreign_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
+ param[:primary_key_proc].call.should == :_id
+ param[:model_proc].call.should == MongoPost
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 == {
+ param.reject{|k, v| [:primary_key_proc, :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_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
+ param[:primary_key_proc].call.should == :_id
+ param[:model_proc].call.should == MongoCategory
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 == {
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:commentable,
:pretty_name=>"Commentable",
:type=>:belongs_to,
- :parent_key=>[:_id],
- :child_key=>:commentable_id,
+ :foreign_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]
+ param[:primary_key_proc].call.should == :_id
+ param[:model_proc].call.should == [MongoBlog, MongoPost]
end
end

0 comments on commit 662cb54

Please sign in to comment.