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.
This commit is contained in:
@@ -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 %>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user