Skip to content

Commit

Permalink
Add new shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoonasAapro committed Nov 30, 2023
1 parent d06d4cf commit c399193
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 128 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gem "decidim-locations", path: "."

gem "bootsnap", "~> 1.4"
gem "puma", ">= 5.0.0"
gem "rgeo-geojson", "~> 2.1", ">= 2.1.1"
gem "uglifier", "~> 4.1"

group :development, :test do
Expand Down
11 changes: 8 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
decidim-locations (0.27.4)
decidim-core (~> 0.27.4)
rgeo-geojson (~> 2.1, >= 2.1.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -111,7 +112,7 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
carrierwave (2.2.4)
carrierwave (2.2.5)
activemodel (>= 5.0.0)
activesupport (>= 5.0.0)
addressable (~> 2.6)
Expand Down Expand Up @@ -362,7 +363,7 @@ GEM
temple
erubi (1.12.0)
escape_utils (1.3.0)
excon (0.104.0)
excon (0.105.0)
execjs (2.9.1)
extended-markdown-filter (0.7.0)
html-pipeline (~> 2.9)
Expand Down Expand Up @@ -499,7 +500,7 @@ GEM
msgpack (1.7.2)
multi_xml (0.6.0)
mustache (1.1.1)
net-imap (0.4.6)
net-imap (0.4.7)
date
net-protocol
net-pop (0.1.2)
Expand Down Expand Up @@ -636,6 +637,9 @@ GEM
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.6)
rgeo (3.0.1)
rgeo-geojson (2.1.1)
rgeo (>= 1.0.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand Down Expand Up @@ -810,6 +814,7 @@ DEPENDENCIES
letter_opener_web (~> 2.0)
listen (~> 3.1)
puma (>= 5.0.0)
rgeo-geojson (~> 2.1, >= 2.1.1)
rubocop-faker
rubocop-performance (~> 1.15.0)
spring (~> 2.0)
Expand Down
10 changes: 7 additions & 3 deletions app/cells/decidim/locations/locations/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@
<% form.fields_for :locations do |locations| %>
<% locations.fields_for "#{ loc.id ||= new_id }" do |location| %>
<%= location.hidden_field :address, class: "location-address", autocomplete: "off", value: loc.address %>
<%= location.hidden_field :shape, class: "location-shape", autocomplete: "off", value: loc.shape %>
<%= location.hidden_field :latitude, class: "location-latitude", autocomplete: "off", value: loc.latitude %>
<%= location.hidden_field :longitude, class: "location-longitude", autocomplete: "off", value: loc.longitude %>
<%= location.hidden_field :geojson, class: "location-geojson", autocomplete: "off", value: loc.geojson %>
<% end %>
<% end %>
</div>
<% end %>
</div>

<template id="model_input_template<%= random_id %>">
<template id="model_input_template-<%= random_id %>">
<% form.fields_for :locations do |locations| %>
<% locations.fields_for "%index%" do |location| %>
<%= location.hidden_field :address, class: "location-address", autocomplete: "off" %>
<%= location.hidden_field :shape, class: "location-shape", autocomplete: "off" %>
<%= location.hidden_field :latitude, class: "location-latitude", autocomplete: "off" %>
<%= location.hidden_field :longitude, class: "location-longitude", autocomplete: "off" %>
<%= location.hidden_field :geojson, class: "location-geojson", autocomplete: "off" %>
<% end %>
<% end %>
</template>
Expand All @@ -54,7 +58,7 @@
<li class="tabs-title text-center" data-action="more">
<ul class="dropdown menu" data-dropdown-menu data-autoclose="false" data-disable-hover="true" data-click-open="true" data-close-on-click="true" role="menubar">
<li class="tabs-title" role="none">
<a id="model_locations_tabs_more<%= random_id %>" href="#" aria-haspopup="true" role="menuitem"><%= icon "ellipses", role: "img", aria_label: "More options" %></a>
<a id="model_locations_tabs_more-<%= random_id %>" href="#" aria-haspopup="true" role="menuitem"><%= icon "ellipses", role: "img", aria_label: "More options" %></a>
<ul data-more-tabs class="menu" role="menu" aria-labelledby="model_locations_tabs_more">
<li role="none" data-action="clear-markers"><a href="#" role="menuitem"><%= t("clear_markers") %></a></li>
</ul>
Expand Down Expand Up @@ -86,7 +90,7 @@
</div>
</div>

<div id="pick_model_locations_map<%= random_id %>">
<div id="pick_model_locations_map-<%= random_id %>">
<%= dynamic_map_for({ type: "model_location"}, { id: random_id, data: { decidim_revgeocoding: geocoding_options, map_configuration: map_configuration, lat: coords[0], lng: coords[1] }}) do %>
<% add_snippets %>
<% end %>
Expand Down
4 changes: 3 additions & 1 deletion app/commands/concerns/decidim/locations/locations_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def update_locations(locatable, form)
attributes = {
address: location.address,
latitude: location.latitude,
longitude: location.longitude
longitude: location.longitude,
shape: location.shape,
geojson: location.geojson
}

if location.id.present?
Expand Down
11 changes: 11 additions & 0 deletions app/forms/decidim/locations/location_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@ class LocationForm < Decidim::Form
attribute :address, String
attribute :latitude, Float
attribute :longitude, Float
attribute :shape, String
attribute :geojson, JSON

validates :address, presence: true, if: ->(form) { !form.deleted && (form.latitude.blank? || form.longitude.blank?) }
validates :latitude, :longitude, presence: true, if: ->(form) { !form.deleted && form.address.blank? }
validate :json_validation, if: ->(form) { !form.deleted }

def to_param
return id if id.present?

"location-id"
end

private

def json_validation
return if RGeo::GeoJson.encode(RGeo::GeoJSON.decode(form.geojson)) == geojson

errors.add(:geojson, :invalid)
end
end
end
end
10 changes: 7 additions & 3 deletions app/helpers/decidim/locations/locations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def format_map_locations(model)
response,
:address,
:latitude,
:longitude
:longitude,
:shape,
:geojson
).map do |record|
# 0: ID of the record
# 1: Title of the record
Expand All @@ -53,15 +55,17 @@ def format_map_locations(model)
# 4: Address text of the record, e.g. "Foobar street 123"
# 5: Latitude of the record, e.g. 1.123
# 6: Longitude of the record, e.g. 2.234
# [123, "Title of the record", "Summary of the record", "Body text of the record", "Foobar street 123", 1.123, 2.234]
# 7: Shape of the record, e.g. "Polygon"
# 8: Geojson of the record, e.g. [[[30.0, 10.0], [12.2, 7.3]]]
# [123, "Title of the record", "Summary of the record", "Body text of the record", "Foobar street 123", 1.123, 2.234, "Shape of the record", "Geojson of the record"]
title = translated_attribute(JSON.parse(record[1]))
body =
begin
translated_attribute(JSON.parse(record[2]))
rescue JSON::ParserError
translated_attribute(record[2])
end
[record[0], title, nil, body, record[3], record[4], record[5]]
[record[0], title, nil, body, record[3], record[4], record[5], record[6], record[7]]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/packs/entrypoints/decidim_locations_edit_map.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import "src/decidim/locations/map";
import "src/decidim/revgeocoding/provider/here";
import "@geoman-io/leaflet-geoman-free";
import "src/decidim/geoman/leaflet-geoman.min.js";
Loading

0 comments on commit c399193

Please sign in to comment.