Skip to content

Commit 0667b26

Browse files
committed
merges custom comment support
2 parents 04cb94a + 7dc4d6d commit 0667b26

File tree

11 files changed

+91
-54
lines changed

11 files changed

+91
-54
lines changed

app/controllers/gtt_print_jobs_controller.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ class GttPrintJobsController < ApplicationController
1010
menu_item :issues
1111

1212
def create
13-
if (layout = params[:gtt_print_layout]).present?
14-
if @issue
15-
@result = RedmineGttPrint.mapfish.print_issue @issue, layout
16-
elsif @issues
17-
@result = RedmineGttPrint.mapfish.print_issues @issues, layout
18-
end
13+
job = GttPrintJob.new gtt_print_job_params
14+
job.issue = @issue
15+
job.issues = @issues
16+
if job.valid?
17+
@result = RedmineGttPrint.mapfish.print job
1918
render status: (@result&.success? ? :created : 422)
2019
else
21-
render_404
20+
render status: 422
2221
end
2322
end
2423

@@ -45,6 +44,10 @@ def show
4544

4645
private
4746

47+
def gtt_print_job_params
48+
params[:gtt_print_job].permit(:layout, :custom_text)
49+
end
50+
4851
def authorize_create
4952
if @project
5053
authorize

app/models/gtt_print_job.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
class GttPrintJob
4+
include ActiveModel::Model
5+
6+
attr_accessor :layout, :custom_text, :issue, :issues
7+
8+
validates :layout, presence: true
9+
validates :issue, presence: true, if: ->{ issues.nil? }
10+
validates :issues, presence: true, if: ->{ issue.nil? }
11+
12+
def list?
13+
issue.nil? && issues.present?
14+
end
15+
16+
def json
17+
if list?
18+
RedmineGttPrint::IssuesToJson.(issues, layout, custom_text: custom_text)
19+
else
20+
RedmineGttPrint::IssueToJson.(issue, layout, custom_text: custom_text)
21+
end
22+
end
23+
24+
def print_config
25+
if list?
26+
RedmineGttPrint.list_config
27+
else
28+
RedmineGttPrint.tracker_config(issue.tracker)
29+
end
30+
end
31+
32+
def format
33+
"pdf"
34+
end
35+
end

app/views/hooks/_print_issue_form.html.erb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<% if @issue and User.current.allowed_to?(:view_gtt_print, @issue.project) and (layouts = RedmineGttPrint.layouts_for_tracker(@issue.tracker)).present? %>
22
<h3><%= l :label_gtt_print_title %></h3>
3-
<%= form_tag gtt_print_jobs_path, remote: true do |f| %>
3+
<%= form_for :gtt_print_job, url: gtt_print_jobs_path, remote: true do |f| %>
44
<%= hidden_field_tag :issue_id, @issue.id %>
5-
<%= text_area_tag :gtt_print_custom_text %><br>
6-
<%= select_tag :gtt_print_layout, options_from_collection_for_select(layouts, :to_s, :to_s) %>
5+
<%= f.text_area :custom_text %><br>
6+
<%= f.select :layout, options_from_collection_for_select(layouts, :to_s, :to_s) %>
77
<button type="submit"><%= l :button_gtt_print_submit %></button>
88
<% end %>
99
<% end %>
@@ -15,10 +15,10 @@
1515
%>
1616

1717
<h3><%= l :label_gtt_print_title %></h3>
18-
<%= form_tag gtt_print_jobs_path, remote: true do |f| %>
18+
<%= form_for :gtt_print_job, url: gtt_print_jobs_path, remote: true do |f| %>
1919
<%= hidden_field_tag :issue_ids, @issues.map(&:id).join(',') %>
20-
<%= text_area_tag :gtt_print_custom_text %><br>
21-
<%= select_tag :gtt_print_layout, options_from_collection_for_select(layouts, :to_s, :to_s) %>
20+
<%= f.text_area :custom_text %><br>
21+
<%= f.select :layout, options_from_collection_for_select(layouts, :to_s, :to_s) %>
2222
<button type="submit"><%= l :button_gtt_print_submit %></button>
2323
<% end %>
2424

lib/redmine_gtt_print/issue_to_json.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ module RedmineGttPrint
44
# server.
55
#
66
class IssueToJson
7-
def initialize(issue, layout)
7+
def initialize(issue, layout, other_attributes = {})
88
@issue = issue
99
@layout = layout
10+
@other_attributes = other_attributes
1011
end
1112

12-
def self.call(issue, layout)
13-
new(issue, layout).call
13+
def self.call(*_)
14+
new(*_).call
1415
end
1516

1617
def call
1718
json = {
1819
layout: @layout,
19-
attributes: self.class.attributes_hash(@issue)
20+
attributes: self.class.attributes_hash(@issue, @other_attributes)
2021
}
2122

2223
if data = @issue.geodata_for_print
@@ -28,7 +29,7 @@ def call
2829

2930
# the following static helpers are used by IssuesToJson as well
3031

