From 569138cbae753c64b24cf19256c2c4964de09419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= <15726+Senen@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:10:11 +0100 Subject: [PATCH 1/2] Extract view into component --- .../table_component.html.erb | 19 ++++++++++++++++++ .../print_investments/table_component.rb | 7 +++++++ .../budgets/print_investments.html.erb | 20 +------------------ 3 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 app/components/management/budgets/print_investments/table_component.html.erb create mode 100644 app/components/management/budgets/print_investments/table_component.rb diff --git a/app/components/management/budgets/print_investments/table_component.html.erb b/app/components/management/budgets/print_investments/table_component.html.erb new file mode 100644 index 000000000..2159a0cfe --- /dev/null +++ b/app/components/management/budgets/print_investments/table_component.html.erb @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + +
<%= t("management.budgets.table_name") %><%= t("management.budgets.table_phase") %><%= t("management.budgets.table_actions") %>
<%= budget.name %><%= budget.current_phase.name %> + <%= link_to t("management.budgets.print_investments"), + print_management_budget_investments_path(budget) %> +
diff --git a/app/components/management/budgets/print_investments/table_component.rb b/app/components/management/budgets/print_investments/table_component.rb new file mode 100644 index 000000000..4f13e14e4 --- /dev/null +++ b/app/components/management/budgets/print_investments/table_component.rb @@ -0,0 +1,7 @@ +class Management::Budgets::PrintInvestments::TableComponent < ApplicationComponent + attr_reader :budget + + def initialize(budget) + @budget = budget + end +end diff --git a/app/views/management/budgets/print_investments.html.erb b/app/views/management/budgets/print_investments.html.erb index 9bb4d7ea3..6e34bfe2f 100644 --- a/app/views/management/budgets/print_investments.html.erb +++ b/app/views/management/budgets/print_investments.html.erb @@ -1,21 +1,3 @@

<%= t("management.budgets.print_investments") %>

- - - - - - - - - - - - - - - -
<%= t("management.budgets.table_name") %><%= t("management.budgets.table_phase") %><%= t("management.budgets.table_actions") %>
<%= @budget.name %><%= @budget.current_phase.name %> - <%= link_to t("management.budgets.print_investments"), - print_management_budget_investments_path(@budget) %> -
+<%= render Management::Budgets::PrintInvestments::TableComponent.new(@budget) %> From 96f584c4f7b1b2b7ee9f694ef0443ec6cdfd828c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= <15726+Senen@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:58:15 +0100 Subject: [PATCH 2/2] Allow printing investments from any budget in the management interface --- .../table_component.html.erb | 20 +++++---- .../print_investments/table_component.rb | 7 +-- app/controllers/management/base_controller.rb | 4 -- .../management/budgets_controller.rb | 2 +- .../budgets/print_investments.html.erb | 8 +++- .../management/budget_investments_spec.rb | 45 +++++++++++++++++++ 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/app/components/management/budgets/print_investments/table_component.html.erb b/app/components/management/budgets/print_investments/table_component.html.erb index 2159a0cfe..424ae41b2 100644 --- a/app/components/management/budgets/print_investments/table_component.html.erb +++ b/app/components/management/budgets/print_investments/table_component.html.erb @@ -7,13 +7,17 @@ - - <%= budget.name %> - <%= budget.current_phase.name %> - - <%= link_to t("management.budgets.print_investments"), - print_management_budget_investments_path(budget) %> - - + <% budgets.each do |budget| %> + + <%= budget.name %> + <%= budget.current_phase.name %> + + <%= link_to t("management.budgets.print_investments"), + print_management_budget_investments_path(budget) %> + + + <% end %> + +<%= paginate budgets %> diff --git a/app/components/management/budgets/print_investments/table_component.rb b/app/components/management/budgets/print_investments/table_component.rb index 4f13e14e4..95f896297 100644 --- a/app/components/management/budgets/print_investments/table_component.rb +++ b/app/components/management/budgets/print_investments/table_component.rb @@ -1,7 +1,8 @@ class Management::Budgets::PrintInvestments::TableComponent < ApplicationComponent - attr_reader :budget + attr_reader :budgets + delegate :paginate, to: :helpers - def initialize(budget) - @budget = budget + def initialize(budgets) + @budgets = budgets end end diff --git a/app/controllers/management/base_controller.rb b/app/controllers/management/base_controller.rb index f426cbd3c..b408d01fa 100644 --- a/app/controllers/management/base_controller.rb +++ b/app/controllers/management/base_controller.rb @@ -49,10 +49,6 @@ class Management::BaseController < ActionController::Base I18n.with_locale(session[:locale], &action) end - def current_budget - Budget.current - end - def clear_password session[:new_password] = nil end diff --git a/app/controllers/management/budgets_controller.rb b/app/controllers/management/budgets_controller.rb index 36f0a8f1d..2c2e3607e 100644 --- a/app/controllers/management/budgets_controller.rb +++ b/app/controllers/management/budgets_controller.rb @@ -19,7 +19,7 @@ class Management::BudgetsController < Management::BaseController end def print_investments - @budget = current_budget + @budgets = Budget.published.order(created_at: :desc).page(params[:page]) end private diff --git a/app/views/management/budgets/print_investments.html.erb b/app/views/management/budgets/print_investments.html.erb index 6e34bfe2f..3af320a92 100644 --- a/app/views/management/budgets/print_investments.html.erb +++ b/app/views/management/budgets/print_investments.html.erb @@ -1,3 +1,9 @@

<%= t("management.budgets.print_investments") %>

-<%= render Management::Budgets::PrintInvestments::TableComponent.new(@budget) %> +<% if @budgets.any? %> + <%= render Management::Budgets::PrintInvestments::TableComponent.new(@budgets) %> +<% else %> +
+ <%= t("management.budgets.no_budgets") %> +
+<% end %> diff --git a/spec/system/management/budget_investments_spec.rb b/spec/system/management/budget_investments_spec.rb index 857a1f146..37a7ad4e4 100644 --- a/spec/system/management/budget_investments_spec.rb +++ b/spec/system/management/budget_investments_spec.rb @@ -407,6 +407,51 @@ describe "Budget Investments" do end context "Printing" do + scenario "Shows all published budgets, last created first" do + finished_budget = create(:budget, :finished) + accepting_budget = create(:budget) + draft_budget = create(:budget, published: false) + login_as_manager(manager) + + click_link "Print budget investments" + + within "#budget_#{accepting_budget.id}" do + expect(page).to have_link("Print budget investments") + end + within "#budget_#{finished_budget.id}" do + expect(page).to have_link("Print budget investments") + end + expect(page).not_to have_content draft_budget.name + expect(accepting_budget.name).to appear_before(finished_budget.name) + end + + scenario "Shows a message when there are no budgets to show" do + login_as_manager(manager) + + click_link "Print budget investments" + + expect(page).to have_content("There are no active participatory budgets.") + end + + scenario "Show pagination when needed" do + allow(Budget).to receive(:default_per_page).and_return(1) + create(:budget, name: "Children") + create(:budget, name: "Sports") + login_as_manager(manager) + click_link "Print budget investments" + + expect(page).to have_content("Sports") + + within("ul.pagination") do + expect(page).to have_content("1") + expect(page).to have_link("2", href: print_investments_management_budgets_path(page: "2")) + expect(page).not_to have_content("3") + click_link "Next", exact: false + end + + expect(page).to have_content("Children") + end + scenario "Printing budget investments" do 16.times { create(:budget_investment, heading: heading) }