1
+ # frozen_string_literal: true
2
+
1
3
module RedmineGttPrint
2
4
3
5
# Transforms the given issue into JSON ready to be sent to the mapfish print
4
6
# server.
5
7
#
6
8
class IssueToJson
7
- def initialize ( issue , layout , other_attributes = { } , custom_fields = { } , attachments = [ ] )
9
+ def initialize ( issue , layout , other_attributes = { } )
8
10
@issue = issue
9
11
@layout = layout
10
- @attachments = attachments
11
- @custom_fields = custom_fields
12
12
@other_attributes = other_attributes
13
13
end
14
14
@@ -17,22 +17,11 @@ def self.call(*_)
17
17
end
18
18
19
19
def call
20
- # makes custom fields accessible by name
21
- @issue . visible_custom_field_values . each do |cfv |
22
- @custom_fields . store ( cfv . custom_field . name , cfv )
23
- end
24
-
25
- # collects image attachment URL's
26
- @issue . attachments . each do |a |
27
- if a . image?
28
- # TODO: url construction doesn't look safe
29
- @attachments << "#{ Setting . protocol } ://#{ Setting . host_name } /attachments/download/#{ a . id } /#{ a . filename } "
30
- end
31
- end
32
-
33
20
json = {
34
21
layout : @layout ,
35
- attributes : self . class . attributes_hash ( @issue , @other_attributes , @custom_fields , @attachments )
22
+ attributes : self . class . attributes_hash ( @issue ,
23
+ @other_attributes ,
24
+ image_urls ( @issue ) )
36
25
}
37
26
38
27
if data = @issue . geodata_for_print
@@ -42,25 +31,35 @@ def call
42
31
json . to_json
43
32
end
44
33
34
+ def image_urls ( issue )
35
+ issue . attachments . map do |a |
36
+ if a . image?
37
+ "#{ Setting . protocol } ://#{ Setting . host_name } /attachments/download/#{ a . id } /#{ a . filename } "
38
+ end
39
+ end . compact
40
+ end
41
+
45
42
# the following static helpers are used by IssuesToJson as well
46
43
47
- def self . attributes_hash ( issue , other_attributes , custom_fields , attachments )
44
+ def self . attributes_hash ( issue , other_attributes , image_urls )
45
+ custom_fields = issue_custom_fields_by_name issue
46
+
48
47
{
49
48
id : issue . id ,
50
49
subject : issue . subject ,
51
50
project_id : issue . project_id ,
52
- project_name : ( Project . find issue . project_id ) . name ,
51
+ project_name : issue . project . name ,
53
52
tracker_id : issue . tracker_id ,
54
- tracker_name : ( Tracker . find issue . tracker_id ) . name ,
53
+ tracker_name : issue . tracker . name ,
55
54
status_id : issue . status_id ,
56
- status_name : ( IssueStatus . find issue . status_id ) . name ,
55
+ status_name : issue . status . name ,
57
56
priority_id : issue . priority_id ,
58
- priority_name : ( IssuePriority . find issue . priority_id ) . name ,
57
+ priority_name : issue . priority . name ,
59
58
# category_id: issue.category_id,
60
59
author_id : issue . author_id ,
61
- author_name : ( User . find issue . author_id ) . name ,
60
+ author_name : issue . author . name ,
62
61
assigned_to_id : issue . assigned_to_id ,
63
- assigned_to_name : issue . assigned_to_id ? ( User . find issue . author_id ) . name : "WIP ",
62
+ assigned_to_name : issue . assigned_to &. name || " ",
64
63
description : issue . description ,
65
64
is_private : issue . is_private ,
66
65
start_date : issue . start_date ,
@@ -69,23 +68,23 @@ def self.attributes_hash(issue, other_attributes, custom_fields, attachments)
69
68
estimated_hours : issue . estimated_hours ,
70
69
created_on : issue . created_on ,
71
70
updated_on : issue . updated_on ,
72
- last_notes : issue . last_notes ? issue . last_notes : "" ,
71
+ last_notes : issue . last_notes || "" ,
73
72
74
73
# Custom text
75
74
custom_text : other_attributes [ :custom_text ] ,
76
75
77
76
# Custom fields fbased on names
78
- cf_通報者 : custom_fields [ "通報者" ] ? custom_fields [ "通報者" ] . value : "" ,
79
- cf_通報手段 : custom_fields [ "通報手段" ] ? custom_fields [ "通報手段" ] . value : "" ,
80
- cf_通報者電話番号 : custom_fields [ "通報者電話番号" ] ? custom_fields [ "通報者電話番号" ] . value : "" ,
81
- cf_通報者メールアドレス : custom_fields [ "通報者メールアドレス" ] ? custom_fields [ "通報者メールアドレス" ] . value : "" ,
82
- cf_現地住所 : custom_fields [ "現地住所" ] ? custom_fields [ "現地住所" ] . value : "" ,
77
+ cf_通報者 : custom_fields [ "通報者" ] || "" ,
78
+ cf_通報手段 : custom_fields [ "通報手段" ] || "" ,
79
+ cf_通報者電話番号 : custom_fields [ "通報者電話番号" ] || "" ,
80
+ cf_通報者メールアドレス : custom_fields [ "通報者メールアドレス" ] || "" ,
81
+ cf_現地住所 : custom_fields [ "現地住所" ] || "" ,
83
82
84
83
# Image attachments (max. 4 iamges)
85
- image_url_1 : attachments . at ( 0 ) ? attachments . at ( 0 ) : "" ,
86
- image_url_2 : attachments . at ( 1 ) ? attachments . at ( 1 ) : "" ,
87
- image_url_3 : attachments . at ( 2 ) ? attachments . at ( 2 ) : "" ,
88
- image_url_4 : attachments . at ( 3 ) ? attachments . at ( 3 ) : "" ,
84
+ image_url_1 : image_urls [ 0 ] || "" ,
85
+ image_url_2 : image_urls [ 1 ] || "" ,
86
+ image_url_3 : image_urls [ 2 ] || "" ,
87
+ image_url_4 : image_urls [ 3 ] || "" ,
89
88
90
89
# Experimental
91
90
# issue: issue,
@@ -124,6 +123,14 @@ def self.attributes_hash(issue, other_attributes, custom_fields, attachments)
124
123
}
125
124
end
126
125
126
+ def self . issue_custom_fields_by_name ( issue )
127
+ Hash [
128
+ issue . visible_custom_field_values . map { |cfv |
129
+ [ cfv . custom_field . name , cfv . value ]
130
+ }
131
+ ]
132
+ end
133
+
127
134
def self . map_data ( center , features )
128
135
{
129
136
center : center ,
0 commit comments