Commit 43d2d32d authored by Natalia Tepluhina's avatar Natalia Tepluhina Committed by Bob Van Landuyt

Created a selector for a given design

- added selection by id and ids
parent 49b37da3
...@@ -2,8 +2,11 @@ ...@@ -2,8 +2,11 @@
module DesignManagement module DesignManagement
class DesignsFinder class DesignsFinder
attr_reader :issue, :current_user, :params include Gitlab::Allowable
# Params:
# ids: integer[]
# visible_at_version: ?version
def initialize(issue, current_user, params = {}) def initialize(issue, current_user, params = {})
@issue = issue @issue = issue
@current_user = current_user @current_user = current_user
...@@ -11,18 +14,33 @@ module DesignManagement ...@@ -11,18 +14,33 @@ module DesignManagement
end end
def execute def execute
unless Ability.allowed?(current_user, :read_design, issue) items = init_collection
return ::DesignManagement::Design.none
end items = by_visible_at_version(items)
items = by_ids(items)
by_visible_at_version(issue.designs) items
end end
private private
attr_reader :issue, :current_user, :params
def init_collection
return ::DesignManagement::Design.none unless can?(current_user, :read_design, issue)
issue.designs
end
# Returns all designs that existed at a particular design version # Returns all designs that existed at a particular design version
def by_visible_at_version(items) def by_visible_at_version(items)
items.visible_at_version(params[:visible_at_version]) items.visible_at_version(params[:visible_at_version])
end end
def by_ids(items)
return items unless params[:ids].present?
items.id_in(params[:ids])
end
end end
end end
...@@ -3,19 +3,31 @@ ...@@ -3,19 +3,31 @@
module Resolvers module Resolvers
module DesignManagement module DesignManagement
class DesignResolver < BaseResolver class DesignResolver < BaseResolver
argument :ids, [GraphQL::ID_TYPE], required: false, description: 'The list of IDs of designs.'
argument :at_version, argument :at_version,
GraphQL::ID_TYPE, GraphQL::ID_TYPE,
required: false, required: false,
description: 'Filters designs to only those that existed at the version. ' \ description: 'Filters designs to only those that existed at the version. ' \
'If argument is omitted or nil then all designs will reflect the latest version.' 'If argument is omitted or nil then all designs will reflect the latest version.'
def resolve(at_version: nil) def resolve(**args)
version = at_version ? GitlabSchema.object_from_id(at_version)&.sync : nil find_designs(args)
end
def version(args)
args[:at_version] ? GitlabSchema.object_from_id(args[:at_version])&.sync : nil
end
def design_ids(args)
args[:ids] ? args[:ids].map { |id| GlobalID.parse(id).model_id } : nil
end
def find_designs(args)
::DesignManagement::DesignsFinder.new( ::DesignManagement::DesignsFinder.new(
object.issue, object.issue,
context[:current_user], context[:current_user],
visible_at_version: version ids: design_ids(args),
visible_at_version: version(args)
).execute ).execute
end end
end end
......
...@@ -52,6 +52,26 @@ describe DesignManagement::DesignsFinder do ...@@ -52,6 +52,26 @@ describe DesignManagement::DesignsFinder do
it { is_expected.to eq([design1]) } it { is_expected.to eq([design1]) }
end end
context 'when argument is the ids of designs' do
let(:params) { { ids: [design1.id] } }
it { is_expected.to eq([design1]) }
end
context 'when arguments are version and id' do
context 'when id is absent at version' do
let(:params) { { visible_at_version: first_version, ids: [design2.id] } }
it { is_expected.to eq([]) }
end
context 'when id is present at version' do
let(:params) { { visible_at_version: second_version, ids: [design2.id] } }
it { is_expected.to eq([design2]) }
end
end
context 'when argument is the second version' do context 'when argument is the second version' do
let(:params) { { visible_at_version: second_version } } let(:params) { { visible_at_version: second_version } }
......
# frozen_string_literal: true
require "spec_helper"
describe Resolvers::DesignManagement::DesignResolver do
include GraphqlHelpers
include DesignManagementTestHelpers
before do
enable_design_management
end
describe "#resolve" do
set(:issue) { create(:issue) }
set(:project) { issue.project }
set(:first_version) { create(:design_version) }
set(:first_design) { create(:design, issue: issue, versions: [first_version]) }
set(:current_user) { create(:user) }
before do
project.add_developer(current_user)
end
context "when the user cannot see designs" do
it "returns nothing" do
expect(resolve_designs(issue.design_collection, {}, current_user: create(:user))).to be_empty
end
end
context "for a design collection" do
it "returns designs" do
expect(resolve_designs(issue.design_collection, {}, current_user: current_user)).to eq([first_design])
end
it "returns all designs" do
second_version = create(:design_version)
second_design = create(:design, issue: issue, versions: [second_version])
expect(resolve_designs(issue.design_collection, {}, current_user: current_user)).to eq([first_design, second_design])
end
end
end
def resolve_designs(obj, args = {}, context = { current_user: current_user })
resolve(described_class, obj: obj, args: args, ctx: context)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment