Move admin investments partial to a component

This way it'll be easier to organize the code related to it.
This commit is contained in:
Javi Martín
2024-10-07 13:45:23 +02:00
parent e4df6426c2
commit 607dabbc8a
5 changed files with 37 additions and 26 deletions

View File

@@ -4,11 +4,11 @@
class: "float-right small clear" %> class: "float-right small clear" %>
<% if params[:advanced_filters].include?("winners") %> <% if params[:advanced_filters].include?("winners") %>
<%= render Admin::Budgets::CalculateWinnersButtonComponent.new(@budget, from_investments: true) %> <%= render Admin::Budgets::CalculateWinnersButtonComponent.new(budget, from_investments: true) %>
<% end %> <% end %>
<% if @investments.any? %> <% if investments.any? %>
<h3 class="inline-block"><%= page_entries_info @investments %></h3> <h3 class="inline-block"><%= page_entries_info investments %></h3>
<%= render "admin/shared/columns_selector", <%= render "admin/shared/columns_selector",
cookie: "investments-columns", cookie: "investments-columns",
default: %w[id title supports admin valuator geozone feasibility price valuation_finished visible_to_valuators selected incompatible] %> default: %w[id title supports admin valuator geozone feasibility price valuation_finished visible_to_valuators selected incompatible] %>
@@ -32,7 +32,7 @@
</th> </th>
<th data-field="geozone"><%= t("admin.budget_investments.index.list.geozone") %></th> <th data-field="geozone"><%= t("admin.budget_investments.index.list.geozone") %></th>
<th data-field="feasibility"><%= t("admin.budget_investments.index.list.feasibility") %></th> <th data-field="feasibility"><%= t("admin.budget_investments.index.list.feasibility") %></th>
<% if @budget.show_money? %> <% if budget.show_money? %>
<th data-field="price"><%= t("admin.budget_investments.index.list.price") %></th> <th data-field="price"><%= t("admin.budget_investments.index.list.price") %></th>
<% end %> <% end %>
<th data-field="valuation_finished"> <th data-field="valuation_finished">
@@ -49,13 +49,13 @@
</thead> </thead>
<tbody> <tbody>
<% @investments.each do |investment| %> <% investments.each do |investment| %>
<%= render Admin::BudgetInvestments::RowComponent.new(investment) %> <%= render Admin::BudgetInvestments::RowComponent.new(investment) %>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<%= paginate @investments %> <%= paginate investments %>
<% else %> <% else %>
<div class="callout primary clear"> <div class="callout primary clear">
<%= t("admin.budget_investments.index.no_budget_investments") %> <%= t("admin.budget_investments.index.no_budget_investments") %>

View File

@@ -0,0 +1,30 @@
class Admin::BudgetInvestments::InvestmentsComponent < ApplicationComponent
attr_reader :budget, :investments
use_helpers :set_direction, :set_sorting_icon
def initialize(budget, investments)
@budget = budget
@investments = investments
end
private
def csv_params
csv_params = params.clone.merge(format: :csv)
csv_params = csv_params.to_unsafe_h.transform_keys(&:to_sym)
csv_params.delete(:page)
csv_params
end
def link_to_investments_sorted_by(column)
direction = set_direction(params[:direction])
icon = set_sorting_icon(direction, column)
translation = t("admin.budget_investments.index.list.#{column}")
link_to(
safe_join([translation, tag.span(class: "icon-sortable #{icon}")]),
admin_budget_budget_investments_path(sort_by: column, direction: direction)
)
end
end

View File

@@ -3,18 +3,6 @@ module BudgetInvestmentsHelper
params.map { |af| t("admin.budget_investments.index.filters.#{af}") }.join(", ") params.map { |af| t("admin.budget_investments.index.filters.#{af}") }.join(", ")
end end
def link_to_investments_sorted_by(column)
direction = set_direction(params[:direction])
icon = set_sorting_icon(direction, column)
translation = t("admin.budget_investments.index.list.#{column}")
link_to(
safe_join([translation, tag.span(class: "icon-sortable #{icon}")]),
admin_budget_budget_investments_path(sort_by: column, direction: direction)
)
end
def set_sorting_icon(direction, sort_by) def set_sorting_icon(direction, sort_by)
if sort_by.to_s == params[:sort_by] if sort_by.to_s == params[:sort_by]
if direction == "desc" if direction == "desc"

View File

@@ -1,11 +1,4 @@
module BudgetsHelper module BudgetsHelper
def csv_params
csv_params = params.clone.merge(format: :csv)
csv_params = csv_params.to_unsafe_h.transform_keys(&:to_sym)
csv_params.delete(:page)
csv_params
end
def namespaced_budget_investment_path(investment, options = {}) def namespaced_budget_investment_path(investment, options = {})
case namespace case namespace
when "management" when "management"

View File

@@ -12,4 +12,4 @@
<%= render "/shared/filter_subnav", i18n_namespace: "admin.budget_investments.index" %> <%= render "/shared/filter_subnav", i18n_namespace: "admin.budget_investments.index" %>
<%= render "investments" %> <%= render Admin::BudgetInvestments::InvestmentsComponent.new(@budget, @investments) %>