Move group labels AR code into finder

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2018-09-21 16:06:58 +03:00
parent e18d8d590b
commit 9176ea999a
No known key found for this signature in database
GPG key ID: 627C5F589F467F17
3 changed files with 51 additions and 4 deletions

View file

@ -10,10 +10,7 @@ class Groups::LabelsController < Groups::ApplicationController
def index def index
respond_to do |format| respond_to do |format|
format.html do format.html do
@labels = @group.labels @labels = GroupLabelsFinder.new(@group, params.merge(sort: sort)).execute
.optionally_search(params[:search])
.order_by(sort)
.page(params[:page])
end end
format.json do format.json do
render json: LabelSerializer.new.represent_appearance(available_labels) render json: LabelSerializer.new.represent_appearance(available_labels)

View file

@ -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

View file

@ -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