fix map rendering for budget headings

This commit is contained in:
Julian Herrero
2018-12-13 10:00:54 +01:00
parent 01033e5371
commit 25e1afea48
10 changed files with 52 additions and 25 deletions

View File

@@ -879,6 +879,13 @@ footer {
} }
} }
.sidebar-map {
.map {
z-index: -1;
}
}
.sidebar-title { .sidebar-title {
border-top: 2px solid $brand; border-top: 2px solid $brand;
display: inline-block; display: inline-block;

View File

@@ -1,6 +1,5 @@
module Budgets module Budgets
class InvestmentsController < ApplicationController class InvestmentsController < ApplicationController
OSM_DISTRICT_LEVEL_ZOOM = 12
include FeatureFlags include FeatureFlags
include CommentableActions include CommentableActions
@@ -180,10 +179,7 @@ module Budgets
end end
def load_map def load_map
@map_location = MapLocation.new @map_location = MapLocation.load_from_heading(@heading)
@map_location.zoom = OSM_DISTRICT_LEVEL_ZOOM
@map_location.latitude = @heading.latitude.to_f
@map_location.longitude = @heading.longitude.to_f
end end
end end

View File

@@ -1,5 +1,7 @@
class Budget class Budget
class Heading < ActiveRecord::Base class Heading < ActiveRecord::Base
OSM_DISTRICT_LEVEL_ZOOM = 12.freeze
include Sluggable include Sluggable
belongs_to :group belongs_to :group
@@ -12,9 +14,9 @@ class Budget
validates :price, presence: true validates :price, presence: true
validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/ validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/
validates :population, numericality: { greater_than: 0 }, allow_nil: true validates :population, numericality: { greater_than: 0 }, allow_nil: true
validates :latitude, length: { maximum: 22, minimum: 1 }, presence: true, \ validates :latitude, length: { maximum: 22 }, allow_blank: true, \
format: /\A(-|\+)?([1-8]?\d(?:\.\d{1,})?|90(?:\.0{1,6})?)\z/ format: /\A(-|\+)?([1-8]?\d(?:\.\d{1,})?|90(?:\.0{1,6})?)\z/
validates :longitude, length: { maximum: 22, minimum: 1}, presence: true, \ validates :longitude, length: { maximum: 22 }, allow_blank: true, \
format: /\A(-|\+)?((?:1[0-7]|[1-9])?\d(?:\.\d{1,})?|180(?:\.0{1,})?)\z/ format: /\A(-|\+)?((?:1[0-7]|[1-9])?\d(?:\.\d{1,})?|180(?:\.0{1,})?)\z/
delegate :budget, :budget_id, to: :group, allow_nil: true delegate :budget, :budget_id, to: :group, allow_nil: true

View File

@@ -18,4 +18,12 @@ class MapLocation < ActiveRecord::Base
} }
end end
def self.load_from_heading(heading)
map = new
map.zoom = Budget::Heading::OSM_DISTRICT_LEVEL_ZOOM
map.latitude = heading.latitude.to_f if heading.latitude.present?
map.longitude = heading.longitude.to_f if heading.latitude.present?
map
end
end end

View File

@@ -9,3 +9,5 @@ $("#<%= dom_id(@investment) %>_ballot").html('<%= j render("/budgets/investments
investment: @investment, investment: @investment,
investment_ids: @investment_ids, investment_ids: @investment_ids,
ballot: @ballot %> ballot: @ballot %>
App.Map.initialize();

View File

@@ -10,3 +10,5 @@ $("#<%= dom_id(@investment) %>_ballot").html('<%= j render("/budgets/investments
investment: @investment, investment: @investment,
investment_ids: @investment_ids, investment_ids: @investment_ids,
ballot: @ballot %> ballot: @ballot %>
App.Map.initialize();

View File

@@ -24,7 +24,9 @@
<% if @heading && !@heading.content_blocks.where(locale: I18n.locale).empty? %> <% if @heading && !@heading.content_blocks.where(locale: I18n.locale).empty? %>
<%= render 'budgets/investments/content_blocks' %> <%= render 'budgets/investments/content_blocks' %>
<% end %> <% end %>
<% if @map_location&.available? %>
<%= render 'budgets/investments/map' %> <%= render 'budgets/investments/map' %>
<% end %>
<%= render "shared/tag_cloud", taggable: 'budget/investment' %> <%= render "shared/tag_cloud", taggable: 'budget/investment' %>
<%= render 'budgets/investments/categories' %> <%= render 'budgets/investments/categories' %>

View File

@@ -89,6 +89,22 @@ feature 'Budget Investments' do
end end
end end
scenario 'Index should show a map if heading has coordinates defined', :js do
create(:budget_investment, heading: heading)
visit budget_investments_path(budget, heading_id: heading.id)
within("#sidebar") do
expect(page).to have_css(".map_location")
end
unlocated_heading = create(:budget_heading, name: "No Map", price: 500, group: group,
longitude: nil, latitude: nil)
create(:budget_investment, heading: unlocated_heading)
visit budget_investments_path(budget, heading_id: unlocated_heading.id)
within("#sidebar") do
expect(page).not_to have_css(".map_location")
end
end
context("Search") do context("Search") do
scenario 'Search by text' do scenario 'Search by text' do

View File

@@ -7,6 +7,12 @@ describe Budget::Heading do
it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget
describe "::OSM_DISTRICT_LEVEL_ZOOM" do
it "should be defined" do
expect(Budget::Heading::OSM_DISTRICT_LEVEL_ZOOM).to be 12
end
end
describe "name" do describe "name" do
before do before do
create(:budget_heading, group: group, name: 'object name') create(:budget_heading, group: group, name: 'object name')
@@ -45,13 +51,6 @@ describe Budget::Heading do
end end
describe "save latitude" do describe "save latitude" do
it "Doesn't allow latitude == nil" do
expect(build(:budget_heading, group: group, name: 'Latitude is nil', population: 12412512, latitude: nil, longitude: '12.123412')).not_to be_valid
end
it "Doesn't allow latitude == ''" do
expect(build(:budget_heading, group: group, name: 'Latitude is an empty string', population: 12412512, latitude: '', longitude: '12.123412')).not_to be_valid
end
it "Doesn't allow latitude < -90" do it "Doesn't allow latitude < -90" do
heading = create(:budget_heading, group: group, name: 'Latitude is < -90') heading = create(:budget_heading, group: group, name: 'Latitude is < -90')
@@ -150,13 +149,6 @@ describe Budget::Heading do
describe "save longitude" do describe "save longitude" do
it "Doesn't allow longitude == nil" do
expect(build(:budget_heading, group: group, name: 'Longitude is nil', population: 12412512, latitude: '12.123412', longitude: nil)).not_to be_valid
end
it "Doesn't allow longitude == ''" do
expect(build(:budget_heading, group: group, name: 'Longitude is an empty string', population: 12412512, latitude: '12.127412', longitude: '')).not_to be_valid
end
it "Doesn't allow longitude < -180" do it "Doesn't allow longitude < -180" do
heading = create(:budget_heading, group: group, name: 'Longitude is < -180') heading = create(:budget_heading, group: group, name: 'Longitude is < -180')