Skip to content

Commit 7b414bd

Browse files
committed
issue json generation simplification and test fix
1 parent 77d3e2a commit 7b414bd

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

lib/redmine_gtt_print/issue_to_json.rb

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
# frozen_string_literal: true
2+
13
module RedmineGttPrint
24

35
# Transforms the given issue into JSON ready to be sent to the mapfish print
46
# server.
57
#
68
class IssueToJson
7-
def initialize(issue, layout, other_attributes = {}, custom_fields = {}, attachments = [])
9+
def initialize(issue, layout, other_attributes = {})
810
@issue = issue
911
@layout = layout
10-
@attachments = attachments
11-
@custom_fields = custom_fields
1212
@other_attributes = other_attributes
1313
end
1414

@@ -17,22 +17,11 @@ def self.call(*_)
1717
end
1818

1919
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-
3320
json = {
3421
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))
3625
}
3726

3827
if data = @issue.geodata_for_print
@@ -42,25 +31,35 @@ def call
4231
json.to_json
4332
end
4433

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+
4542
# the following static helpers are used by IssuesToJson as well
4643

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+
4847
{
4948
id: issue.id,
5049
subject: issue.subject,
5150
project_id: issue.project_id,
52-
project_name: (Project.find issue.project_id).name,
51+
project_name: issue.project.name,
5352
tracker_id: issue.tracker_id,
54-
tracker_name: (Tracker.find issue.tracker_id).name,
53+
tracker_name: issue.tracker.name,
5554
status_id: issue.status_id,
56-
status_name: (IssueStatus.find issue.status_id).name,
55+
status_name: issue.status.name,
5756
priority_id: issue.priority_id,
58-
priority_name: (IssuePriority.find issue.priority_id).name,
57+
priority_name: issue.priority.name,
5958
# category_id: issue.category_id,
6059
author_id: issue.author_id,
61-
author_name: (User.find issue.author_id).name,
60+
author_name: issue.author.name,
6261
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 || "",
6463
description: issue.description,
6564
is_private: issue.is_private,
6665
start_date: issue.start_date,
@@ -69,23 +68,23 @@ def self.attributes_hash(issue, other_attributes, custom_fields, attachments)
6968
estimated_hours: issue.estimated_hours,
7069
created_on: issue.created_on,
7170
updated_on: issue.updated_on,
72-
last_notes: issue.last_notes ? issue.last_notes : "",
71+
last_notes: issue.last_notes || "",
7372

7473
# Custom text
7574
custom_text: other_attributes[:custom_text],
7675

7776
# 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["現地住所"] || "",
8382

8483
# 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] || "",
8988

9089
# Experimental
9190
# issue: issue,
@@ -124,6 +123,14 @@ def self.attributes_hash(issue, other_attributes, custom_fields, attachments)
124123
}
125124
end
126125

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+
127134
def self.map_data(center, features)
128135
{
129136
center: center,

test/unit/issue_to_json_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class IssueToJsonTest < ActiveSupport::TestCase
1313
:custom_fields,
1414
:custom_values,
1515
:custom_fields_trackers,
16-
:attachments
16+
:attachments,
17+
:users,
18+
:email_addresses
1719

1820
setup do
1921
@issue = Issue.find 1

0 commit comments

Comments
 (0)