From 91e10e189719f5056bc3aa26b4962fa0962b2796 Mon Sep 17 00:00:00 2001 From: Vicente Mendoza Date: Wed, 17 Jan 2018 13:15:35 +0100 Subject: [PATCH 1/5] new map on budget homepage --- app/assets/javascripts/map.js.coffee | 14 ++++++++++++-- app/controllers/budgets_controller.rb | 2 ++ app/helpers/map_locations_helper.rb | 22 +++++++++++++--------- app/views/budgets/index.html.erb | 2 +- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/map.js.coffee b/app/assets/javascripts/map.js.coffee index 28ac34f29..853debfa3 100644 --- a/app/assets/javascripts/map.js.coffee +++ b/app/assets/javascripts/map.js.coffee @@ -24,6 +24,7 @@ App.Map = longitudeInputSelector = $(element).data('longitude-input-selector') zoomInputSelector = $(element).data('zoom-input-selector') removeMarkerSelector = $(element).data('marker-remove-selector') + addMarkerInvestemts = $(element).data('marker-investments-coordenates') editable = $(element).data('marker-editable') marker = null; markerIcon = L.divIcon( @@ -69,11 +70,15 @@ App.Map = $(zoomInputSelector).val '' return + contentPopup = (title,investment,budget) -> + content = "#{title}" + return content + mapCenterLatLng = new (L.LatLng)(mapCenterLatitude, mapCenterLongitude) map = L.map(element.id).setView(mapCenterLatLng, zoom) L.tileLayer(mapTilesProvider, attribution: mapAttribution).addTo map - if markerLatitude && markerLongitude + if markerLatitude && markerLongitude && !addMarkerInvestemts marker = createMarker(markerLatitude, markerLongitude) if editable @@ -81,6 +86,11 @@ App.Map = map.on 'zoomend', updateFormfields map.on 'click', moveOrPlaceMarker + if addMarkerInvestemts + for i in addMarkerInvestemts + add_marker=createMarker(i.lat , i.long) + add_marker.bindPopup(contentPopup(i.investment_title, i.investment_id, i.budget_id)) + toogleMap: -> $('.map').toggle() - $('.js-location-map-remove-marker').toggle() \ No newline at end of file + $('.js-location-map-remove-marker').toggle() diff --git a/app/controllers/budgets_controller.rb b/app/controllers/budgets_controller.rb index 4f215c46f..35089fae2 100644 --- a/app/controllers/budgets_controller.rb +++ b/app/controllers/budgets_controller.rb @@ -15,6 +15,8 @@ class BudgetsController < ApplicationController def index @budgets = @budgets.order(:created_at) + budgets_map_locations = Budget.where.not(phase: 'drafting').map{ |budget| budget.investments.map{|investment| investment.map_location}}.flatten.compact + @budgets_coordenates = budgets_map_locations.map{ |ml| {lat: ml.latitude, long: ml.longitude, investment_title: ml.investment.title , investment_id: ml.investment.id, budget_id: ml.investment.budget.id}} end end diff --git a/app/helpers/map_locations_helper.rb b/app/helpers/map_locations_helper.rb index 85b02e28c..4dd825006 100644 --- a/app/helpers/map_locations_helper.rb +++ b/app/helpers/map_locations_helper.rb @@ -24,11 +24,12 @@ module MapLocationsHelper "remove-marker-link-#{dom_id(map_location)}" end - def render_map(map_location, parent_class, editable, remove_marker_label) + def render_map(map_location, parent_class, editable, remove_marker_label,investments_coordenates=nil) + map_location = MapLocation.new if map_location.nil? map = content_tag_for :div, map_location, class: "map", - data: prepare_map_settings(map_location, editable, parent_class) + data: prepare_map_settings(map_location, editable, parent_class, investments_coordenates) map += map_location_remove_marker(map_location, remove_marker_label) if editable map end @@ -44,23 +45,26 @@ module MapLocationsHelper end end + private - def prepare_map_settings(map_location, editable, parent_class) + def prepare_map_settings(map_location, editable, parent_class,marker_investments_coordenates=nil) options = { map: "", - map_center_latitude: map_location_latitude(map_location), - map_center_longitude: map_location_longitude(map_location), - map_zoom: map_location_zoom(map_location), map_tiles_provider: Rails.application.secrets.map_tiles_provider, map_tiles_provider_attribution: Rails.application.secrets.map_tiles_provider_attribution, marker_editable: editable, - marker_latitude: map_location.latitude, - marker_longitude: map_location.longitude, + map_center_latitude: map_location_latitude(map_location), + map_center_longitude: map_location_longitude(map_location), + map_zoom: map_location_zoom(map_location), + marker_latitude: map_location_latitude(map_location.latitude), + marker_longitude: map_location_longitude(map_location.longitude), + marker_investments_coordenates: marker_investments_coordenates, marker_remove_selector: "##{map_location_remove_marker_link_id(map_location)}", latitude_input_selector: "##{map_location_input_id(parent_class, 'latitude')}", longitude_input_selector: "##{map_location_input_id(parent_class, 'longitude')}", - zoom_input_selector: "##{map_location_input_id(parent_class, 'zoom')}" + zoom_input_selector: "##{map_location_input_id(parent_class, 'zoom')}", + marker_investments_coordenates: marker_investments_coordenates } end diff --git a/app/views/budgets/index.html.erb b/app/views/budgets/index.html.erb index 0ccc8bcef..658ea2be6 100644 --- a/app/views/budgets/index.html.erb +++ b/app/views/budgets/index.html.erb @@ -4,7 +4,7 @@ <% end %> <%= render "shared/section_header", i18n_namespace: "budgets.index.section_header", image: "budgets" %> - +<%= render_map(nil, "budgets",false,nil, @budgets_coordenates) %>
From d0df96548731f763aa30a03b582a4d2be46150c4 Mon Sep 17 00:00:00 2001 From: Vicente Mendoza Date: Wed, 17 Jan 2018 17:10:19 +0100 Subject: [PATCH 2/5] fix some issues about test and variables name --- app/assets/javascripts/map.js.coffee | 8 ++++---- app/controllers/budgets_controller.rb | 2 +- app/helpers/map_locations_helper.rb | 20 +++++++++----------- app/views/budgets/index.html.erb | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/map.js.coffee b/app/assets/javascripts/map.js.coffee index 853debfa3..26f2c259b 100644 --- a/app/assets/javascripts/map.js.coffee +++ b/app/assets/javascripts/map.js.coffee @@ -24,7 +24,7 @@ App.Map = longitudeInputSelector = $(element).data('longitude-input-selector') zoomInputSelector = $(element).data('zoom-input-selector') removeMarkerSelector = $(element).data('marker-remove-selector') - addMarkerInvestemts = $(element).data('marker-investments-coordenates') + addMarkerInvestments = $(element).data('marker-investments-coordinates') editable = $(element).data('marker-editable') marker = null; markerIcon = L.divIcon( @@ -78,7 +78,7 @@ App.Map = map = L.map(element.id).setView(mapCenterLatLng, zoom) L.tileLayer(mapTilesProvider, attribution: mapAttribution).addTo map - if markerLatitude && markerLongitude && !addMarkerInvestemts + if markerLatitude && markerLongitude && !addMarkerInvestments marker = createMarker(markerLatitude, markerLongitude) if editable @@ -86,8 +86,8 @@ App.Map = map.on 'zoomend', updateFormfields map.on 'click', moveOrPlaceMarker - if addMarkerInvestemts - for i in addMarkerInvestemts + if addMarkerInvestments + for i in addMarkerInvestments add_marker=createMarker(i.lat , i.long) add_marker.bindPopup(contentPopup(i.investment_title, i.investment_id, i.budget_id)) diff --git a/app/controllers/budgets_controller.rb b/app/controllers/budgets_controller.rb index 35089fae2..586c90e94 100644 --- a/app/controllers/budgets_controller.rb +++ b/app/controllers/budgets_controller.rb @@ -16,7 +16,7 @@ class BudgetsController < ApplicationController def index @budgets = @budgets.order(:created_at) budgets_map_locations = Budget.where.not(phase: 'drafting').map{ |budget| budget.investments.map{|investment| investment.map_location}}.flatten.compact - @budgets_coordenates = budgets_map_locations.map{ |ml| {lat: ml.latitude, long: ml.longitude, investment_title: ml.investment.title , investment_id: ml.investment.id, budget_id: ml.investment.budget.id}} + @budgets_coordinates = budgets_map_locations.map{ |ml| {lat: ml.latitude, long: ml.longitude, investment_title: ml.investment.title , investment_id: ml.investment.id, budget_id: ml.investment.budget.id}} end end diff --git a/app/helpers/map_locations_helper.rb b/app/helpers/map_locations_helper.rb index 4dd825006..e05f5695e 100644 --- a/app/helpers/map_locations_helper.rb +++ b/app/helpers/map_locations_helper.rb @@ -24,12 +24,12 @@ module MapLocationsHelper "remove-marker-link-#{dom_id(map_location)}" end - def render_map(map_location, parent_class, editable, remove_marker_label,investments_coordenates=nil) + def render_map(map_location, parent_class, editable, remove_marker_label, investments_coordinates=nil) map_location = MapLocation.new if map_location.nil? map = content_tag_for :div, map_location, class: "map", - data: prepare_map_settings(map_location, editable, parent_class, investments_coordenates) + data: prepare_map_settings(map_location, editable, parent_class, investments_coordinates) map += map_location_remove_marker(map_location, remove_marker_label) if editable map end @@ -45,27 +45,25 @@ module MapLocationsHelper end end - private - def prepare_map_settings(map_location, editable, parent_class,marker_investments_coordenates=nil) + def prepare_map_settings(map_location, editable, parent_class, investments_coordinates=nil) options = { map: "", - map_tiles_provider: Rails.application.secrets.map_tiles_provider, - map_tiles_provider_attribution: Rails.application.secrets.map_tiles_provider_attribution, - marker_editable: editable, map_center_latitude: map_location_latitude(map_location), map_center_longitude: map_location_longitude(map_location), map_zoom: map_location_zoom(map_location), - marker_latitude: map_location_latitude(map_location.latitude), - marker_longitude: map_location_longitude(map_location.longitude), - marker_investments_coordenates: marker_investments_coordenates, + map_tiles_provider: Rails.application.secrets.map_tiles_provider, + map_tiles_provider_attribution: Rails.application.secrets.map_tiles_provider_attribution, + marker_editable: editable, marker_remove_selector: "##{map_location_remove_marker_link_id(map_location)}", latitude_input_selector: "##{map_location_input_id(parent_class, 'latitude')}", longitude_input_selector: "##{map_location_input_id(parent_class, 'longitude')}", zoom_input_selector: "##{map_location_input_id(parent_class, 'zoom')}", - marker_investments_coordenates: marker_investments_coordenates + marker_investments_coordinates: investments_coordinates } + options[:marker_latitude] = map_location.latitude if map_location.latitude.present? + options[:marker_longitude] = map_location.longitude if map_location.longitude.present? end end diff --git a/app/views/budgets/index.html.erb b/app/views/budgets/index.html.erb index 658ea2be6..e034dd482 100644 --- a/app/views/budgets/index.html.erb +++ b/app/views/budgets/index.html.erb @@ -4,7 +4,7 @@ <% end %> <%= render "shared/section_header", i18n_namespace: "budgets.index.section_header", image: "budgets" %> -<%= render_map(nil, "budgets",false,nil, @budgets_coordenates) %> +<%= render_map(nil, "budgets", false, nil, @budgets_coordinates) %>
From c673ca2319396a038b00e29ae034416b893e78aa Mon Sep 17 00:00:00 2001 From: Vicente Mendoza Date: Wed, 17 Jan 2018 18:36:45 +0100 Subject: [PATCH 3/5] fix tests --- app/helpers/map_locations_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/helpers/map_locations_helper.rb b/app/helpers/map_locations_helper.rb index e05f5695e..a471d1b8d 100644 --- a/app/helpers/map_locations_helper.rb +++ b/app/helpers/map_locations_helper.rb @@ -64,6 +64,7 @@ module MapLocationsHelper } options[:marker_latitude] = map_location.latitude if map_location.latitude.present? options[:marker_longitude] = map_location.longitude if map_location.longitude.present? + options end end From eeefa646a4871deb722ad614b0c0d4cc5da2fb07 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Thu, 18 Jan 2018 01:39:43 +0100 Subject: [PATCH 4/5] Default dev seeds geolocation at madrid, and geolocate all investments --- db/dev_seeds.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index cc36569c8..940771d5b 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -64,8 +64,8 @@ section "Creating Settings" do Setting.create(key: 'verification_offices_url', value: 'http://oficinas-atencion-ciudadano.url/') Setting.create(key: 'min_age_to_participate', value: '16') Setting.create(key: 'proposal_improvement_path', value: nil) - Setting.create(key: 'map_latitude', value: 51.48) - Setting.create(key: 'map_longitude', value: 0.0) + Setting.create(key: 'map_latitude', value: 40.41) + Setting.create(key: 'map_longitude', value: -3.7) Setting.create(key: 'map_zoom', value: 10) Setting.create(key: 'related_content_score_threshold', value: -0.3) end @@ -451,6 +451,17 @@ section "Creating Investments" do end end +section "Geolocating Investments" do + Budget.all.each do |budget| + budget.investments.each do |investment| + MapLocation.create(latitude: 40.4167278 + rand(-10..10)/100.to_f, + longitude: -3.7055274 + rand(-10..10)/100.to_f, + zoom: 10, + investment_id: investment.id) + end + end +end + section "Balloting Investments" do Budget.balloting.last.investments.each do |investment| investment.update(selected: true, feasibility: "feasible") From 304b3941a7de9aacbe242994b637d5da6a053750 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Thu, 18 Jan 2018 01:40:58 +0100 Subject: [PATCH 5/5] Move current budget's geolocated investments code to helper function --- app/controllers/budgets_controller.rb | 3 +-- app/helpers/budgets_helper.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/controllers/budgets_controller.rb b/app/controllers/budgets_controller.rb index f542a8c3f..52b735188 100644 --- a/app/controllers/budgets_controller.rb +++ b/app/controllers/budgets_controller.rb @@ -16,8 +16,7 @@ class BudgetsController < ApplicationController def index @budgets = @budgets.order(:created_at) @budget = current_budget - budgets_map_locations = current_budget.investments.map{ |investment| investment.map_location }.compact - @budgets_coordinates = budgets_map_locations.map{ |ml| {lat: ml.latitude, long: ml.longitude, investment_title: ml.investment.title , investment_id: ml.investment.id, budget_id: ml.investment.budget.id}} + @budgets_coordinates = current_budget_map_locations end end diff --git a/app/helpers/budgets_helper.rb b/app/helpers/budgets_helper.rb index 5e1949775..8ace7ccfb 100644 --- a/app/helpers/budgets_helper.rb +++ b/app/helpers/budgets_helper.rb @@ -53,4 +53,16 @@ module BudgetsHelper !budget.drafting? || current_user&.administrator? end + def current_budget_map_locations + current_budget.investments.map do |investment| + next unless investment.map_location.present? + { + lat: investment.map_location.latitude, + long: investment.map_location.longitude, + investment_title: investment.title, + investment_id: investment.id, + budget_id: current_budget.id + } + end.flatten.compact + end end