Extract component to render my ballot

This is the only part of the sidebar that needs to be re-rendered after
an AJAX request adding or removing investments to a ballot, so having a
separate view just for it will make it easier to simplify the code.
This commit is contained in:
Javi Martín
2023-03-07 16:56:58 +01:00
parent 17d7451816
commit d0be5c4850
4 changed files with 67 additions and 44 deletions

View File

@@ -0,0 +1,44 @@
<div class="my-ballot">
<h2 class="sidebar-title">
<%= t("budgets.investments.index.sidebar.my_ballot") %>
</h2>
<% if ballot.investments.by_heading(heading.id).count > 0 %>
<p>
<em><%= sanitize(ballot.voted_info(heading)) %></em>
</p>
<% elsif assigned_heading.present? %>
<p>
<%= sanitize(t("budgets.investments.index.sidebar.different_heading_assigned",
heading_link: heading_link(assigned_heading, budget)
)) %>
<br>
<small>
<%= sanitize(t("budgets.investments.index.sidebar.change_ballot",
check_ballot: link_to(t("budgets.investments.index.sidebar.check_ballot_link"),
budget_ballot_path(budget)))) %>
</small>
</p>
<% else %>
<p><strong><%= t("budgets.investments.index.sidebar.zero") %></strong></p>
<% end %>
<p>
<%= sanitize(ballot.change_vote_info(
link: link_to(t("budgets.investments.index.sidebar.change_vote_link"),
budget_ballot_path(budget)))) %>
</p>
<ul class="ballot-list">
<% if heading %>
<%= render Budgets::Ballot::InvestmentForSidebarComponent.with_collection(
ballot.investments.by_heading(heading.id),
investment_ids: investment_ids
) %>
<% end %>
</ul>
<%= link_to t("budgets.investments.header.check_ballot"),
budget_ballot_path(budget),
class: "button hollow expanded" %>
</div>

View File

@@ -0,0 +1,17 @@
class Budgets::Investments::MyBallotComponent < ApplicationComponent
attr_reader :ballot, :heading, :investment_ids, :assigned_heading
delegate :heading_link, to: :helpers
def initialize(ballot:, heading:, investment_ids:, assigned_heading:)
@ballot = ballot
@heading = heading
@investment_ids = investment_ids
@assigned_heading = assigned_heading
end
private
def budget
ballot.budget
end
end

View File

@@ -13,50 +13,12 @@
<% end %>
<% if @heading && can?(:show, @ballot) %>
<div class="my-ballot">
<h2 class="sidebar-title">
<%= t("budgets.investments.index.sidebar.my_ballot") %>
</h2>
<% if @ballot.investments.by_heading(@heading.id).count > 0 %>
<p>
<em><%= sanitize(@ballot.voted_info(@heading)) %></em>
</p>
<% elsif @assigned_heading.present? %>
<p>
<%= sanitize(t("budgets.investments.index.sidebar.different_heading_assigned",
heading_link: heading_link(@assigned_heading, @budget)
)) %>
<br>
<small>
<%= sanitize(t("budgets.investments.index.sidebar.change_ballot",
check_ballot: link_to(t("budgets.investments.index.sidebar.check_ballot_link"),
budget_ballot_path(@budget)))) %>
</small>
</p>
<% else %>
<p><strong><%= t("budgets.investments.index.sidebar.zero") %></strong></p>
<% end %>
<p>
<%= sanitize(@ballot.change_vote_info(
link: link_to(t("budgets.investments.index.sidebar.change_vote_link"),
budget_ballot_path(@budget)))) %>
</p>
<ul class="ballot-list">
<% if @heading %>
<%= render Budgets::Ballot::InvestmentForSidebarComponent.with_collection(
@ballot.investments.by_heading(@heading.id),
investment_ids: @investment_ids
) %>
<% end %>
</ul>
<%= link_to t("budgets.investments.header.check_ballot"),
budget_ballot_path(@budget),
class: "button hollow expanded" %>
</div>
<%= render Budgets::Investments::MyBallotComponent.new(
ballot: @ballot,
heading: @heading,
investment_ids: @investment_ids,
assigned_heading: @assigned_heading
) %>
<% end %>
<%= render Budgets::Investments::ContentBlocksComponent.new(@heading) %>