From 25e1afea485c3878560fd527fdf0d0fc9577281b Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Thu, 13 Dec 2018 10:00:54 +0100 Subject: [PATCH] fix map rendering for budget headings --- app/assets/stylesheets/layout.scss | 7 +++++++ .../budgets/investments_controller.rb | 6 +----- app/models/budget/heading.rb | 6 ++++-- app/models/map_location.rb | 8 ++++++++ app/views/budgets/ballot/lines/create.js.erb | 2 ++ app/views/budgets/ballot/lines/destroy.js.erb | 2 ++ app/views/budgets/investments/_map.html.erb | 6 +++--- .../budgets/investments/_sidebar.html.erb | 4 +++- spec/features/budgets/investments_spec.rb | 16 +++++++++++++++ spec/models/budget/heading_spec.rb | 20 ++++++------------- 10 files changed, 52 insertions(+), 25 deletions(-) diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index 59b851e79..7f4e1bb82 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -879,6 +879,13 @@ footer { } } +.sidebar-map { + + .map { + z-index: -1; + } +} + .sidebar-title { border-top: 2px solid $brand; display: inline-block; diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index a456ff54e..c0e91b080 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -1,6 +1,5 @@ module Budgets class InvestmentsController < ApplicationController - OSM_DISTRICT_LEVEL_ZOOM = 12 include FeatureFlags include CommentableActions @@ -180,10 +179,7 @@ module Budgets end def load_map - @map_location = MapLocation.new - @map_location.zoom = OSM_DISTRICT_LEVEL_ZOOM - @map_location.latitude = @heading.latitude.to_f - @map_location.longitude = @heading.longitude.to_f + @map_location = MapLocation.load_from_heading(@heading) end end diff --git a/app/models/budget/heading.rb b/app/models/budget/heading.rb index 20f9aa921..50c5d3992 100644 --- a/app/models/budget/heading.rb +++ b/app/models/budget/heading.rb @@ -1,5 +1,7 @@ class Budget class Heading < ActiveRecord::Base + OSM_DISTRICT_LEVEL_ZOOM = 12.freeze + include Sluggable belongs_to :group @@ -12,9 +14,9 @@ class Budget validates :price, presence: true validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/ 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/ - 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/ delegate :budget, :budget_id, to: :group, allow_nil: true diff --git a/app/models/map_location.rb b/app/models/map_location.rb index f66d71e34..d141c167d 100644 --- a/app/models/map_location.rb +++ b/app/models/map_location.rb @@ -18,4 +18,12 @@ class MapLocation < ActiveRecord::Base } 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 diff --git a/app/views/budgets/ballot/lines/create.js.erb b/app/views/budgets/ballot/lines/create.js.erb index b1c9f76be..6edf8a0f2 100644 --- a/app/views/budgets/ballot/lines/create.js.erb +++ b/app/views/budgets/ballot/lines/create.js.erb @@ -9,3 +9,5 @@ $("#<%= dom_id(@investment) %>_ballot").html('<%= j render("/budgets/investments investment: @investment, investment_ids: @investment_ids, ballot: @ballot %> + +App.Map.initialize(); diff --git a/app/views/budgets/ballot/lines/destroy.js.erb b/app/views/budgets/ballot/lines/destroy.js.erb index 82c303047..94120d3a9 100644 --- a/app/views/budgets/ballot/lines/destroy.js.erb +++ b/app/views/budgets/ballot/lines/destroy.js.erb @@ -10,3 +10,5 @@ $("#<%= dom_id(@investment) %>_ballot").html('<%= j render("/budgets/investments investment: @investment, investment_ids: @investment_ids, ballot: @ballot %> + +App.Map.initialize(); diff --git a/app/views/budgets/investments/_map.html.erb b/app/views/budgets/investments/_map.html.erb index af04ef639..37aa50f67 100644 --- a/app/views/budgets/investments/_map.html.erb +++ b/app/views/budgets/investments/_map.html.erb @@ -2,7 +2,7 @@
diff --git a/app/views/budgets/investments/_sidebar.html.erb b/app/views/budgets/investments/_sidebar.html.erb index 40322dbc9..e509aa17f 100644 --- a/app/views/budgets/investments/_sidebar.html.erb +++ b/app/views/budgets/investments/_sidebar.html.erb @@ -24,7 +24,9 @@ <% if @heading && !@heading.content_blocks.where(locale: I18n.locale).empty? %> <%= render 'budgets/investments/content_blocks' %> <% end %> -<%= render 'budgets/investments/map' %> +<% if @map_location&.available? %> + <%= render 'budgets/investments/map' %> +<% end %> <%= render "shared/tag_cloud", taggable: 'budget/investment' %> <%= render 'budgets/investments/categories' %> diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index 93f2f3799..641ab61fa 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -89,6 +89,22 @@ feature 'Budget Investments' do 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 scenario 'Search by text' do diff --git a/spec/models/budget/heading_spec.rb b/spec/models/budget/heading_spec.rb index edd96a8b8..816d7507e 100644 --- a/spec/models/budget/heading_spec.rb +++ b/spec/models/budget/heading_spec.rb @@ -7,6 +7,12 @@ describe Budget::Heading do 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 before do create(:budget_heading, group: group, name: 'object name') @@ -45,13 +51,6 @@ describe Budget::Heading do end 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 heading = create(:budget_heading, group: group, name: 'Latitude is < -90') @@ -150,13 +149,6 @@ describe Budget::Heading 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 heading = create(:budget_heading, group: group, name: 'Longitude is < -180')