From bb4280916814f5b26109c2b9da0ec32d9c6593a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 7 Oct 2024 13:08:53 +0200 Subject: [PATCH] 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. --- .../budget_investments/row_component.html.erb | 111 ++++++++++++++++++ .../admin/budget_investments/row_component.rb | 14 +++ .../budget_investments/_investments.html.erb | 4 +- .../_select_investment.html.erb | 109 ----------------- .../toggle_selection.js.erb | 4 +- .../budget_investments/row_component_spec.rb | 9 ++ .../select_investment_spec.rb | 13 -- 7 files changed, 138 insertions(+), 126 deletions(-) create mode 100644 app/components/admin/budget_investments/row_component.html.erb create mode 100644 app/components/admin/budget_investments/row_component.rb delete mode 100644 app/views/admin/budget_investments/_select_investment.html.erb create mode 100644 spec/components/admin/budget_investments/row_component_spec.rb delete mode 100644 spec/views/admin/budget_investments/select_investment_spec.rb diff --git a/app/components/admin/budget_investments/row_component.html.erb b/app/components/admin/budget_investments/row_component.html.erb new file mode 100644 index 000000000..e615a67c3 --- /dev/null +++ b/app/components/admin/budget_investments/row_component.html.erb @@ -0,0 +1,111 @@ + + + <%= investment.id %> + + + + <%= 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" %> + + + + <%= investment.total_votes %> + + + + <% if investment.administrator.present? %> + "> + <%= investment.administrator.description_or_name %> + + <% else %> + <%= t("admin.budget_investments.index.no_admin_assigned") %> + <% end %> + + + + <%= investment.author.name %> + + + + <% 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 %> + + + + <%= investment.heading.name %> + + + + <%= t("admin.budget_investments.index.feasibility.#{investment.feasibility}") %> + + + <% if budget.show_money? %> + + <%= investment.formatted_price %> + + <% end %> + + + <%= investment.valuation_finished? ? t("shared.yes") : t("shared.no") %> + + + + <% 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 %> + + + + <% 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 %> + + + <% if params[:advanced_filters]&.include?("selected") %> + + <%= investment.incompatible? ? t("shared.yes") : t("shared.no") %> + + <% end %> + diff --git a/app/components/admin/budget_investments/row_component.rb b/app/components/admin/budget_investments/row_component.rb new file mode 100644 index 000000000..96a429cf2 --- /dev/null +++ b/app/components/admin/budget_investments/row_component.rb @@ -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 diff --git a/app/views/admin/budget_investments/_investments.html.erb b/app/views/admin/budget_investments/_investments.html.erb index f378fd63a..05559b935 100644 --- a/app/views/admin/budget_investments/_investments.html.erb +++ b/app/views/admin/budget_investments/_investments.html.erb @@ -49,9 +49,7 @@ <% @investments.each do |investment| %> - - <%= render "/admin/budget_investments/select_investment", investment: investment %> - + <%= render Admin::BudgetInvestments::RowComponent.new(investment) %> <% end %> diff --git a/app/views/admin/budget_investments/_select_investment.html.erb b/app/views/admin/budget_investments/_select_investment.html.erb deleted file mode 100644 index 9753b35f9..000000000 --- a/app/views/admin/budget_investments/_select_investment.html.erb +++ /dev/null @@ -1,109 +0,0 @@ - - <%= investment.id %> - - - - <%= 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" %> - - - - <%= investment.total_votes %> - - - - <% if investment.administrator.present? %> - "> - <%= investment.administrator.description_or_name %> - - <% else %> - <%= t("admin.budget_investments.index.no_admin_assigned") %> - <% end %> - - - - <%= investment.author.name %> - - - - <% 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 %> - - - - <%= investment.heading.name %> - - - - <%= t("admin.budget_investments.index.feasibility.#{investment.feasibility}") %> - - -<% if @budget.show_money? %> - - <%= investment.formatted_price %> - -<% end %> - - - <%= investment.valuation_finished? ? t("shared.yes") : t("shared.no") %> - - - - <% 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 %> - - - - <% 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 %> - - -<% if params[:advanced_filters]&.include?("selected") %> - - <%= investment.incompatible? ? t("shared.yes") : t("shared.no") %> - -<% end %> diff --git a/app/views/admin/budget_investments/toggle_selection.js.erb b/app/views/admin/budget_investments/toggle_selection.js.erb index ecf457e8c..75fc98a36 100644 --- a/app/views/admin/budget_investments/toggle_selection.js.erb +++ b/app/views/admin/budget_investments/toggle_selection.js.erb @@ -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"); diff --git a/spec/components/admin/budget_investments/row_component_spec.rb b/spec/components/admin/budget_investments/row_component_spec.rb new file mode 100644 index 000000000..4f7dfab7c --- /dev/null +++ b/spec/components/admin/budget_investments/row_component_spec.rb @@ -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 diff --git a/spec/views/admin/budget_investments/select_investment_spec.rb b/spec/views/admin/budget_investments/select_investment_spec.rb deleted file mode 100644 index e04c6dd8f..000000000 --- a/spec/views/admin/budget_investments/select_investment_spec.rb +++ /dev/null @@ -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