Skip to content

Commit bd4ae21

Browse files
committed
refactor issue* patches #50
1 parent 2662d27 commit bd4ae21

File tree

4 files changed

+59
-93
lines changed

4 files changed

+59
-93
lines changed

lib/redmine_gtt.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# Global Hooks
22
require 'redmine_gtt/hooks/view_layouts_base_html_head_hook'
33

4-
# Issue Hooks
5-
require 'redmine_gtt/patches/issue_patch.rb'
6-
require 'redmine_gtt/patches/issues_controller_patch.rb'
7-
require 'redmine_gtt/patches/issues_helper_patch.rb'
8-
94

105
# API Template Hooks
116
# Seems like this is not necessary
@@ -24,9 +19,13 @@
2419
module RedmineGtt
2520

2621
def self.setup
22+
RedmineGtt::Patches::IssuesHelperPatch.apply
23+
24+
RedmineGtt::Patches::IssuePatch.apply
2725
RedmineGtt::Patches::ProjectPatch.apply
2826
RedmineGtt::Patches::UserPatch.apply
2927

28+
RedmineGtt::Patches::IssuesControllerPatch.apply
3029
RedmineGtt::Patches::ProjectsControllerPatch.apply
3130
RedmineGtt::Patches::ProjectsHelperPatch.apply
3231
RedmineGtt::Patches::UsersControllerPatch.apply
@@ -35,7 +34,6 @@ def self.setup
3534
# IssueQuery.send(:include, RedmineGtt::Patches::IssueQueryPatch)
3635
# end
3736

38-
# IssuesController.send(:prepend, RedmineGtt::Patches::IssuesControllerPatch)
3937

4038
#Redmine::Views::ApiTemplateHandler.send(:prepend, RedmineGtt::Patches::ApiTemplateHandlerPatch)
4139
end

lib/redmine_gtt/patches/issue_patch.rb

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,44 @@ module RedmineGtt
22
module Patches
33

44
module IssuePatch
5-
def self.included(base)
6-
base.extend(ClassMethods)
7-
base.send(:include, InstanceMethods)
8-
base.class_eval do
9-
unloadable
5+
6+
def self.apply
7+
Issue.prepend self unless Issue < self
8+
Issue.class_eval do
109
safe_attributes "geom" if lambda {|issue, user| user.allowed_to?(:edit_issues, issue.project)}
1110
end
1211
end
1312

14-
module ClassMethods
15-
end
16-
17-
module InstanceMethods
18-
def geojson
19-
unless self.geom.nil?
20-
factory = RGeo::GeoJSON::EntityFactory.instance
21-
wkb = RGeo::WKRep::WKBParser.new().parse(self.geom)
22-
RGeo::GeoJSON.encode factory.feature(wkb, self.id, self.as_json)
23-
else
24-
nil
25-
end
13+
def geojson
14+
unless self.geom.nil?
15+
factory = RGeo::GeoJSON::EntityFactory.instance
16+
wkb = RGeo::WKRep::WKBParser.new().parse(self.geom)
17+
RGeo::GeoJSON.encode factory.feature(wkb, self.id, self.as_json)
18+
else
19+
nil
2620
end
21+
end
2722

28-
def geom=(g)
29-
# Turn geometry attribute into WKB for database use
30-
if (g.present?)
31-
begin
32-
geojson = JSON.parse(g)
33-
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
34-
wkb = RGeo::WKRep::WKBGenerator.new(
35-
:hex_format => true
36-
)
37-
self[:geom] = wkb.generate(feature.geometry)
38-
rescue
39-
# The Gemetry is likely to be already in WKB format
40-
self[:geom] = g
41-
end
42-
else
43-
self[:geom] = nil
23+
def geom=(g)
24+
# Turn geometry attribute into WKB for database use
25+
if (g.present?)
26+
begin
27+
geojson = JSON.parse(g)
28+
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
29+
wkb = RGeo::WKRep::WKBGenerator.new(
30+
:hex_format => true
31+
)
32+
self[:geom] = wkb.generate(feature.geometry)
33+
rescue
34+
# The Gemetry is likely to be already in WKB format
35+
self[:geom] = g
4436
end
37+
else
38+
self[:geom] = nil
4539
end
4640
end
47-
4841
end
42+
4943
end
5044
end
5145

