Skip to content

Commit df78941

Browse files
committed
Merge branch 'develop' into 'master'
Develop See merge request gtt/redmine_gtt!15
2 parents 4077fed + 4eacdb2 commit df78941

File tree

9 files changed

+130
-18
lines changed

9 files changed

+130
-18
lines changed

init.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
author 'Georepublic'
66
author_url 'https://hub.georepublic.net/gtt/redmine_gtt'
77
description 'Adds location-based task management and maps'
8-
version '0.8.1'
8+
version '0.9.0'
99

1010
requires_redmine :version_or_higher => '3.4.0'
1111

lib/redmine_gtt/patches/projects_controller_patch.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ def self.apply
66
ProjectsController.prepend self unless ProjectsController < self
77
end
88

9+
# overrides index action to add spatial filtering to projects API listing
10+
def index
11+
respond_to do |format|
12+
format.api {
13+
scope = RedmineGtt::SpatialProjectsQuery.new(
14+
contains: params[:contains],
15+
projects: Project.visible.sorted
16+
).scope
17+
@project_count = scope.count
18+
@offset, @limit = api_offset_and_limit
19+
@projects = scope.offset(@offset).limit(@limit).to_a
20+
}
21+
format.any { super }
22+
end
23+
end
24+
925
def show
1026
respond_to do |format|
1127
format.geojson { send_data(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module RedmineGtt
2+
class SpatialProjectsQuery
3+
def initialize(contains: nil, projects: Project.active.visible)
4+
@contains = contains.presence
5+
@projects = projects
6+
end
7+
8+
QUERY_SQL = (<<-SQL
9+
#{Project.table_name}.geom IS NOT NULL AND
10+
ST_Intersects(#{Project.table_name}.geom, ST_GeomFromText('%s', 4326))
11+
SQL
12+
).freeze
13+
14+
def scope
15+
if @contains
16+
@projects = @projects.where(
17+
Project.send(:sanitize_sql_array, [ QUERY_SQL, @contains ])
18+
)
19+
end
20+
@projects
21+
end
22+
23+
end
24+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require_relative '../test_helper'
2+
3+
class ProjectsApiTest < Redmine::IntegrationTest
4+
fixtures :users, :email_addresses, :roles, :projects, :members, :member_roles
5+
6+
setup do
7+
User.current = nil
8+
@project = Project.find 'ecookbook'
9+
@project.enabled_modules.create name: 'gtt'
10+
end
11+
12+
test 'should should filter projects by geometry' do
13+
get '/projects.xml'
14+
assert_response :success
15+
xml = xml_data
16+
assert projects = xml.xpath('/projects/project')
17+
assert projects.any?
18+
assert_equal Project.visible.count, projects.size
19+
20+
get '/projects.xml', contains: 'POINT(123.271 9.35)'
21+
assert_response :success
22+
xml = xml_data
23+
assert projects = xml.xpath('/projects/project')
24+
assert_equal 0, projects.size
25+
26+
@project.update_attribute :geojson, {
27+
'type' => 'Feature',
28+
'geometry' => {
29+
'type' => 'Polygon',
30+
'coordinates' => [
31+
[[123.269691,9.305099], [123.279691,9.305099],[123.279691,9.405099],[123.269691,9.405099]]
32+
]
33+
}
34+
}.to_json
35+
36+
assert @project.visible?
37+
38+
get '/projects.xml', contains: 'POINT(123.271 9.55)'
39+
assert_response :success
40+
xml = xml_data
41+
assert projects = xml.xpath('/projects/project')
42+
assert_equal 0, projects.size
43+
44+
get '/projects.xml', contains: 'POINT(123.271 9.35)'
45+
assert_response :success
46+
xml = xml_data
47+
assert projects = xml.xpath('/projects/project')
48+
assert_equal 1, projects.size, xml.to_s
49+
assert_equal 'ecookbook', projects.xpath('identifier').text
50+
51+
end
52+
53+
def xml_data
54+
Nokogiri::XML(@response.body)
55+
end
56+
end
57+
58+

test/test_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
class GttTest < ActiveSupport::TestCase
55

6+
def test_geojson
7+
{'type'=>'Feature','geometry'=>{ 'type'=>'Polygon','coordinates'=> [[[135.22073422241215,34.70569060003112],[135.30227337646488,34.6990600142143],[135.3000417785645,34.670969984370885],[135.25283489990238,34.676052303889435],[135.1942125396729,34.67668404351015],[135.22073422241215,34.70569060003112]]]}}.to_json
8+
end
9+
610
def test_geom
711
RedmineGtt::Conversions::WkbToGeom.("01030000000100000006000000C84B374110E76040381DD011545A4140C84B3739ACE96040F07E6DCC7A594140C84B37F199E960403CBC2D58E2554140C84B373917E8604098CBC3E188564140C84B37FD36E66040F24C2E959D564140C84B374110E76040381DD011545A4140")
812
end
913

1014
def assert_geojson(json)
15+
json = JSON.parse json if json.is_a?(String)
1116
assert_equal 'Feature', json['type']
1217
assert geom = json['geometry']
1318
assert_equal 'Polygon', geom['type']

test/unit/gtt_map_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class GttMapTest < GttTest
66
@ts = GttTileSource.create! name: 'test', type: 'ol.source.OSM'
77
end
88

9-
test 'should compute json from wkb' do
9+
test 'should compute json from geom' do
1010
m = GttMap.new layers: [@ts], geom: test_geom
1111
assert_geojson m.json
1212
end

test/unit/issue_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ class IssueTest < GttTest
66
setup do
77
@project = Project.find 'ecookbook'
88
@issue = @project.issues.last
9-
@issue.update_column :geom, test_geom
9+
@issue.update_attribute :geojson, test_geojson
10+
@issue = Issue.find @issue.id
11+
end
12+
13+
test 'should have geom attribute' do
14+
assert @issue.geom.present?
1015
end
1116

1217
test 'should have geojson attribute' do

test/unit/project_test.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
class ProjectTest < GttTest
44
fixtures :projects
55

6-
test 'should have geojson attribute' do
7-
p = Project.find 'ecookbook'
8-
p.update_column :geom, test_geom
6+
setup do
7+
@p = Project.find 'ecookbook'
8+
@p.update_attribute :geojson, test_geojson
9+
end
910

10-
assert_geojson p.geojson
11+
test 'should have geojson and geom attribute' do
12+
assert @p.geom.present?
13+
assert_geojson @p.geojson
1114
end
1215

1316
test 'should render properties in as_geojson' do
14-
p = Project.find 'ecookbook'
15-
p.update_column :geom, test_geom
16-
17-
j = p.as_geojson include_properties: true
17+
j = @p.as_geojson include_properties: true
1818
assert_geojson j
19-
assert_equal p.id, j['properties']['id']
20-
assert_equal p.identifier, j['properties']['identifier']
21-
assert_equal p.name, j['properties']['name']
19+
assert_equal @p.id, j['properties']['id']
20+
assert_equal @p.identifier, j['properties']['identifier']
21+
assert_equal @p.name, j['properties']['name']
2222

23-
j = p.as_geojson include_properties: { only: [:id, :identifier] }
23+
j = @p.as_geojson include_properties: { only: [:id, :identifier] }
2424
assert_geojson j
25-
assert_equal p.id, j['properties']['id']
26-
assert_equal p.identifier, j['properties']['identifier']
25+
assert_equal @p.id, j['properties']['id']
26+
assert_equal @p.identifier, j['properties']['identifier']
2727
assert_nil j['properties']['name']
2828
end
2929

test/unit/user_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ class UserTest < GttTest
55

66
setup do
77
@user = User.find_by_login 'dlopper'
8-
@user.update_column :geom, test_geom
8+
@user.update_attribute :geojson, test_geojson
9+
end
10+
11+
test 'should set geom from json' do
12+
assert @user.geom.present?
913
end
1014

1115
test 'should have geojson attribute' do

0 commit comments

Comments
 (0)