Files
nairobi/app/components/admin/budgets/actions_component.rb
Javi Martín 8b13daad95 Add and apply rules for multi-line hashes
For the HashAlignment rule, we're using the default `key` style (keys
are aligned and values aren't) instead of the `table` style (both keys
and values are aligned) because, even if we used both in the
application, we used the `key` style a lot more. Furthermore, the
`table` style looks strange in places where there are both very long and
very short keys and sometimes we weren't even consistent with the
`table` style, aligning some keys without aligning other keys.

Ideally we could align hashes to "either key or table", so developers
can decide whether keeping the symmetry of the code is worth it in a
case-per-case basis, but Rubocop doesn't allow this option.
2023-08-18 14:56:16 +02:00

97 lines
2.5 KiB
Ruby

class Admin::Budgets::ActionsComponent < ApplicationComponent
attr_reader :budget
def initialize(budget)
@budget = budget
end
private
def action(action_name, **options)
render Admin::ActionComponent.new(
action_name,
budget,
"aria-describedby": true,
**options
)
end
def actions
@actions ||= {
calculate_winners: {
hint: winners_hint,
html: winners_action
},
ballots: {
hint: ballots_hint,
html: ballots_action
},
destroy: {
hint: destroy_hint,
html: destroy_action
}
}.select { |_, button| button[:html].present? }
end
def winners_action
render Admin::Budgets::CalculateWinnersButtonComponent.new(budget)
end
def winners_hint
t("admin.budgets.actions.descriptions.calculate_winners", phase: t("budgets.phase.finished"))
end
def destroy_action
action(:destroy,
text: t("admin.budgets.edit.delete"),
method: :delete,
confirm: t("admin.budgets.actions.confirm.destroy"),
disabled: budget.investments.any? || budget.poll)
end
def destroy_hint
if budget.investments.any?
t("admin.budgets.destroy.unable_notice")
elsif budget.poll
t("admin.budgets.destroy.unable_notice_polls")
else
t("admin.budgets.actions.descriptions.destroy")
end
end
def ballots_action
if budget.published? && !budget.balloting_finished? && !budget.poll.present?
action(:ballots,
text: t("admin.budgets.actions.ballots"),
path: create_budget_poll_path,
method: :post,
confirm: t("admin.budgets.actions.confirm.ballots"),
disabled: !feature?("polls"))
end
end
def ballots_hint
if feature?("polls")
t("admin.budgets.actions.descriptions.ballots")
else
link = admin_settings_path(anchor: "tab-participation-processes")
t("admin.budgets.ballots.feature_disabled", link: link)
end
end
def create_budget_poll_path
balloting_phase = budget.phases.find_by(kind: "balloting")
admin_polls_path(poll: {
name: budget.name,
budget_id: budget.id,
starts_at: balloting_phase.starts_at,
ends_at: balloting_phase.ends_at
})
end
def descriptor_id(action_name)
"#{dom_id(budget, action_name)}_descriptor"
end
end