Extract component to render the budget map

We're going to make a change, and it's easier if we've already got a
component with tests so we don't have to write system tests to check
whether the map is rendered.
This commit is contained in:
Javi Martín
2022-05-02 18:07:37 +02:00
parent 0d70b76331
commit 69ae2d31ee
6 changed files with 56 additions and 27 deletions

View File

@@ -33,13 +33,7 @@
<% end %>
<%= render Budgets::SupportsInfoComponent.new(budget) %>
<% unless budget.informing? %>
<div class="map inline">
<h2><%= t("budgets.index.map") %></h2>
<%= render_map(nil, "budgets", false, nil, coordinates) %>
</div>
<% end %>
<%= render Budgets::MapComponent.new(budget) %>
</div>
</div>
</div>

View File

@@ -1,22 +1,8 @@
class Budgets::BudgetComponent < ApplicationComponent
delegate :wysiwyg, :auto_link_already_sanitized_html, :render_map, to: :helpers
delegate :wysiwyg, :auto_link_already_sanitized_html, to: :helpers
attr_reader :budget
def initialize(budget)
@budget = budget
end
private
def coordinates
return unless budget.present?
if budget.publishing_prices_or_later? && budget.investments.selected.any?
investments = budget.investments.selected
else
investments = budget.investments
end
MapLocation.where(investment_id: investments).map(&:json_data)
end
end

View File

@@ -0,0 +1,4 @@
<div class="map inline">
<h2><%= t("budgets.index.map") %></h2>
<%= render_map(nil, "budgets", false, nil, coordinates) %>
</div>

View File

@@ -0,0 +1,26 @@
class Budgets::MapComponent < ApplicationComponent
delegate :render_map, to: :helpers
attr_reader :budget
def initialize(budget)
@budget = budget
end
def render?
!budget.informing?
end
private
def coordinates
return unless budget.present?
if budget.publishing_prices_or_later? && budget.investments.selected.any?
investments = budget.investments.selected
else
investments = budget.investments
end
MapLocation.where(investment_id: investments).map(&:json_data)
end
end

View File

@@ -0,0 +1,23 @@
require "rails_helper"
describe Budgets::MapComponent do
let(:budget) { build(:budget) }
describe "#render?" do
it "is rendered after the informing phase" do
budget.phase = "accepting"
render_inline Budgets::MapComponent.new(budget)
expect(page.first("div.map")).to have_content "located geographically"
end
it "is not rendered during the informing phase" do
budget.phase = "informing"
render_inline Budgets::MapComponent.new(budget)
expect(page).not_to be_rendered
end
end
end

View File

@@ -100,8 +100,6 @@ describe "Budgets" do
expect(page).not_to have_link("List of all investment projects")
expect(page).not_to have_link("List of all unfeasible investment projects")
expect(page).not_to have_link("List of all investment projects not selected for balloting")
expect(page).not_to have_css("div.map")
end
end
@@ -114,8 +112,6 @@ describe "Budgets" do
within("#budget_info") do
expect(page).not_to have_link heading.name
expect(page).to have_content "#{heading.name}\n€1,000,000"
expect(page).to have_css("div.map")
end
end
@@ -342,7 +338,7 @@ describe "Budgets" do
}
end
allow_any_instance_of(Budgets::BudgetComponent).to receive(:coordinates).and_return(coordinates)
allow_any_instance_of(Budgets::MapComponent).to receive(:coordinates).and_return(coordinates)
visit budgets_path