Move investment partial to a component

This way we'll be able to simplify it a little bit.

Note that the original partial didn't include the whole row and only
the cells. Since, most of the time, we include the whole row in
partials, we're slightly modifying the component.
This commit is contained in:
Javi Martín
2024-10-07 13:08:53 +02:00
parent 02b6302f25
commit bb42809168
7 changed files with 138 additions and 126 deletions

View File

@@ -0,0 +1,111 @@
<tr id="<%= dom_id(investment) %>" class="budget_investment">
<td class="text-right" data-field="id">
<strong><%= investment.id %></strong>
</td>
<td data-field="title">
<%= link_to investment.title,
admin_budget_budget_investment_path(budget_id: budget.id,
id: investment.id,
params: Budget::Investment.filter_params(params).to_h),
target: "_blank" %>
</td>
<td class="text-center" data-field="supports">
<%= investment.total_votes %>
</td>
<td class="small" data-field="admin">
<% if investment.administrator.present? %>
<span title="<%= t("admin.budget_investments.index.assigned_admin") %>">
<%= investment.administrator.description_or_name %>
</span>
<% else %>
<%= t("admin.budget_investments.index.no_admin_assigned") %>
<% end %>
</td>
<td class="small" data-field="author">
<%= investment.author.name %>
</td>
<td class="small" data-field="valuator">
<% valuators = [investment.assigned_valuation_groups, investment.assigned_valuators].compact %>
<% no_valuators_assigned = t("admin.budget_investments.index.no_valuators_assigned") %>
<%= valuators.present? ? valuators.join(", ") : no_valuators_assigned %>
</td>
<td class="small" data-field="geozone">
<%= investment.heading.name %>
</td>
<td class="small" data-field="feasibility">
<%= t("admin.budget_investments.index.feasibility.#{investment.feasibility}") %>
</td>
<% if budget.show_money? %>
<td class="small" data-field="price">
<%= investment.formatted_price %>
</td>
<% end %>
<td class="small text-center" data-field="valuation_finished">
<%= investment.valuation_finished? ? t("shared.yes") : t("shared.no") %>
</td>
<td class="small text-center" data-field="visible_to_valuators">
<% if can?(:admin_update, investment) %>
<%= form_for [:admin, budget, investment], remote: true, format: :json do |f| %>
<%= f.check_box :visible_to_valuators,
label: false,
class: "js-submit-on-change",
id: "budget_investment_visible_to_valuators" %>
<% end %>
<% else %>
<%= investment.visible_to_valuators? ? t("shared.yes") : t("shared.no") %>
<% end %>
</td>
<td id="selection" class="small text-center" data-field="selected">
<% if investment.selected? %>
<%= link_to_if can?(:toggle_selection, investment),
t("admin.budget_investments.index.selected"),
toggle_selection_admin_budget_budget_investment_path(
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]
),
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"),
toggle_selection_admin_budget_budget_investment_path(
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]
),
method: :patch,
remote: true,
class: "button small hollow expanded" %>
<% end %>
<% end %>
</td>
<% if params[:advanced_filters]&.include?("selected") %>
<td class="small text-center" data-field="incompatible">
<%= investment.incompatible? ? t("shared.yes") : t("shared.no") %>
</td>
<% end %>
</tr>

View File

@@ -0,0 +1,14 @@
class Admin::BudgetInvestments::RowComponent < ApplicationComponent
attr_reader :investment
use_helpers :can?
def initialize(investment)
@investment = investment
end
private
def budget
investment.budget
end
end

View File

@@ -49,9 +49,7 @@
<tbody> <tbody>
<% @investments.each do |investment| %> <% @investments.each do |investment| %>
<tr id="<%= dom_id(investment) %>" class="budget_investment"> <%= render Admin::BudgetInvestments::RowComponent.new(investment) %>
<%= render "/admin/budget_investments/select_investment", investment: investment %>
</tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>

View File

