module Tr3llo::Command::Card::AddLabel

Public Instance Methods

execute(key, board_id) click to toggle source
# File lib/3llo/command/card/add_label.rb, line 7
def execute(key, board_id)
  card_id = Entities.parse_id(:card, key)
  assert_card_id!(card_id, key)

  card = API::Card.find(card_id)

  interface = Application.fetch_interface!()

  interface.print_frame do
    label_ids = select_labels(interface, card, board_id)

    add_labels(card_id, label_ids)
    interface.puts("Chosen labels have been added to the card.")
  end
end

Private Instance Methods

add_labels(card_id, label_ids) click to toggle source
# File lib/3llo/command/card/add_label.rb, line 29
def add_labels(card_id, label_ids)
  API::Card.add_labels(card_id, label_ids)
end
assert_card_id!(card_id, key) click to toggle source
# File lib/3llo/command/card/add_label.rb, line 25
def assert_card_id!(card_id, key)
  raise InvalidArgumentError.new("#{key.inspect} is not a valid card key") unless card_id
end
select_labels(interface, card, board_id) click to toggle source
# File lib/3llo/command/card/add_label.rb, line 33
def select_labels(interface, card, board_id)
  label_options =
    API::Label.find_all_by_board(board_id)
      .map { |label| [label.name, label.id] }
      .to_h()

  preselected_label_ids =
    card.labels.flat_map do |label|
      index = label_options.find_index { |_label_name, label_id| label_id == label.id }

      index ? [index + 1] : []
    end

  interface.input.multi_select(
    "Choose the labels to add to this card:",
    label_options,
    default: preselected_label_ids,
    enum: "."
  )
end