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? %>
|
<% if can?(:toggle_selection, investment) && investment.feasible? && investment.valuation_finished? %>
|
||||||
<%= link_to_if can?(:toggle_selection, investment),
|
<%= link_to text, path, method: :patch, remote: true, class: html_class %>
|
||||||
t("admin.budget_investments.index.selected"),
|
<% elsif selected? %>
|
||||||
path,
|
<%= selected_text %>
|
||||||
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 %>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent
|
class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent
|
||||||
attr_reader :investment
|
attr_reader :investment
|
||||||
use_helpers :can?
|
use_helpers :can?
|
||||||
|
delegate :selected?, to: :investment
|
||||||
|
|
||||||
def initialize(investment)
|
def initialize(investment)
|
||||||
@investment = investment
|
@investment = investment
|
||||||
@@ -8,6 +9,18 @@ class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent
|
|||||||
|
|
||||||
private
|
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
|
def path
|
||||||
toggle_selection_admin_budget_budget_investment_path(
|
toggle_selection_admin_budget_budget_investment_path(
|
||||||
investment.budget,
|
investment.budget,
|
||||||
@@ -20,4 +33,12 @@ class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent
|
|||||||
page: params[:page]
|
page: params[:page]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_class
|
||||||
|
if selected?
|
||||||
|
"button small expanded"
|
||||||
|
else
|
||||||
|
"button small hollow expanded"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user