summaryrefslogtreecommitdiff
path: root/app/controllers/concerns/labels_as_hash.rb
blob: 601d3bf50eb8adc65e7c07b6c86bb0c4aec7d1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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])

    if target.respond_to?(:labels)
      already_set_labels = available_labels & target.labels
      if already_set_labels.present?
        titles = already_set_labels.map(&:title)
        label_hashes.each do |hash|
          hash[:set] = true if titles.include?(hash['title'])
        end
      end
    end

    label_hashes
  end
end