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.
45 lines
1001 B
Ruby
45 lines
1001 B
Ruby
class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent
|
|
attr_reader :investment
|
|
use_helpers :can?
|
|
delegate :selected?, to: :investment
|
|
|
|
def initialize(investment)
|
|
@investment = investment
|
|
end
|
|
|
|
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,
|
|
investment,
|
|
filter: params[:filter],
|
|
sort_by: params[:sort_by],
|
|
min_total_supports: params[:min_total_supports],
|
|
max_total_supports: params[:max_total_supports],
|
|
advanced_filters: params[:advanced_filters],
|
|
page: params[:page]
|
|
)
|
|
end
|
|
|
|
def html_class
|
|
if selected?
|
|
"button small expanded"
|
|
else
|
|
"button small hollow expanded"
|
|
end
|
|
end
|
|
end
|