2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-17 22:11:33 -04:00
|
|
|
module Labels
|
2016-10-19 09:53:31 -04:00
|
|
|
class FindOrCreateService
|
2018-02-26 11:23:19 -05:00
|
|
|
def initialize(current_user, parent, params = {})
|
2016-10-17 22:11:33 -04:00
|
|
|
@current_user = current_user
|
2018-02-26 11:23:19 -05:00
|
|
|
@parent = parent
|
|
|
|
@available_labels = params.delete(:available_labels)
|
2017-03-29 07:45:15 -04:00
|
|
|
@params = params.dup.with_indifferent_access
|
2016-10-17 22:11:33 -04:00
|
|
|
end
|
|
|
|
|
2019-08-30 03:51:45 -04:00
|
|
|
def execute(skip_authorization: false, find_only: false)
|
2016-10-28 05:31:45 -04:00
|
|
|
@skip_authorization = skip_authorization
|
2019-08-30 03:51:45 -04:00
|
|
|
find_or_create_label(find_only: find_only)
|
2016-10-17 22:11:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-02-26 11:23:19 -05:00
|
|
|
attr_reader :current_user, :parent, :params, :skip_authorization
|
2016-10-17 22:11:33 -04:00
|
|
|
|
|
|
|
def available_labels
|
2016-10-28 05:31:45 -04:00
|
|
|
@available_labels ||= LabelsFinder.new(
|
|
|
|
current_user,
|
2018-02-26 11:23:19 -05:00
|
|
|
"#{parent_type}_id".to_sym => parent.id,
|
2018-07-04 05:52:58 -04:00
|
|
|
include_ancestor_groups: include_ancestor_groups?,
|
2018-02-26 11:23:19 -05:00
|
|
|
only_group_labels: parent_is_group?
|
2016-10-28 05:31:45 -04:00
|
|
|
).execute(skip_authorization: skip_authorization)
|
2016-10-17 22:11:33 -04:00
|
|
|
end
|
|
|
|
|
2016-11-10 05:23:44 -05:00
|
|
|
# Only creates the label if current_user can do so, if the label does not exist
|
|
|
|
# and the user can not create the label, nil is returned
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-08-30 03:51:45 -04:00
|
|
|
def find_or_create_label(find_only: false)
|
2016-10-17 22:11:33 -04:00
|
|
|
new_label = available_labels.find_by(title: title)
|
2016-11-10 05:23:44 -05:00
|
|
|
|
2019-08-30 03:51:45 -04:00
|
|
|
return new_label if find_only
|
|
|
|
|
2018-02-26 11:23:19 -05:00
|
|
|
if new_label.nil? && (skip_authorization || Ability.allowed?(current_user, :admin_label, parent))
|
2018-07-04 05:52:58 -04:00
|
|
|
create_params = params.except(:include_ancestor_groups)
|
|
|
|
new_label = Labels::CreateService.new(create_params).execute(parent_type.to_sym => parent)
|
2016-11-10 05:23:44 -05:00
|
|
|
end
|
2016-10-17 22:11:33 -04:00
|
|
|
|
|
|
|
new_label
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-17 22:11:33 -04:00
|
|
|
|
|
|
|
def title
|
|
|
|
params[:title] || params[:name]
|
|
|
|
end
|
2018-02-26 11:23:19 -05:00
|
|
|
|
|
|
|
def parent_type
|
|
|
|
parent.model_name.param_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent_is_group?
|
|
|
|
parent_type == "group"
|
|
|
|
end
|
2018-07-04 05:52:58 -04:00
|
|
|
|
|
|
|
def include_ancestor_groups?
|
|
|
|
params[:include_ancestor_groups] == true
|
|
|
|
end
|
2016-10-17 22:11:33 -04:00
|
|
|
end
|
|
|
|
end
|