From 4a42804a7d4e3e17f28d7a08a03f7d5d8d1eab95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 21 Aug 2021 22:56:02 +0200 Subject: [PATCH] Extract calculate winners button to a component This way we remove some duplication. --- ...alculate_winners_button_component.html.erb | 13 ++++++++ .../calculate_winners_button_component.rb | 30 +++++++++++++++++++ .../admin/budgets/form_component.html.erb | 7 +---- .../admin/budgets/form_component.rb | 4 --- app/helpers/budgets_helper.rb | 12 -------- .../budget_investments/_investments.html.erb | 14 +-------- 6 files changed, 45 insertions(+), 35 deletions(-) create mode 100644 app/components/admin/budgets/calculate_winners_button_component.html.erb create mode 100644 app/components/admin/budgets/calculate_winners_button_component.rb diff --git a/app/components/admin/budgets/calculate_winners_button_component.html.erb b/app/components/admin/budgets/calculate_winners_button_component.html.erb new file mode 100644 index 000000000..ea7fdbf58 --- /dev/null +++ b/app/components/admin/budgets/calculate_winners_button_component.html.erb @@ -0,0 +1,13 @@ +<% if display_button? %> + <%= link_to text, + calculate_winners_admin_budget_path(budget), + method: :put, + class: html_class %> +<% elsif from_investments %> + + <%= t("admin.budgets.winners.calculate") %> + +
+ <%= t("admin.budget_investments.index.cannot_calculate_winners") %> +
+<% end %> diff --git a/app/components/admin/budgets/calculate_winners_button_component.rb b/app/components/admin/budgets/calculate_winners_button_component.rb new file mode 100644 index 000000000..92eb9a8e6 --- /dev/null +++ b/app/components/admin/budgets/calculate_winners_button_component.rb @@ -0,0 +1,30 @@ +class Admin::Budgets::CalculateWinnersButtonComponent < ApplicationComponent + attr_reader :budget, :from_investments + + def initialize(budget, from_investments: false) + @budget = budget + @from_investments = from_investments + end + + private + + def display_button? + budget.balloting_or_later? + end + + def text + if budget.investments.winners.empty? + t("admin.budgets.winners.calculate") + else + t("admin.budgets.winners.recalculate") + end + end + + def html_class + if from_investments + "button hollow float-right clear" + else + "button hollow" + end + end +end diff --git a/app/components/admin/budgets/form_component.html.erb b/app/components/admin/budgets/form_component.html.erb index ae286844e..6fa6ecec1 100644 --- a/app/components/admin/budgets/form_component.html.erb +++ b/app/components/admin/budgets/form_component.html.erb @@ -86,12 +86,7 @@
- <% if display_calculate_winners_button?(budget) %> - <%= link_to calculate_winner_button_text(budget), - calculate_winners_admin_budget_path(budget), - method: :put, - class: "button hollow" %> - <% end %> + <%= render Admin::Budgets::CalculateWinnersButtonComponent.new(budget) %> <% if budget.has_winning_investments? %> <%= link_to t("budgets.show.see_results"), diff --git a/app/components/admin/budgets/form_component.rb b/app/components/admin/budgets/form_component.rb index 3d57663f0..d57457e7a 100644 --- a/app/components/admin/budgets/form_component.rb +++ b/app/components/admin/budgets/form_component.rb @@ -5,10 +5,6 @@ class Admin::Budgets::FormComponent < ApplicationComponent attr_reader :budget, :wizard alias_method :wizard?, :wizard - delegate :display_calculate_winners_button?, - :calculate_winner_button_text, - :calculate_winners_admin_budget_path, - to: :helpers def initialize(budget, wizard: false) @budget = budget diff --git a/app/helpers/budgets_helper.rb b/app/helpers/budgets_helper.rb index 43afd7ed2..c472a0f35 100644 --- a/app/helpers/budgets_helper.rb +++ b/app/helpers/budgets_helper.rb @@ -33,18 +33,6 @@ module BudgetsHelper budget.published? || current_user&.administrator? end - def display_calculate_winners_button?(budget) - budget.balloting_or_later? - end - - def calculate_winner_button_text(budget) - if budget.investments.winners.empty? - t("admin.budgets.winners.calculate") - else - t("admin.budgets.winners.recalculate") - end - end - def budget_subnav_items_for(budget) { results: t("budgets.results.link"), diff --git a/app/views/admin/budget_investments/_investments.html.erb b/app/views/admin/budget_investments/_investments.html.erb index 890448513..15bbdc216 100644 --- a/app/views/admin/budget_investments/_investments.html.erb +++ b/app/views/admin/budget_investments/_investments.html.erb @@ -3,19 +3,7 @@ class: "float-right small clear" %> <% if params[:advanced_filters].include?("winners") %> - <% if display_calculate_winners_button?(@budget) %> - <%= link_to calculate_winner_button_text(@budget), - calculate_winners_admin_budget_path(@budget), - method: :put, - class: "button hollow float-right clear" %> - <% else %> - - <%= t("admin.budgets.winners.calculate") %> - -
- <%= t("admin.budget_investments.index.cannot_calculate_winners") %> -
- <% end %> + <%= render Admin::Budgets::CalculateWinnersButtonComponent.new(@budget, from_investments: true) %> <% end %> <% if @investments.any? %>