summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-07-14 10:54:14 -0700
committerDouwe Maan <douwe@selenight.nl>2017-07-14 10:54:14 -0700
commitf5e4a02a7151d9a2930a332cbf2bbce5ddbbd17e (patch)
tree239a94391946cadf8689875de51f52c598324d71
parent04860fc18ba8843c423a5cded2b5feb626d6ffe3 (diff)
downloadgitlab-ce-revert-c42e481f.tar.gz
Get DropDefaultScopeOnFinders to work for association collectionsrevert-c42e481f
-rw-r--r--app/models/concerns/sortable.rb12
-rw-r--r--spec/models/concerns/sortable_spec.rb35
2 files changed, 33 insertions, 14 deletions
diff --git a/app/models/concerns/sortable.rb b/app/models/concerns/sortable.rb
index fdacfa5a194..00e72d40116 100644
--- a/app/models/concerns/sortable.rb
+++ b/app/models/concerns/sortable.rb
@@ -38,9 +38,15 @@ module Sortable
scope :order_name_asc, -> { reorder(name: :asc) }
scope :order_name_desc, -> { reorder(name: :desc) }
- # All queries (relations) on this model are instances of this `relation_klass`.
- relation_klass = relation_delegate_class(ActiveRecord::Relation)
- relation_klass.prepend DropDefaultScopeOnFinders
+ # All queries (relations) on this model are instances of one of these classes.
+ [
+ ActiveRecord::Relation,
+ ActiveRecord::Associations::CollectionProxy,
+ ActiveRecord::AssociationRelation
+ ].each do |klass|
+ relation_klass = relation_delegate_class(klass)
+ relation_klass.prepend DropDefaultScopeOnFinders
+ end
end
module ClassMethods
diff --git a/spec/models/concerns/sortable_spec.rb b/spec/models/concerns/sortable_spec.rb
index d1e17c4f684..9dc56f2b311 100644
--- a/spec/models/concerns/sortable_spec.rb
+++ b/spec/models/concerns/sortable_spec.rb
@@ -1,21 +1,34 @@
require 'spec_helper'
describe Sortable do
- let(:relation) { Issue.all }
+ shared_examples 'sorts when appropriate' do
+ describe '#where' do
+ it 'orders by id, descending' do
+ order_node = relation.where(iid: 1).order_values.first
+ expect(order_node).to be_a(Arel::Nodes::Descending)
+ expect(order_node.expr.name).to eq(:id)
+ end
+ end
+
+ describe '#find_by' do
+ it 'does not order' do
+ expect(relation).to receive(:unscope).with(:order).and_call_original
- describe '#where' do
- it 'orders by id, descending' do
- order_node = relation.where(iid: 1).order_values.first
- expect(order_node).to be_a(Arel::Nodes::Descending)
- expect(order_node.expr.name).to eq(:id)
+ relation.find_by(iid: 1)
+ end
end
end
- describe '#find_by' do
- it 'does not order' do
- expect(relation).to receive(:unscope).with(:order).and_call_original
+ describe 'on the root relation' do
+ let(:relation) { Issue.all }
- relation.find_by(iid: 1)
- end
+ it_behaves_like 'sorts when appropriate'
+ end
+
+ describe 'on an association collection' do
+ let(:project) { create(:project) }
+ let(:relation) { project.issues.all }
+
+ it_behaves_like 'sorts when appropriate'
end
end