Skip to content

Commit 2662d27

Browse files
committed
use prepend for user* patches #50
1 parent 475b0aa commit 2662d27

File tree

3 files changed

+44
-69
lines changed

3 files changed

+44
-69
lines changed

lib/redmine_gtt.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
require 'redmine_gtt/patches/issues_controller_patch.rb'
77
require 'redmine_gtt/patches/issues_helper_patch.rb'
88

9-
# User Hooks
10-
require 'redmine_gtt/patches/user_patch.rb'
11-
require 'redmine_gtt/patches/users_controller_patch.rb'
12-
139

1410
# API Template Hooks
1511
# Seems like this is not necessary
@@ -29,9 +25,11 @@ module RedmineGtt
2925

3026
def self.setup
3127
RedmineGtt::Patches::ProjectPatch.apply
28+
RedmineGtt::Patches::UserPatch.apply
3229

3330
RedmineGtt::Patches::ProjectsControllerPatch.apply
3431
RedmineGtt::Patches::ProjectsHelperPatch.apply
32+
RedmineGtt::Patches::UsersControllerPatch.apply
3533

3634
# unless IssueQuery.included_modules.include?(RedmineGtt::Patches::IssueQueryPatch)
3735
# IssueQuery.send(:include, RedmineGtt::Patches::IssueQueryPatch)

lib/redmine_gtt/patches/user_patch.rb

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,48 @@ module RedmineGtt
22
module Patches
33

44
module UserPatch
5-
def self.included(base)
6-
base.extend(ClassMethods)
7-
base.send(:include, InstanceMethods)
8-
base.class_eval do
9-
unloadable
5+
def self.apply
6+
User.prepend self unless User < self
7+
User.class_eval do
108
safe_attributes "geom"
119
end
1210
end
1311

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(
22-
:support_ewkb => true,
23-
:default_srid => 4326
24-
).parse(self.geom)
25-
properties = self.as_json({except: [
26-
:hashed_password, :hashed_password, :salt, :must_change_passwd,
27-
:passwd_changed_on, :auth_source_id, :geom
28-
]})
29-
RGeo::GeoJSON.encode factory.feature(wkb, self.id, properties)
30-
else
31-
nil
32-
end
12+
def geojson
13+
unless self.geom.nil?
14+
factory = RGeo::GeoJSON::EntityFactory.instance
15+
wkb = RGeo::WKRep::WKBParser.new(
16+
:support_ewkb => true,
17+
:default_srid => 4326
18+
).parse(self.geom)
19+
properties = self.as_json({except: [
20+
:hashed_password, :hashed_password, :salt, :must_change_passwd,
21+
:passwd_changed_on, :auth_source_id, :geom
22+
]})
23+
RGeo::GeoJSON.encode factory.feature(wkb, self.id, properties)
24+
else
25+
nil
3326
end
27+
end
3428

35-
def geom=(g)
36-
# Turn geometry attribute into WKB for database use
37-
if (g.present?)
38-
geojson = JSON.parse(g)
39-
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
29+
def geom=(g)
30+
# Turn geometry attribute into WKB for database use
31+
if (g.present?)
32+
geojson = JSON.parse(g)
33+
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
4034

41-
ewkb = RGeo::WKRep::WKBGenerator.new(
42-
:type_format => :ewkb,
43-
:emit_ewkb_srid => true,
44-
:hex_format => true
45-
)
46-
self[:geom] = ewkb.generate(feature.geometry)
47-
else
48-
self[:geom] = nil
49-
end
35+
ewkb = RGeo::WKRep::WKBGenerator.new(
36+
:type_format => :ewkb,
37+
:emit_ewkb_srid => true,
38+
:hex_format => true
39+
)
40+
self[:geom] = ewkb.generate(feature.geometry)
41+
else
42+
self[:geom] = nil
5043
end
5144
end
5245

5346
end
5447
end
5548
end
5649

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

lib/redmine_gtt/patches/users_controller_patch.rb

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

44
module UsersControllerPatch
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-
end
12-
end
13-
14-
module ClassMethods
5+
def self.apply
6+
UsersController.prepend self unless UsersController < self
157
end
168

17-
module InstanceMethods
18-
def show_with_geojson
19-
respond_to do |format|
20-
format.geojson { send_data(
21-
@user.geojson.to_json,
22-
:type => 'application/json; header=present',
23-
:filename => "#{@user.login}.geojson")
24-
}
25-
format.any { show_without_geojson }
26-
end
9+
def show
10+
respond_to do |format|
11+
format.geojson { send_data(
12+
@user.geojson.to_json,
13+
:type => 'application/json; header=present',
14+
:filename => "#{@user.login}.geojson")
15+
}
16+
format.any { super }
2717
end
2818
end
29-
3019
end
20+
3121
end
3222
end
3323

34-
unless UsersController.included_modules.include?(RedmineGtt::Patches::UsersControllerPatch)
35-
UsersController.send(:include, RedmineGtt::Patches::UsersControllerPatch)
36-
end

0 commit comments

Comments
 (0)