summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2018-09-21 16:06:58 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2018-09-21 16:34:18 +0300
commit9176ea999a96580358be9bbcd42403879c5ae707 (patch)
tree33f9b50e0f85fbe11b79df8cf56ddfe8f8861ead
parente18d8d590b45daca41a1be39289fe29859504c09 (diff)
downloadgitlab-ce-dz-group-labels-finder.tar.gz
Move group labels AR code into finderdz-group-labels-finder
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--app/controllers/groups/labels_controller.rb5
-rw-r--r--app/finders/group_labels_finder.rb17
-rw-r--r--spec/finders/group_labels_finder_spec.rb33
3 files changed, 51 insertions, 4 deletions
diff --git a/app/controllers/groups/labels_controller.rb b/app/controllers/groups/labels_controller.rb
index 059cf160fa2..ae31313db64 100644
--- a/app/controllers/groups/labels_controller.rb
+++ b/app/controllers/groups/labels_controller.rb
@@ -10,10 +10,7 @@ class Groups::LabelsController < Groups::ApplicationController
def index
respond_to do |format|
format.html do
- @labels = @group.labels
- .optionally_search(params[:search])
- .order_by(sort)
- .page(params[:page])
+ @labels = GroupLabelsFinder.new(@group, params.merge(sort: sort)).execute
end
format.json do
render json: LabelSerializer.new.represent_appearance(available_labels)
diff --git a/app/finders/group_labels_finder.rb b/app/finders/group_labels_finder.rb
new file mode 100644
index 00000000000..903023033ed
--- /dev/null
+++ b/app/finders/group_labels_finder.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class GroupLabelsFinder
+ attr_reader :group, :params
+
+ def initialize(group, params = {})
+ @group = group
+ @params = params
+ end
+
+ def execute
+ group.labels
+ .optionally_search(params[:search])
+ .order_by(params[:sort])
+ .page(params[:page])
+ end
+end
diff --git a/spec/finders/group_labels_finder_spec.rb b/spec/finders/group_labels_finder_spec.rb
new file mode 100644
index 00000000000..ef68fc105e4
--- /dev/null
+++ b/spec/finders/group_labels_finder_spec.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe GroupLabelsFinder, '#execute' do
+ let!(:group) { create(:group) }
+ let!(:label1) { create(:group_label, title: 'Foo', description: 'Lorem ipsum', group: group) }
+ let!(:label2) { create(:group_label, title: 'Bar', description: 'Fusce consequat', group: group) }
+
+ it 'returns all group labels sorted by name if no params' do
+ result = described_class.new(group).execute
+
+ expect(result.to_a).to match_array([label2, label1])
+ end
+
+ it 'returns all group labels sorted by name desc' do
+ result = described_class.new(group, sort: 'name_desc').execute
+
+ expect(result.to_a).to match_array([label2, label1])
+ end
+
+ it 'returns group labels that march search' do
+ result = described_class.new(group, search: 'Foo').execute
+
+ expect(result.to_a).to match_array([label1])
+ end
+
+ it 'returns second page of labels' do
+ result = described_class.new(group, page: '2').execute
+
+ expect(result.to_a).to match_array([])
+ end
+end