From facfb807e1c38194738c6199d9298b2f222a36ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 18 Feb 2019 15:44:04 +0100 Subject: [PATCH] Show all investments in the map We were showing only the ones being shown in the current page because we were modifying `@investments` using a method which used `@investments`, and we were calling that method twice. There are many possible solutions: using a local variable to store the result of the `investments` method, modifying `@investments` after modifying `@investments_map_coordinates`, ... I've used the one which in my humble opinion is a bit less fragile: not using `@investments` inside the `investments` method. That way, the `investments` method will always return the same result. Note `stub_const("Budgets::InvestmentsController::PER_PAGE", 2)` wouldn't work because `Budgets::InvestmentsController` isn't loaded when that line is executed. So we need to load it. Instead of requiring the file, using `#{Budgets::InvestmentsController}` seems to be an easier solution. --- .../budgets/investments_controller.rb | 8 ++++---- spec/features/budgets/investments_spec.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 381a4cea1..a48fa3188 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -169,11 +169,11 @@ module Budgets def investments if @current_order == 'random' - @investments.apply_filters_and_search(@budget, params, @current_filter) - .send("sort_by_#{@current_order}", params[:random_seed]) + @budget.investments.apply_filters_and_search(@budget, params, @current_filter) + .send("sort_by_#{@current_order}", params[:random_seed]) else - @investments.apply_filters_and_search(@budget, params, @current_filter) - .send("sort_by_#{@current_order}") + @budget.investments.apply_filters_and_search(@budget, params, @current_filter) + .send("sort_by_#{@current_order}") end end diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index a0d02bc37..e669579df 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -1874,6 +1874,24 @@ feature 'Budget Investments' do expect(page).to have_css(".map-icon", count: 0, visible: false) end end + + scenario "Shows all investments and not only the ones on the current page", :js do + stub_const("#{Budgets::InvestmentsController}::PER_PAGE", 2) + + 3.times do + create(:map_location, investment: create(:budget_investment, heading: heading)) + end + + visit budget_investments_path(budget, heading_id: heading.id) + + within("#budget-investments") do + expect(page).to have_css(".budget-investment", count: 2) + end + + within(".map_location") do + expect(page).to have_css(".map-icon", count: 3, visible: false) + end + end end end