From 95f36ed52fb0c4385852ba7578b4fe9e19a8465d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 21 Aug 2021 00:44:08 +0200 Subject: [PATCH] Simplify code to toggle investment selection This way it'll be easier to change the link/button used to toggle the selection. Note that the conditions in the view seem to be different because we no longer include the `selected?` condition when rendering the link/button. However, an investment can only be selected if it's feasible and its valuation is finished, so writing something like this would have been redundant: ```ruby can?(:toggle_selection, investment) && (selected? || investment.feasible? && investment.valuation_finished?) ``` The reason why the previous code was using the `selected?` condition was to check whether to render the link/button to select or to deselect an investment. We're now doing that in the Ruby part of the component. --- .../toggle_selection_component.html.erb | 19 ++++------------- .../toggle_selection_component.rb | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/components/admin/budget_investments/toggle_selection_component.html.erb b/app/components/admin/budget_investments/toggle_selection_component.html.erb index 97d671e67..9d5d3c87e 100644 --- a/app/components/admin/budget_investments/toggle_selection_component.html.erb +++ b/app/components/admin/budget_investments/toggle_selection_component.html.erb @@ -1,16 +1,5 @@ -<% if investment.selected? %> - <%= link_to_if can?(:toggle_selection, investment), - t("admin.budget_investments.index.selected"), - path, - method: :patch, - remote: true, - class: "button small expanded" %> -<% elsif investment.feasible? && investment.valuation_finished? %> - <% if can?(:toggle_selection, investment) %> - <%= link_to t("admin.budget_investments.index.select"), - path, - method: :patch, - remote: true, - class: "button small hollow expanded" %> - <% end %> +<% if can?(:toggle_selection, investment) && investment.feasible? && investment.valuation_finished? %> + <%= link_to text, path, method: :patch, remote: true, class: html_class %> +<% elsif selected? %> + <%= selected_text %> <% end %> diff --git a/app/components/admin/budget_investments/toggle_selection_component.rb b/app/components/admin/budget_investments/toggle_selection_component.rb index 9dd870d86..64d662fd5 100644 --- a/app/components/admin/budget_investments/toggle_selection_component.rb +++ b/app/components/admin/budget_investments/toggle_selection_component.rb @@ -1,6 +1,7 @@ class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent attr_reader :investment use_helpers :can? + delegate :selected?, to: :investment def initialize(investment) @investment = investment @@ -8,6 +9,18 @@ class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent private + def text + if selected? + selected_text + else + t("admin.budget_investments.index.select") + end + end + + def selected_text + t("admin.budget_investments.index.selected") + end + def path toggle_selection_admin_budget_budget_investment_path( investment.budget, @@ -20,4 +33,12 @@ class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent page: params[:page] ) end + + def html_class + if selected? + "button small expanded" + else + "button small hollow expanded" + end + end end