Permalink
Browse files

adds ability to sort on belongs_to columns with Mongoid

  • Loading branch information...
1 parent 6b963bd commit ce60396426e86fba950c79a642bb0456317bc6d4 @bbenezech bbenezech committed Dec 26, 2012
@@ -107,6 +107,10 @@ def embedded?
false
end
+ def adapter_supports_joins?
+ true
+ end
+
private
def query_conditions(query, fields = config.list.fields.select(&:queryable?))
@@ -109,6 +109,10 @@ def object_id_from_string(str)
ObjectId.from_string(str)
end
+ def adapter_supports_joins?
+ false
+ end
+
private
def query_conditions(query, fields = config.list.fields.select(&:queryable?))
@@ -409,6 +413,7 @@ def perform_search_on_associated_collection(field_name, conditions)
def sort_by(options, scope)
return scope unless options[:sort]
+
field_name, collection_name = options[:sort].to_s.split('.').reverse
if collection_name && collection_name != table_name
# sorting by associated model column is not supported, so just ignore
@@ -12,7 +12,7 @@ class BelongsToAssociation < RailsAdmin::Config::Fields::Association
end
register_instance_option :sortable do
- @sortable ||= associated_model_config.abstract_model.properties.map{ |p| p[:name] }.include?(associated_model_config.object_label_method) ? associated_model_config.object_label_method : {self.abstract_model.table_name => self.method_name}
+ @sortable ||= abstract_model.adapter_supports_joins? && associated_model_config.abstract_model.properties.map{ |p| p[:name] }.include?(associated_model_config.object_label_method) ? associated_model_config.object_label_method : {self.abstract_model.table_name => self.method_name}
end
register_instance_option :searchable do
@@ -43,9 +43,18 @@
expect(controller.send(:get_sort_hash, RailsAdmin.config(Category))).to eq({:sort=>"categories.parent_category_id", :sort_reverse=>true})
end
- it "works with belongs_to associations with label method real column" do
- controller.params = { :sort => "team", :model_name =>"players" }
- expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq({:sort=>"teams.name", :sort_reverse=>true})
+ context "using mongoid, not supporting joins", :mongoid => true do
+ it "gives back the remote table with label name" do
+ controller.params = { :sort => "team", :model_name =>"players" }
+ expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq({:sort=>"players.team_id", :sort_reverse=>true})
+ end
+ end
+
+ context "using active_record, supporting joins", :active_record => true do
+ it "gives back the local column" do
+ controller.params = { :sort => "team", :model_name =>"players" }
+ expect(controller.send(:get_sort_hash, RailsAdmin.config(Player))).to eq({:sort=>"teams.name", :sort_reverse=>true})
+ end
end
end

0 comments on commit ce60396

Please sign in to comment.