Extract link to toggle selection to a component

This commit is contained in:
Javi Martín
2021-08-20 01:50:15 +02:00
parent 5251fbadd9
commit 3cebce9a29
9 changed files with 35 additions and 25 deletions

View File

@@ -0,0 +1 @@
<%= link_to_toggle_proposal_selection %>

View File

@@ -0,0 +1,26 @@
class Admin::Proposals::ToggleSelectionComponent < ApplicationComponent
attr_reader :proposal
def initialize(proposal)
@proposal = proposal
end
def link_to_toggle_proposal_selection
if proposal.selected?
button_text = t("admin.proposals.index.selected")
html_class = "button expanded"
else
button_text = t("admin.proposals.index.select")
html_class = "button hollow expanded"
end
case proposal.class.to_s
when "Proposal"
path = toggle_selection_admin_proposal_path(proposal)
when "Legislation::Proposal"
path = toggle_selection_admin_legislation_process_proposal_path(proposal.process, proposal)
end
link_to button_text, path, remote: true, method: :patch, class: html_class
end
end

View File

@@ -64,25 +64,6 @@ module ProposalsHelper
proposals_current_view == "default" ? "minimal" : "default"
end
def link_to_toggle_proposal_selection(proposal)
if proposal.selected?
button_text = t("admin.proposals.index.selected")
html_class = "button expanded"
else
button_text = t("admin.proposals.index.select")
html_class = "button hollow expanded"
end
case proposal.class.to_s
when "Proposal"
path = toggle_selection_admin_proposal_path(proposal)
when "Legislation::Proposal"
path = toggle_selection_admin_legislation_process_proposal_path(proposal.process, proposal)
end
link_to button_text, path, remote: true, method: :patch, class: html_class
end
def css_for_proposal_info_row(proposal)
if proposal.image.present?
if params[:selected].present?

View File

@@ -19,7 +19,7 @@
<td class="text-center"><%= proposal.id %></td>
<td><%= proposal.title %></td>
<td class="text-center"><%= proposal.votes_score %></td>
<td class="select"><%= render "select_proposal", proposal: proposal %></td>
<td class="select"><%= render Admin::Proposals::ToggleSelectionComponent.new(proposal) %></td>
</tr>
<% end %>
</tbody>

View File

@@ -1 +0,0 @@
<%= link_to_toggle_proposal_selection(proposal) %>

View File

@@ -1 +1,3 @@
$("#<%= dom_id(@proposal) %> .select").html("<%= j render("select_proposal", proposal: @proposal) %>");
$("#<%= dom_id(@proposal) %> .select").html(
"<%= j render(Admin::Proposals::ToggleSelectionComponent.new(@proposal)) %>"
);

View File

@@ -1 +0,0 @@
<%= link_to_toggle_proposal_selection(proposal) %>

View File

@@ -27,7 +27,7 @@
<td><%= link_to proposal.title, admin_proposal_path(proposal) %></td>
<td><%= proposal.author.username %></td>
<td><%= proposal.milestones.count %></td>
<td class="js-select"><%= render "select_proposal", proposal: proposal %></td>
<td class="js-select"><%= render Admin::Proposals::ToggleSelectionComponent.new(proposal) %></td>
</tr>
<% end %>
</tbody>

View File

@@ -1 +1,3 @@
$("#<%= dom_id(@proposal) %> .js-select").html("<%= j render("select_proposal", proposal: @proposal) %>");
$("#<%= dom_id(@proposal) %> .js-select").html(
"<%= j render(Admin::Proposals::ToggleSelectionComponent.new(@proposal)) %>"
);