2018-09-28 09:47:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module LabelsAsHash
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def labels_as_hash(target = nil, params = {})
|
|
|
|
available_labels = LabelsFinder.new(
|
|
|
|
current_user,
|
|
|
|
params
|
|
|
|
).execute
|
|
|
|
|
|
|
|
label_hashes = available_labels.as_json(only: [:title, :color])
|
|
|
|
|
2021-03-18 17:09:00 -04:00
|
|
|
if target.respond_to?(:labels)
|
2018-09-28 09:47:58 -04:00
|
|
|
already_set_labels = available_labels & target.labels
|
|
|
|
if already_set_labels.present?
|
|
|
|
titles = already_set_labels.map(&:title)
|
|
|
|
label_hashes.each do |hash|
|
|
|
|
if titles.include?(hash['title'])
|
|
|
|
hash[:set] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
label_hashes
|
|
|
|
end
|
|
|
|
end
|