Extract methods in toggle selection component

This commit is contained in:
Javi Martín
2021-08-20 02:08:25 +02:00
parent 3cebce9a29
commit 3febe3f0cc
2 changed files with 27 additions and 15 deletions

View File

@@ -1 +1 @@
<%= link_to_toggle_proposal_selection %> <%= link_to text, path, options %>

View File

@@ -5,22 +5,34 @@ class Admin::Proposals::ToggleSelectionComponent < ApplicationComponent
@proposal = proposal @proposal = proposal
end end
def link_to_toggle_proposal_selection private
def text
if proposal.selected? if proposal.selected?
button_text = t("admin.proposals.index.selected") t("admin.proposals.index.selected")
html_class = "button expanded"
else else
button_text = t("admin.proposals.index.select") t("admin.proposals.index.select")
html_class = "button hollow expanded" end
end end
def path
case proposal.class.to_s case proposal.class.to_s
when "Proposal" when "Proposal"
path = toggle_selection_admin_proposal_path(proposal) toggle_selection_admin_proposal_path(proposal)
when "Legislation::Proposal" when "Legislation::Proposal"
path = toggle_selection_admin_legislation_process_proposal_path(proposal.process, proposal) toggle_selection_admin_legislation_process_proposal_path(proposal.process, proposal)
end
end end
link_to button_text, path, remote: true, method: :patch, class: html_class def options
{ remote: true, method: :patch, class: html_class }
end
def html_class
if proposal.selected?
"button expanded"
else
"button hollow expanded"
end
end end
end end