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.
This commit is contained in:
Javi Martín
2019-02-18 15:44:04 +01:00
parent 8ecf7f4505
commit facfb807e1
2 changed files with 22 additions and 4 deletions

View File

@@ -169,11 +169,11 @@ module Budgets
def investments def investments
if @current_order == 'random' if @current_order == 'random'
@investments.apply_filters_and_search(@budget, params, @current_filter) @budget.investments.apply_filters_and_search(@budget, params, @current_filter)
.send("sort_by_#{@current_order}", params[:random_seed]) .send("sort_by_#{@current_order}", params[:random_seed])
else else
@investments.apply_filters_and_search(@budget, params, @current_filter) @budget.investments.apply_filters_and_search(@budget, params, @current_filter)
.send("sort_by_#{@current_order}") .send("sort_by_#{@current_order}")
end end
end end

View File

@@ -1874,6 +1874,24 @@ feature 'Budget Investments' do
expect(page).to have_css(".map-icon", count: 0, visible: false) expect(page).to have_css(".map-icon", count: 0, visible: false)
end end
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
end end