Skip to content

Commit 4740114

Browse files
committed
refactoring - use new helper for issue form #50
1 parent ff892ef commit 4740114

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

app/helpers/gtt_map_helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ module GttMapHelper
55
def map_form_field(form, map, field: :geojson, edit_mode:)
66
safe_join [
77
form.hidden_field(field, id: 'geom'),
8-
map_tag(map: map, edit: edit_mode)
8+
map_tag(map: map, bounds: map.bounds, edit: edit_mode)
99
]
1010
end
1111

12-
def map_tag(map: nil,
13-
geom: map.json, layers: map&.layers, bounds: geom, edit: nil)
12+
def map_tag(map: nil, layers: map&.layers,
13+
geom: map.json, bounds: map.bounds,
14+
edit: nil)
1415

1516
data = {
1617
geom: geom,

app/models/gtt_map.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class GttMap
22

3-
attr_reader :layers, :json
3+
attr_reader :layers, :json, :bounds
44

5-
def initialize(wkb: nil, json: nil, layers:)
5+
def initialize(wkb: nil, json: nil, layers:, bounds: nil)
66
unless @json = json
77
@json = RedmineGtt::Conversions.wkb_to_json wkb if wkb
88
end
99

10+
@bounds = bounds || @json
1011
@layers = layers
1112
end
1213

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
User.current.allowed_to?(:edit_issues, @issue.project))
44
%>
55
<div class="box">
6-
<%= f.hidden_field(:geom, :value => @issue.geojson, :id => 'geom') %>
7-
8-
<%= content_tag(:div, "", :data => {
9-
:geom => @issue.geojson,
10-
:bounds => @project.geojson,
11-
:layers => @project.gtt_tile_sources,
12-
:edit => 'Point LineString Polygon'
13-
}, :id => 'ol-' + rand(36**8).to_s(36), :class => 'ol-map')
14-
%>
6+
<%= map_form_field f, @issue.map, edit_mode: 'Point LineString Polygon' %>
157
</div>
168
<% end %>

lib/redmine_gtt.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ def self.setup
3333
RedmineGtt::Patches::ProjectsHelperPatch.apply
3434
RedmineGtt::Patches::UsersControllerPatch.apply
3535

36-
[ ProjectsController, MyController, UsersController ].each do |c|
37-
c.class_eval do
38-
helper 'gtt_map'
39-
end
40-
end
36+
[
37+
IssuesController,
38+
MyController,
39+
ProjectsController,
40+
UsersController,
41+
].each{ |c| c.send :helper, 'gtt_map' }
4142

4243
# unless IssueQuery.included_modules.include?(RedmineGtt::Patches::IssueQueryPatch)
4344
# IssueQuery.send(:include, RedmineGtt::Patches::IssueQueryPatch)

lib/redmine_gtt/patches/geojson_attribute.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ def geojson_additional_properties
88
as_json except: [:geom]
99
end
1010

11-
def geojson
11+
# use simple: true to skip inclusion of record's properties
12+
def geojson(simple: false)
1213
@geojson ||= if geom.present?
1314
Conversions.wkb_to_json(
14-
geom, id: id, properties: geojson_additional_properties
15+
geom,
16+
id: id,
17+
properties: (simple ? {} : geojson_additional_properties)
1518
)
1619
end
1720
end

lib/redmine_gtt/patches/issue_patch.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ def self.apply
88
Issue.prepend self
99
Issue.prepend GeojsonAttribute
1010
Issue.class_eval do
11-
safe_attributes "geom",
11+
safe_attributes "geojson",
1212
if: ->(issue, user){ user.allowed_to?(:edit_issues, issue.project)}
1313
end
1414
end
1515
end
1616

1717
def map
18-
GttMap.new json: geojson, layers: project.gtt_tile_sources
18+
GttMap.new json: geojson, layers: project.gtt_tile_sources,
19+
bounds: (new_record? ? project.map.json : geojson(simple: true))
1920
end
2021

2122
end

0 commit comments

Comments
 (0)