52-
unless Issue.included_modules.include?(RedmineGtt::Patches::IssuePatch)
53-
Issue.send(:include, RedmineGtt::Patches::IssuePatch)
54-
end

lib/redmine_gtt/patches/issues_controller_patch.rb

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,44 @@ module RedmineGtt
22
module Patches
33

44
module IssuesControllerPatch
5-
def self.included(base)
6-
base.extend(ClassMethods)
7-
base.send(:include, InstanceMethods)
8-
base.class_eval do
9-
unloadable
10-
alias_method_chain :show, :geojson
11-
alias_method_chain :index, :geojson
12-
end
5+
def self.apply
6+
IssuesController.prepend self unless IssuesController < self
137
end
148

15-
module ClassMethods
9+
def show
10+
respond_to do |format|
11+
format.geojson { send_data(
12+
@issue.geojson.to_json,
13+
:type => 'application/json; header=present',
14+
:filename => "#{@issue.id}.geojson")
15+
}
16+
format.any { super }
17+
end
1618
end
1719

18-
module InstanceMethods
19-
def show_with_geojson
20-
respond_to do |format|
21-
format.geojson { send_data(
22-
@issue.geojson.to_json,
23-
:type => 'application/json; header=present',
24-
:filename => "#{@issue.id}.geojson")
25-
}
26-
format.any { show_without_geojson }
27-
end
28-
end
2920

30-
def index_with_geojson
31-
# It's not so great to have to copy the whole "index" method to add
32-
# another output format (geojson). Hopefully this isn't too annoying
33-
# to maintain. See the previous alias how it would be much nicer.
34-
retrieve_query
21+
def index
22+
retrieve_query
3523

36-
if @query.valid?
37-
respond_to do |format|
38-
format.geojson { send_data(
24+
if @query.valid?
25+
respond_to do |format|
26+
format.geojson {
27+
send_data(
28+
# TODO move that method somewhere else:
3929
IssuesHelper.get_geojson(@issues).to_json,
4030
:type => 'application/json; header=present',
4131
:filename => "issues.geojson")
42-
}
43-
format.any { index_without_geojson }
44-
end
45-
else
46-
respond_to do |format|
47-
format.any { index_without_geojson }
48-
end
32+
}
33+
format.any { super }
4934
end
35+
else
36+
super
5037
end
38+
rescue ActiveRecord::RecordNotFound
39+
render_404
5140
end
5241

5342
end
5443
end
5544
end
5645

57-
unless IssuesController.included_modules.include?(RedmineGtt::Patches::IssuesControllerPatch)
58-
IssuesController.send(:include, RedmineGtt::Patches::IssuesControllerPatch)
59-
end

lib/redmine_gtt/patches/issues_helper_patch.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ module RedmineGtt
22
module Patches
33

44
module IssuesHelperPatch
5-
def self.included(base)
6-
base.extend(ClassMethods)
7-
base.send(:include, InstanceMethods)
8-
base.class_eval do
9-
unloadable
10-
end
5+
def self.apply
6+
IssuesHelper.extend(ClassMethods)
117
end
128

9+
# FIXME move that method somewhere else
1310
module ClassMethods
1411
def get_geojson(issues)
1512
unless issues.nil?
@@ -35,13 +32,7 @@ def get_geojson(issues)
3532
end
3633
end
3734

38-
module InstanceMethods
39-
end
40-
4135
end
4236
end
4337
end
4438

45-
unless IssuesHelper.included_modules.include?(RedmineGtt::Patches::IssuesHelperPatch)
46-
IssuesHelper.send(:include, RedmineGtt::Patches::IssuesHelperPatch)
47-
end

0 commit comments

Comments
 (0)