Skip to content

Commit d44fd31

Browse files
committed
move project map configuration to GTT settings tab #71
- also more refactoring, introduces helper methods for map rendering in both show and edit modes
1 parent 08ba9b2 commit d44fd31

File tree

16 files changed

+146
-59
lines changed

16 files changed

+146
-59
lines changed

app/helpers/gtt_map_helper.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
module GttMapHelper
4+
5+
def map_form_field(form, map, field: :geojson, edit_mode:)
6+
safe_join [
7+
form.hidden_field(field, id: 'geom'),
8+
map_tag(map: map, edit: edit_mode)
9+
]
10+
end
11+
12+
def map_tag(map: nil,
13+
geom: map.json, layers: map&.layers, bounds: geom, edit: nil)
14+
15+
data = {
16+
geom: geom,
17+
layers: layers
18+
}
19+
data[:bounds] = bounds if bounds
20+
data[:edit] = edit if edit
21+
22+
map_id = rand(36**8).to_s(36)
23+
content_tag(:div, "", data: data, id: "ol-#{map_id}", class: 'ol-map')
24+
end
25+
26+
end

app/models/gtt_configuration.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
class GttConfiguration
22
include ActiveModel::Model
33

4-
attr_accessor :project, :gtt_tile_source_ids
4+
attr_accessor :project, :geojson, :gtt_tile_source_ids
55

66
def self.for(project)
7-
new project: project
7+
new(
8+
project: project,
9+
geojson: project.geojson,
10+
gtt_tile_source_ids: project.gtt_tile_source_ids
11+
)
812
end
913

10-
def gtt_tile_source_ids
11-
project.gtt_tile_source_ids
14+
def self.from_params(params)
15+
new geojson: params[:geojson],
16+
gtt_tile_source_ids: params[:gtt_tile_source_ids]
1217
end
1318

14-
def gtt_tile_source_ids=(ids)
15-
project.gtt_tile_source_ids = ids
19+
def map
20+
GttMap.new json: geojson, layers: project.gtt_tile_sources
1621
end
22+
1723
end

app/models/gtt_map.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class GttMap
2+
3+
attr_reader :layers, :json
4+
5+
def initialize(wkb: nil, json: nil, layers:)
6+
unless @json = json
7+
@json = RedmineGtt::Conversions.wkb_to_json wkb if wkb
8+
end
9+
10+
@layers = layers
11+
end
12+
13+
end

app/overrides/projects.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
Deface::Override.new(
2-
:virtual_path => "projects/_form",
3-
:name => "deface_view_projects_form_map",
4-
:insert_after => "div.box",
5-
:partial => "projects/form/map"
6-
)
7-
81
Deface::Override.new(
92
:virtual_path => "projects/show",
103
:name => "deface_view_projects_show_map",

app/views/projects/form/_map.html.erb

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/views/projects/settings/_gtt.html.erb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
<% @gtt_configuration ||= GttConfiguration.for(@project) %>
2-
<%= labelled_form_for @gtt_configuration,
1+
<% @form ||= GttConfiguration.for(@project) %>
2+
<%= labelled_form_for @form,
33
url: update_gtt_configuration_path(@project),
44
html: { method: :put } do |f| %>
55

66
<h3><%= t :label_gtt_settings_headline %></h3>
7+
78
<div class="box tabular">
89
<p>
910
<%= f.select :gtt_tile_source_ids,
10-
options_from_collection_for_select(GttTileSource.all.order(name: :asc), :id, :name, selected: @gtt_configuration.gtt_tile_source_ids),
11+
options_from_collection_for_select(GttTileSource.all.order(name: :asc), :id, :name, selected: @form.gtt_tile_source_ids),
1112
{}, { multiple: true, size: 5 } %>
1213
<br /><em><%= t :gtt_tile_sources_info %></em>
1314
</p>
1415
</div>
1516

17+
<div class="box">
18+
<%= map_form_field f, @form.map, edit_mode: 'Polygon' %>
19+
</div>
20+
21+
1622
<%= submit_tag l(:button_save) %>
1723

1824
<% end %>

app/views/projects/show/_map.html.erb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
<div class="usermap box">
33
<h3><%= l(:label_project_map) %></h3>
44

5-
<%= content_tag(:div, "",:data => {
6-
:geom => @project.geojson,
7-
:bounds => @project.geojson,
8-
:layers => @project.gtt_tile_sources
9-
}, :id => 'ol-' + rand(36**8).to_s(36), :class => 'ol-map')
10-
%>
5+
<%= map_tag map: @project.map %>
116
</div>
127
<% end %>

lib/redmine_gtt.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# Global Hooks
24
require 'redmine_gtt/hooks/view_layouts_base_html_head_hook'
35

@@ -30,6 +32,10 @@ def self.setup
3032
RedmineGtt::Patches::ProjectsHelperPatch.apply
3133
RedmineGtt::Patches::UsersControllerPatch.apply
3234

35+
ProjectsController.class_eval do
36+
helper 'gtt_map'
37+
end
38+
3339
# unless IssueQuery.included_modules.include?(RedmineGtt::Patches::IssueQueryPatch)
3440
# IssueQuery.send(:include, RedmineGtt::Patches::IssueQueryPatch)
3541
# end

lib/redmine_gtt/actions/update_project_settings.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@ class UpdateProjectSettings < Base
44

55
Result = ImmutableStruct.new(:settings_saved?, :error)
66

7-
def initialize(project, params)
8-
@project = project
9-
@parameters = params.symbolize_keys
7+
def initialize(form)
8+
@form = form
9+
@project = form.project
1010
end
1111

1212
def call
13-
if tile_source_ids = @parameters.delete(:gtt_tile_source_ids)
13+
@project.transaction do
14+
if update_project
15+
Result.new settings_saved: true
16+
else
17+
Result.new settings_saved: false,
18+
error: @project.errors.full_messages
19+
end
20+
end
21+
end
22+
23+
private
24+
25+
def update_project
26+
if tile_source_ids = @form.gtt_tile_source_ids
1427
@project.gtt_tile_source_ids = tile_source_ids
1528
end
16-
Result.new settings_saved: true
29+
@project.geojson = @form.geojson
30+
31+
@project.save
1732
end
33+
1834
end
1935
end
2036
end

lib/redmine_gtt/patches/geojson_attribute.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ def geojson_additional_properties
99
end
1010

1111
def geojson
12-
if geom.present?
12+
@geojson ||= if geom.present?
1313
Conversions.wkb_to_json(
1414
geom, id: id, properties: geojson_additional_properties
1515
)
1616
end
1717
end
1818

19-
def geom=(geometry)
19+
def geojson=(geometry)
20+
@geojson = geometry
2021
if (geometry.present?)
21-
self[:geom] = Conversions.to_wkb geometry
22+
self.geom = Conversions.to_wkb geometry
2223
else
23-
self[:geom] = nil
24+
self.geom = nil
2425
end
2526
end
2627

0 commit comments

Comments
 (0)