@@ -1,109 +0,0 @@
<td class="text-right" data-field="id">
<strong><%= investment.id %></strong>
</td>
<td data-field="title">
<%= link_to investment.title,
admin_budget_budget_investment_path(budget_id: @budget.id,
id: investment.id,
params: Budget::Investment.filter_params(params).to_h),
target: "_blank" %>
</td>
<td class="text-center" data-field="supports">
<%= investment.total_votes %>
</td>
<td class="small" data-field="admin">
<% if investment.administrator.present? %>
<span title="<%= t("admin.budget_investments.index.assigned_admin") %>">
<%= investment.administrator.description_or_name %>
</span>
<% else %>
<%= t("admin.budget_investments.index.no_admin_assigned") %>
<% end %>
</td>
<td class="small" data-field="author">
<%= investment.author.name %>
</td>
<td class="small" data-field="valuator">
<% valuators = [investment.assigned_valuation_groups, investment.assigned_valuators].compact %>
<% no_valuators_assigned = t("admin.budget_investments.index.no_valuators_assigned") %>
<%= valuators.present? ? valuators.join(", ") : no_valuators_assigned %>
</td>
<td class="small" data-field="geozone">
<%= investment.heading.name %>
</td>
<td class="small" data-field="feasibility">
<%= t("admin.budget_investments.index.feasibility.#{investment.feasibility}") %>
</td>
<% if @budget.show_money? %>
<td class="small" data-field="price">
<%= investment.formatted_price %>
</td>
<% end %>
<td class="small text-center" data-field="valuation_finished">
<%= investment.valuation_finished? ? t("shared.yes") : t("shared.no") %>
</td>
<td class="small text-center" data-field="visible_to_valuators">
<% if can?(:admin_update, investment) %>
<%= form_for [:admin, investment.budget, investment], remote: true, format: :json do |f| %>
<%= f.check_box :visible_to_valuators,
label: false,
class: "js-submit-on-change",
id: "budget_investment_visible_to_valuators" %>
<% end %>
<% else %>
<%= investment.visible_to_valuators? ? t("shared.yes") : t("shared.no") %>
<% end %>
</td>
<td id="selection" class="small text-center" data-field="selected">
<% if investment.selected? %>
<%= link_to_if can?(:toggle_selection, investment),
t("admin.budget_investments.index.selected"),
toggle_selection_admin_budget_budget_investment_path(
@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]
),
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"),
toggle_selection_admin_budget_budget_investment_path(
@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]
),
method: :patch,
remote: true,
class: "button small hollow expanded" %>
<% end %>
<% end %>
</td>
<% if params[:advanced_filters]&.include?("selected") %>
<td class="small text-center" data-field="incompatible">
<%= investment.incompatible? ? t("shared.yes") : t("shared.no") %>
</td>
<% end %>

View File

@@ -1 +1,3 @@
$("#<%= dom_id(@investment) %>").html("<%= j render("select_investment", investment: @investment) %>").trigger("inserted"); $("#<%= dom_id(@investment) %>").replaceWith(
"<%= j render Admin::BudgetInvestments::RowComponent.new(@investment) %>"
).trigger("inserted");

View File

@@ -0,0 +1,9 @@
require "rails_helper"
describe Admin::BudgetInvestments::RowComponent, :admin do
it "uses a JSON request to update visible to valuators" do
render_inline Admin::BudgetInvestments::RowComponent.new(create(:budget_investment))
expect(page).to have_css "form[action$='json'] input[name$='[visible_to_valuators]']"
end
end

View File

@@ -1,13 +0,0 @@
require "rails_helper"
describe "investment row" do
it "uses a JSON request to update visible to valuators" do
investment = create(:budget_investment)
@budget = investment.budget
sign_in(create(:administrator).user)
render "admin/budget_investments/select_investment", investment: investment
expect(rendered).to have_css "form[action$='json'] input[name$='[visible_to_valuators]']"
end
end