Class: Labels::AvailableLabelsService
- Inherits:
-
Object
- Object
- Labels::AvailableLabelsService
- Defined in:
- app/services/labels/available_labels_service.rb
Instance Attribute Summary collapse
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #available_labels ⇒ Object
- #filter_labels_ids_in_param(key) ⇒ Object
- #filter_locked_label_ids(ids) ⇒ Object
- #find_or_create_by_titles(key = :labels, find_only: false) ⇒ Object
-
#initialize(current_user, parent, params) ⇒ AvailableLabelsService
constructor
A new instance of AvailableLabelsService.
Constructor Details
#initialize(current_user, parent, params) ⇒ AvailableLabelsService
Returns a new instance of AvailableLabelsService.
6 7 8 9 10 |
# File 'app/services/labels/available_labels_service.rb', line 6 def initialize(current_user, parent, params) @current_user = current_user @parent = parent @params = params end |
Instance Attribute Details
#current_user ⇒ Object (readonly)
Returns the value of attribute current_user.
4 5 6 |
# File 'app/services/labels/available_labels_service.rb', line 4 def current_user @current_user end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'app/services/labels/available_labels_service.rb', line 4 def params @params end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
4 5 6 |
# File 'app/services/labels/available_labels_service.rb', line 4 def parent @parent end |
Instance Method Details
#available_labels ⇒ Object
47 48 49 |
# File 'app/services/labels/available_labels_service.rb', line 47 def available_labels @available_labels ||= LabelsFinder.new(current_user, finder_params).execute end |
#filter_labels_ids_in_param(key) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'app/services/labels/available_labels_service.rb', line 33 def filter_labels_ids_in_param(key) ids = Array.wrap(params[key]) return [] if ids.empty? # rubocop:disable CodeReuse/ActiveRecord existing_ids = available_labels.id_in(ids).pluck(:id) # rubocop:enable CodeReuse/ActiveRecord ids.map(&:to_i) & existing_ids end |
#filter_locked_label_ids(ids) ⇒ Object
43 44 45 |
# File 'app/services/labels/available_labels_service.rb', line 43 def filter_locked_label_ids(ids) available_labels.with_lock_on_merge.id_in(ids).pluck(:id) # rubocop:disable CodeReuse/ActiveRecord end |
#find_or_create_by_titles(key = :labels, find_only: false) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/services/labels/available_labels_service.rb', line 12 def find_or_create_by_titles(key = :labels, find_only: false) labels = params.delete(key) return [] unless labels labels = labels.split(',').map(&:strip) if labels.is_a?(String) existing_labels = LabelsFinder.new(current_user, finder_params(labels)).execute.index_by(&:title) labels.map do |label_name| label = Labels::FindOrCreateService.new( current_user, parent, include_ancestor_groups: true, title: label_name, existing_labels_by_title: existing_labels ).execute(find_only: find_only) label end.compact end |