From cd1a0548ba9a1def3105959f7242ce29cb1b8ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Wed, 7 Mar 2018 17:49:38 +0100 Subject: [PATCH 1/4] Add `map_location#json_data` method --- app/helpers/budgets_helper.rb | 11 +---------- app/models/map_location.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/helpers/budgets_helper.rb b/app/helpers/budgets_helper.rb index 85a727600..fc20c36a9 100644 --- a/app/helpers/budgets_helper.rb +++ b/app/helpers/budgets_helper.rb @@ -65,15 +65,6 @@ module BudgetsHelper 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 + MapLocation.where(investment_id: current_budget.investments).map { |l| l.json_data } end end diff --git a/app/models/map_location.rb b/app/models/map_location.rb index 3b0b2b3c0..590eeb284 100644 --- a/app/models/map_location.rb +++ b/app/models/map_location.rb @@ -9,4 +9,12 @@ class MapLocation < ActiveRecord::Base latitude.present? && longitude.present? && zoom.present? end + def json_data + { + id: id, + lat: latitude, + long: longitude + } + end + end From 535a96603a310762d5bc31531d4c5b4d4e05d324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Fri, 9 Mar 2018 17:44:51 +0100 Subject: [PATCH 2/4] Add `budget/investments#json_data` method --- .../budgets/investments_controller.rb | 22 ++++++++++++++++--- config/routes/budget.rb | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 902eaf50f..c48ce7fc4 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -4,10 +4,11 @@ module Budgets include CommentableActions include FlagActions - before_action :authenticate_user!, except: [:index, :show] + before_action :authenticate_user!, except: [:index, :show, :json_data] - load_and_authorize_resource :budget - load_and_authorize_resource :investment, through: :budget, class: "Budget::Investment" + load_and_authorize_resource :budget, except: :json_data + load_and_authorize_resource :investment, through: :budget, class: "Budget::Investment", + except: :json_data before_action -> { flash.now[:notice] = flash[:notice].html_safe if flash[:html_safe] && flash[:notice] } before_action :load_ballot, only: [:index, :show] @@ -16,6 +17,8 @@ module Budgets before_action :load_categories, only: [:index, :new, :create] before_action :set_default_budget_filter, only: :index + skip_authorization_check only: :json_data + feature_flag :budgets has_orders %w{most_voted newest oldest}, only: :show @@ -79,6 +82,19 @@ module Budgets super end + def json_data + investment = Budget::Investment.find(params[:id]) + data = { + investment_id: investment.id, + investment_title: investment.title, + budget_id: investment.budget.id + }.to_json + + respond_to do |format| + format.json { render json: data } + end + end + private def resource_model diff --git a/config/routes/budget.rb b/config/routes/budget.rb index 3a06f3c7c..e3a0acc16 100644 --- a/config/routes/budget.rb +++ b/config/routes/budget.rb @@ -17,3 +17,5 @@ scope '/participatory_budget' do post :vote, on: :member end end + +get 'investments/:id/json_data', to: :json_data, controller: 'budgets/investments' From 064089926dd738429e53d655a51a912cc0718f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Fri, 9 Mar 2018 17:43:32 +0100 Subject: [PATCH 3/4] Add ajax request for marker investment info --- app/assets/javascripts/map.js.coffee | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/map.js.coffee b/app/assets/javascripts/map.js.coffee index 26f2c259b..e70a65f4c 100644 --- a/app/assets/javascripts/map.js.coffee +++ b/app/assets/javascripts/map.js.coffee @@ -70,9 +70,18 @@ App.Map = $(zoomInputSelector).val '' return - contentPopup = (title,investment,budget) -> - content = "#{title}" - return content + openMarkerPopup = (e) -> + marker = e.target + + $.ajax 'investments/' + marker.options['id'] + '/json_data', + type: 'GET' + dataType: 'json' + success: (data) -> + e.target.bindPopup(getPopupContent(data)).openPopup() + + getPopupContent = (data) -> + content = "#{data['investment_title']}" + return content mapCenterLatLng = new (L.LatLng)(mapCenterLatitude, mapCenterLongitude) map = L.map(element.id).setView(mapCenterLatLng, zoom) @@ -88,8 +97,11 @@ App.Map = 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)) + if App.Map.validCoordinates(i) + marker = createMarker(i.lat, i.long) + marker.options['id'] = i.id + + marker.on 'click', openMarkerPopup toogleMap: -> $('.map').toggle() From 4713d3734614c97689b0349cdbdee6f987d01851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Fri, 9 Mar 2018 17:45:27 +0100 Subject: [PATCH 4/4] Add `budget/investments#json_data` method permissions --- app/models/abilities/everyone.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index a3720803e..23cfdc971 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -21,7 +21,7 @@ module Abilities can [:search, :read], Annotation can [:read], Budget can [:read], Budget::Group - can [:read, :print], Budget::Investment + can [:read, :print, :json_data], Budget::Investment can :read_results, Budget, phase: "finished" can :new, DirectMessage can [:read, :debate, :draft_publication, :allegations, :result_publication, :proposals], Legislation::Process, published: true