From b54a8458d5029b3494165b7430e21b3ae34ecc0c Mon Sep 17 00:00:00 2001 From: hellekin Date: Fri, 22 Jan 2021 05:32:15 +0100 Subject: Upgrade Rails and add StimulusJS support --- app/helpers/application_helper.rb | 19 ------------------- app/helpers/leaflet_helper.rb | 11 +++++++++++ app/helpers/map_helper.rb | 19 +++++++++++++++++++ app/helpers/sections_helper.rb | 19 +++++++++++++++++++ app/helpers/taxonomies/filter_helper.rb | 2 +- 5 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 app/helpers/leaflet_helper.rb create mode 100644 app/helpers/map_helper.rb create mode 100644 app/helpers/sections_helper.rb (limited to 'app/helpers') diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 15d5082..e39da5f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -6,25 +6,6 @@ module ApplicationHelper def current_agency current_user.agencies.where(agent: current_agent).first end - %w(observer editor maintainer leader).each do |role| - define_method :"current_user_#{role}?" do - current_agency.send(:"#{role}?") - end - end - - def map_container(map = Map.first) - raw tag.div( - tag.div(id: 'map', - data: { - target: 'map.container' - }), - data: { - controller: 'map', - 'map-latitude': map.latitude, - 'map-longitude': map.longitude, - 'map-zoom': map.zoom - }) - end # Markdown helper # Always use all extensions. Additional parser and render options may be diff --git a/app/helpers/leaflet_helper.rb b/app/helpers/leaflet_helper.rb new file mode 100644 index 0000000..6ca03f3 --- /dev/null +++ b/app/helpers/leaflet_helper.rb @@ -0,0 +1,11 @@ +module LeafletHelper + def marker_for(resource, options = {}) + coords = [resource.latitude, resource.longitude] + + "L.marker(#{coords}, #{options}).bindPopup(%s)" % popup_for(resource) + end + + def popup_for(resource) + render partial: 'resource/popup', locals: { resource: resource } + end +end diff --git a/app/helpers/map_helper.rb b/app/helpers/map_helper.rb new file mode 100644 index 0000000..3403801 --- /dev/null +++ b/app/helpers/map_helper.rb @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +module MapHelper + def map_container(map = Map.first) + raw tag.div( + tag.div(id: 'map', + data: { + target: 'map.container' + }), + data: { + controller: 'map', + 'map-latitude': map.latitude, + 'map-longitude': map.longitude, + 'map-zoom': map.zoom + }) + end +end diff --git a/app/helpers/sections_helper.rb b/app/helpers/sections_helper.rb new file mode 100644 index 0000000..8180fee --- /dev/null +++ b/app/helpers/sections_helper.rb @@ -0,0 +1,19 @@ +module SectionsHelper + # Render a section as a GeoJSON FeatureCollection + def geojson_feature_collection(section) + out = [] + class_name = { baseVal: "cat#{section.category_id} sec#{section.id}" } + style = { fill: section.category.color } + + section.resources.each do |marker| + # Add styling + marker = marker.to_geojson + marker['className'] = class_name + marker['style'] = style + + out << marker.to_json + end + + raw("{ \"type\": \"FeatureCollection\", \"features\": [ #{out.join(',')} ] }") + end +end diff --git a/app/helpers/taxonomies/filter_helper.rb b/app/helpers/taxonomies/filter_helper.rb index 97fe271..92a501f 100644 --- a/app/helpers/taxonomies/filter_helper.rb +++ b/app/helpers/taxonomies/filter_helper.rb @@ -11,7 +11,7 @@ module Taxonomies::FilterHelper @taxonomy.categories.each do |cat| list = [] cat.sections.each do |sec| - list << tag.li(h("#{sec.rank}. #{sec.name}"), id: "section-#{sec.id}", data: { action: "taxonomy#section", target: 'taxonomy.section', 'taxonomy-section-id': sec.id }) + list << tag.li(h("#{sec.rank}. #{sec.name}"), id: "section-#{sec.id}", data: { action: "click->taxonomy#section", target: 'taxonomy.section', 'taxonomy-section-id': sec.id }) end html << tag.li(h("#{cat.rank}. #{cat.name}") << tag.ol(list.join.html_safe), id: "category-#{cat.id}", -- cgit v1.2.3