31-
def self.attributes_hash(issue)
32+
def self.attributes_hash(issue, other_attributes)
3233
{
3334
id: issue.id,
3435
subject: issue.subject,
@@ -55,6 +56,9 @@ def self.attributes_hash(issue)
5556
created_on: issue.created_on,
5657
updated_on: issue.updated_on,
5758

59+
# Custom text
60+
# custom_text: other_attributes[:custom_text]
61+
5862
# Experimental
5963
# issue: issue,
6064
# project: (Project.find issue.project_id),

lib/redmine_gtt_print/issues_to_json.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ module RedmineGttPrint
44
# mapfish print server
55
#
66
class IssuesToJson
7-
def initialize(issues, layout)
7+
def initialize(issues, layout, other_attributes = {})
88
@issues = issues
99
@layout = layout
10+
@other_attributes = other_attributes
1011
end
1112

12-
def self.call(issues, layout)
13-
new(issues, layout).call
13+
def self.call(*_)
14+
new(*_).call
1415
end
1516

1617
def call
1718
hsh = {
1819
layout: @layout,
1920
attributes: {
20-
issues: @issues.map{|i| IssueToJson.attributes_hash(i)}
21+
issues: @issues.map{|i|
22+
IssueToJson.attributes_hash(i, @other_attributes)
23+
}
2124
}
2225
}
2326

lib/redmine_gtt_print/mapfish.rb

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module RedmineGttPrint
24
class Mapfish
35

@@ -27,6 +29,14 @@ def get_capabilities(print_config)
2729
end
2830
end
2931

32+
def print(job)
33+
if ref = request_print(job.json, job.print_config, job.format)
34+
CreateJobResult.new success: true, ref: ref
35+
else
36+
CreateJobResult.new
37+
end
38+
end
39+
3040
# {"done"=>true, "status"=>"finished", "elapsedTime"=>6588, "waitingTime"=>0, "downloadURL"=>"/mapfish/print/report/a790a8e0-d2b9-4f27-8a83-d58a70b66568@36419944-3e1d-4b17-9aab-f56aec338242"}
3141
def get_status(ref)
3242
r = HTTParty.get "#{@host}/print/status/#{ref}.json"
@@ -49,25 +59,6 @@ def get_print(ref)
4959
end
5060
end
5161

52-
def print_issue(issue, layout, format: 'pdf')
53-
json = IssueToJson.(issue, layout)
54-
print_config = RedmineGttPrint.tracker_config(issue.tracker)
55-
if ref = request_print(json, print_config, format)
56-
CreateJobResult.new success: true, ref: ref
57-
else
58-
CreateJobResult.new
59-
end
60-
end
61-
62-
def print_issues(issues, layout, format: 'pdf')
63-
json = IssuesToJson.(issues, layout)
64-
if ref = request_print(json, RedmineGttPrint.list_config, format)
65-
CreateJobResult.new success: true, ref: ref
66-
else
67-
CreateJobResult.new
68-
end
69-
end
70-
7162
private
7263

7364
def request_print(json, print_config, format)

test/integration/issue_print_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ def setup
3333
def test_should_create_print_job
3434
get "/issues/#{@issue.id}"
3535
assert_response :success
36-
assert_select '#gtt_print_layout option', text: "A4 portrait", count: 0
36+
assert_select '#gtt_print_job_layout option', text: "A4 portrait", count: 0
3737

3838
log_user 'jsmith', 'jsmith'
3939
get '/projects/ecookbook/issues/new'
4040
assert_response :success
4141

4242
get "/issues/#{@issue.id}"
4343
assert_response :success
44-
assert_select '#gtt_print_layout option', text: "A4 portrait"
44+
assert_select '#gtt_print_job_layout option', text: "A4 portrait"
4545

4646
xhr :post, "/gtt_print_jobs", { issue_id: @issue.id,
47-
gtt_print_layout: "A4 portrait" }
47+
gtt_print_job: { layout: "A4 portrait" } }
4848
assert_response :created
4949

5050
assert_equal @issue, @mapfish.issue
51-
assert_equal 'A4 portrait', @mapfish.template
51+
assert_equal 'A4 portrait', @mapfish.layout
5252
end
5353

5454
def test_should_check_job_status

test/integration/mapfish_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ class MapfishTest < ActionDispatch::IntegrationTest
3636
test "should issue print job" do
3737
i = @issue
3838
i.update_attribute :geojson, test_geojson
39+
job = GttPrintJob.new issue: i, layout: 'A4 portrait'
3940

40-
assert r = @mapfish.print_issue(i, 'A4 portrait')
41+
assert r = @mapfish.print(job)
4142
assert r.success?
4243
assert ref = r.ref
4344
sleep 3

test/test_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require File.expand_path(File.dirname(__FILE__) + '/../../redmine_gtt/test/test_helper')
22

33
class TestMapfish
4-
attr_reader :issue, :template
4+
attr_reader :issue, :layout
55
def print_configs
66
['test-template']
77
end
@@ -25,9 +25,9 @@ def printjob_ready(name)
2525
@ready_jobs << name
2626
end
2727

28-
def print_issue(issue, template)
29-
@issue = issue
30-
@template = template
28+
def print(job)
29+
@issue = job.issue
30+
@layout = job.layout
3131

3232
Result.new(success: true, ref: 'some-job')
3333
end

test/unit/issue_to_json_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class IssueToJsonTest < ActiveSupport::TestCase
2424
assert j = RedmineGttPrint::IssueToJson.(@issue, 'das layout')
2525
assert h = JSON.parse(j)
2626
assert_equal 'das layout', h['layout']
27-
assert_equal @issue.subject, h['attributes']['title']
27+
assert_equal @issue.subject, h['attributes']['subject']
2828
assert map = h['attributes']['map']
2929
assert_equal 2, map['center'].size
3030
assert geo = map['layers'][0]['geoJson']
@@ -40,7 +40,7 @@ class IssueToJsonTest < ActiveSupport::TestCase
4040
i = Issue.find(2)
4141
assert j = RedmineGttPrint::IssueToJson.(i, 'layout')
4242
assert h = JSON.parse(j)
43-
assert_equal i.subject, h['attributes']['title']
43+
assert_equal i.subject, h['attributes']['subject']
4444
assert_nil h['attributes']['map']
4545
end
4646

0 commit comments

Comments
 (0)