repolho 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +22 -1
- data/lib/repolho/main.rb +32 -0
- data/lib/repolho/version.rb +1 -1
- data/test/container_test.rb +99 -0
- data/test/fixtures/simple.rep.erb +15 -0
- data/test/main_test.rb +397 -0
- data/test/page_test.rb +84 -0
- data/test/report_test.rb +26 -0
- data/test/stats_test.rb +69 -0
- data/test/test_helper.rb +30 -0
- data.tar.gz.sig +0 -0
- metadata +36 -1
- metadata.gz.sig +4 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e241b0d81fce8a3b55de4dbf8eccfe3e34a72a95020fd6098bb501a2434257fe
|
|
4
|
+
data.tar.gz: ebb7296a6025cd82acb5461863f7599f52cfb8494184adc29fd6888004584e23
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 773100dbaed0b7d43c0b8ca8139fcbd7c925ef18515ef39d89e8131f3738bfc4edb8070188a64c40cea5ea03532d4b07f1dcfe57ac66890b88954735b1b22a51
|
|
7
|
+
data.tar.gz: 97150a6758cd926f08efbe920d5be2013cbf5e92d77f4768947086ceb0e333bb0fabe187bf624127a2417f3ed2b99f4cf233dc84029361e5e87dc857e12525ee
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/README.md
CHANGED
|
@@ -58,7 +58,7 @@ sections). Everything inside the custom elements is processed as ERB.
|
|
|
58
58
|
|
|
59
59
|
| Element | Purpose | Attributes |
|
|
60
60
|
|---------|---------|------------|
|
|
61
|
-
| `<report>` | Root container of the report | `pagination` (optional, `"true"`): enables client-side page navigation. Also accepts `pagination-first`, `pagination-prev`, `pagination-next`, `pagination-last` for custom labels. |
|
|
61
|
+
| `<report>` | Root container of the report | `pagination` (optional, `"true"`): enables client-side page navigation. Also accepts `pagination-first`, `pagination-prev`, `pagination-next`, `pagination-last` for custom labels.<br>`orientation` (optional, `"portrait"` or `"landscape"`): page orientation for print.<br>`paper` (optional, e.g. `"A4"`, `"Letter"`): paper size for print. If either is present, a `@media print` style is injected at the end of `<body>`. |
|
|
62
62
|
| `<report-page>` | Defines a page (required, only one) | `rows` (required): max data rows per page |
|
|
63
63
|
| `<report-group>` | Nested grouping | `id` (required), `attr` (required), `reprint-header-on-page-break` (optional, `"true"`) |
|
|
64
64
|
| `<report-header>` | Header of the current container | — |
|
|
@@ -300,6 +300,27 @@ Repolho::Main.new(path, data, current_template: @current_template, user: current
|
|
|
300
300
|
|
|
301
301
|
They become `@current_template` and `@user` inside the templates.
|
|
302
302
|
|
|
303
|
+
## Testing
|
|
304
|
+
|
|
305
|
+
The project uses [Minitest](https://github.com/minitest/minitest).
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
# From the gem root
|
|
309
|
+
bundle install
|
|
310
|
+
bundle exec rake test
|
|
311
|
+
# or
|
|
312
|
+
ruby -Ilib:test test/stats_test.rb
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Tests cover:
|
|
316
|
+
|
|
317
|
+
- `Stats` (sum, avg, min, max, count, reset, numeric coercion)
|
|
318
|
+
- `Container` (ERB rendering of headers/footers/rows, attribute access, stats delegation, status)
|
|
319
|
+
- `Page` (row counting via `<tr>`, page open/close, page breaks, raw append)
|
|
320
|
+
- `Report` (inheritance and composition)
|
|
321
|
+
- `Main` (data normalization for hashes / ActiveRecord-like objects / models, section extraction, full HTML generation, groupings, page breaks, pagination, print CSS for orientation/paper, previous/after ERB)
|
|
322
|
+
|
|
323
|
+
Integration tests assert on the generated HTML structure (`tbody.repolho-page`, data attributes, totals, labels).
|
|
303
324
|
|
|
304
325
|
## Signing
|
|
305
326
|
|
data/lib/repolho/main.rb
CHANGED
|
@@ -77,6 +77,8 @@ module Repolho
|
|
|
77
77
|
@after = ""
|
|
78
78
|
@body = ""
|
|
79
79
|
@pagination = false
|
|
80
|
+
@orientation = nil
|
|
81
|
+
@paper = nil
|
|
80
82
|
@pagination_labels = {
|
|
81
83
|
first: "First",
|
|
82
84
|
prev: "<",
|
|
@@ -124,6 +126,15 @@ module Repolho
|
|
|
124
126
|
end
|
|
125
127
|
end
|
|
126
128
|
|
|
129
|
+
css = print_css
|
|
130
|
+
if css
|
|
131
|
+
if after_content =~ /<\/body>/i
|
|
132
|
+
after_content = after_content.sub(/<\/body>/i, "#{css}</body>")
|
|
133
|
+
else
|
|
134
|
+
after_content += css
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
127
138
|
@page.append_raw(after_content)
|
|
128
139
|
end
|
|
129
140
|
|
|
@@ -136,6 +147,25 @@ module Repolho
|
|
|
136
147
|
@page.instance_variable_set(:@pagination_last, @pagination_labels[:last])
|
|
137
148
|
end
|
|
138
149
|
|
|
150
|
+
def print_css
|
|
151
|
+
return nil unless @orientation || @paper
|
|
152
|
+
|
|
153
|
+
size_parts = []
|
|
154
|
+
size_parts << @paper if @paper
|
|
155
|
+
size_parts << @orientation if @orientation
|
|
156
|
+
size = size_parts.join(" ")
|
|
157
|
+
|
|
158
|
+
<<~HTML
|
|
159
|
+
<style>
|
|
160
|
+
@media print {
|
|
161
|
+
@page {
|
|
162
|
+
size: #{size};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
</style>
|
|
166
|
+
HTML
|
|
167
|
+
end
|
|
168
|
+
|
|
139
169
|
def make_columns
|
|
140
170
|
return [] if @data.empty?
|
|
141
171
|
|
|
@@ -199,6 +229,8 @@ module Repolho
|
|
|
199
229
|
|
|
200
230
|
report_node = report.first
|
|
201
231
|
@pagination = report_node["pagination"] == "true"
|
|
232
|
+
@orientation = report_node["orientation"]&.downcase
|
|
233
|
+
@paper = report_node["paper"]
|
|
202
234
|
|
|
203
235
|
if @pagination
|
|
204
236
|
@pagination_labels[:first] = report_node["pagination-first"] if report_node["pagination-first"]
|
data/lib/repolho/version.rb
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "test_helper"
|
|
4
|
+
|
|
5
|
+
module Repolho
|
|
6
|
+
class ContainerTest < Minitest::Test
|
|
7
|
+
def setup
|
|
8
|
+
@stats = { "value" => Stats.new }
|
|
9
|
+
@locals = { company: "Acme" }
|
|
10
|
+
@container = Container.new("group1", @locals, @stats)
|
|
11
|
+
@page = Page.new("page", {}, { "value" => Stats.new })
|
|
12
|
+
@container.page = @page
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_locals_become_instance_variables
|
|
16
|
+
assert_equal "Acme", @container.instance_variable_get(:@company)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_sum_avg_count_min_max_delegate_to_stats
|
|
20
|
+
@stats["value"].add(10)
|
|
21
|
+
@stats["value"].add(20)
|
|
22
|
+
|
|
23
|
+
assert_equal 30.0, @container.sum(:value)
|
|
24
|
+
assert_in_delta 15.0, @container.avg(:value), 0.001
|
|
25
|
+
assert_equal 2, @container.count(:value)
|
|
26
|
+
assert_equal 10, @container.min(:value)
|
|
27
|
+
assert_equal 20, @container.max(:value)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_sum_missing_attr_returns_zero
|
|
31
|
+
assert_equal 0.0, @container.sum(:missing)
|
|
32
|
+
assert_equal 0, @container.count(:missing)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_attr_returns_current_row_value
|
|
36
|
+
@container.instance_variable_set(:@cur_row, { "id" => 42, "name" => "Test" })
|
|
37
|
+
|
|
38
|
+
assert_equal 42, @container.attr(:id)
|
|
39
|
+
assert_equal "Test", @container.attr(:name)
|
|
40
|
+
assert_nil @container.attr(:missing)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_attr_without_current_row
|
|
44
|
+
assert_equal "", @container.attr(:anything)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_start_renders_header_erb
|
|
48
|
+
@container.header = { content: "<tr><td><%= @company %></td></tr>" }
|
|
49
|
+
|
|
50
|
+
result = @container.start
|
|
51
|
+
|
|
52
|
+
assert_includes result, "Acme"
|
|
53
|
+
assert_includes result, "<tr>"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_finish_renders_footer_erb_and_increments_number
|
|
57
|
+
@container.footer = { content: "<tr><td>Total: <%= sum(:value) %></td></tr>" }
|
|
58
|
+
@stats["value"].add(100)
|
|
59
|
+
|
|
60
|
+
result = @container.finish
|
|
61
|
+
|
|
62
|
+
assert_includes result, "100"
|
|
63
|
+
assert_equal 2, @container.number
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_row_renders_template
|
|
67
|
+
@container.row_template = "<tr><td><%= attr(:id) %></td></tr>"
|
|
68
|
+
@container.instance_variable_set(:@cur_row, { "id" => 7 })
|
|
69
|
+
|
|
70
|
+
result = @container.row
|
|
71
|
+
|
|
72
|
+
assert_includes result, "7"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_row_without_template_returns_nil
|
|
76
|
+
assert_nil @container.row
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def test_force_header_reprint
|
|
80
|
+
@container.status = Container::STARTED
|
|
81
|
+
@container.force_header_reprint
|
|
82
|
+
|
|
83
|
+
assert_equal Container::FINISHED, @container.status
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def test_reset_stats
|
|
87
|
+
@stats["value"].add(50)
|
|
88
|
+
@container.reset_stats
|
|
89
|
+
|
|
90
|
+
assert_equal 0.0, @stats["value"].sum
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def test_status_constants
|
|
94
|
+
assert_equal 0, Container::UNSTARTED
|
|
95
|
+
assert_equal 1, Container::STARTED
|
|
96
|
+
assert_equal 2, Container::FINISHED
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head><title>Fixture</title></head>
|
|
4
|
+
<body>
|
|
5
|
+
<table>
|
|
6
|
+
<report>
|
|
7
|
+
<report-page rows="10">
|
|
8
|
+
<report-row>
|
|
9
|
+
<tr><td><%= attr(:id) %></td><td><%= attr(:value) %></td></tr>
|
|
10
|
+
</report-row>
|
|
11
|
+
</report-page>
|
|
12
|
+
</report>
|
|
13
|
+
</table>
|
|
14
|
+
</body>
|
|
15
|
+
</html>
|
data/test/main_test.rb
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "test_helper"
|
|
4
|
+
|
|
5
|
+
module Repolho
|
|
6
|
+
class MainTest < Minitest::Test
|
|
7
|
+
include TestHelpers
|
|
8
|
+
|
|
9
|
+
SIMPLE_TEMPLATE = <<~HTML
|
|
10
|
+
<!DOCTYPE html>
|
|
11
|
+
<html>
|
|
12
|
+
<head><title>Test</title></head>
|
|
13
|
+
<body>
|
|
14
|
+
<table>
|
|
15
|
+
<report>
|
|
16
|
+
<report-header>
|
|
17
|
+
<tr><th>Report Header <%= @company %></th></tr>
|
|
18
|
+
</report-header>
|
|
19
|
+
<report-page rows="5">
|
|
20
|
+
<report-header>
|
|
21
|
+
<tr><th>Id</th><th>Value</th></tr>
|
|
22
|
+
</report-header>
|
|
23
|
+
<report-footer>
|
|
24
|
+
<tr><td>Page total</td><td><%= sum(:value) %></td></tr>
|
|
25
|
+
</report-footer>
|
|
26
|
+
<report-row>
|
|
27
|
+
<tr>
|
|
28
|
+
<td><%= attr(:id) %></td>
|
|
29
|
+
<td><%= attr(:value) %></td>
|
|
30
|
+
</tr>
|
|
31
|
+
</report-row>
|
|
32
|
+
</report-page>
|
|
33
|
+
<report-footer>
|
|
34
|
+
<tr><td>Grand total</td><td><%= sum(:value) %></td></tr>
|
|
35
|
+
</report-footer>
|
|
36
|
+
</report>
|
|
37
|
+
</table>
|
|
38
|
+
</body>
|
|
39
|
+
</html>
|
|
40
|
+
HTML
|
|
41
|
+
|
|
42
|
+
GROUP_TEMPLATE = <<~HTML
|
|
43
|
+
<!DOCTYPE html>
|
|
44
|
+
<html>
|
|
45
|
+
<body>
|
|
46
|
+
<table>
|
|
47
|
+
<report>
|
|
48
|
+
<report-page rows="10">
|
|
49
|
+
<report-header>
|
|
50
|
+
<tr><th>Name</th><th>Value</th></tr>
|
|
51
|
+
</report-header>
|
|
52
|
+
<report-group id="state" attr="state">
|
|
53
|
+
<report-header>
|
|
54
|
+
<tr><td colspan="2">State: <%= attr(:state) %></td></tr>
|
|
55
|
+
</report-header>
|
|
56
|
+
<report-footer>
|
|
57
|
+
<tr><td>State total</td><td><%= sum(:value) %></td></tr>
|
|
58
|
+
</report-footer>
|
|
59
|
+
<report-row>
|
|
60
|
+
<tr>
|
|
61
|
+
<td><%= attr(:name) %></td>
|
|
62
|
+
<td><%= attr(:value) %></td>
|
|
63
|
+
</tr>
|
|
64
|
+
</report-row>
|
|
65
|
+
</report-group>
|
|
66
|
+
</report-page>
|
|
67
|
+
</report>
|
|
68
|
+
</table>
|
|
69
|
+
</body>
|
|
70
|
+
</html>
|
|
71
|
+
HTML
|
|
72
|
+
|
|
73
|
+
def setup
|
|
74
|
+
@data = [
|
|
75
|
+
{ "id" => 1, "name" => "Alice", "value" => 10.0, "state" => "SP" },
|
|
76
|
+
{ "id" => 2, "name" => "Bob", "value" => 20.0, "state" => "SP" },
|
|
77
|
+
{ "id" => 3, "name" => "Carol", "value" => 15.0, "state" => "RJ" }
|
|
78
|
+
]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def test_normalize_data_array_of_hashes
|
|
82
|
+
main = Main.new("/dev/null", @data)
|
|
83
|
+
normalized = main.send(:normalize_data, @data)
|
|
84
|
+
|
|
85
|
+
assert_equal 3, normalized.size
|
|
86
|
+
assert_equal "Alice", normalized[0]["name"]
|
|
87
|
+
assert_kind_of String, normalized[0].keys.first
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_normalize_data_empty
|
|
91
|
+
main = Main.new("/dev/null", [])
|
|
92
|
+
assert_empty main.send(:normalize_data, [])
|
|
93
|
+
assert_empty main.send(:normalize_data, nil)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_normalize_data_active_record_result_like
|
|
97
|
+
result = Object.new
|
|
98
|
+
rows = [{ "id" => 1, "value" => 100 }]
|
|
99
|
+
result.define_singleton_method(:columns) { %w[id value] }
|
|
100
|
+
result.define_singleton_method(:each) { |&b| rows.each(&b) }
|
|
101
|
+
result.define_singleton_method(:map) { |&b| rows.map(&b) }
|
|
102
|
+
|
|
103
|
+
main = Main.new("/dev/null", result)
|
|
104
|
+
normalized = main.send(:normalize_data, result)
|
|
105
|
+
|
|
106
|
+
assert_equal 1, normalized.size
|
|
107
|
+
assert_equal 100, normalized[0]["value"]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def test_normalize_data_models_with_attributes
|
|
111
|
+
model = Object.new
|
|
112
|
+
model.define_singleton_method(:attributes) { { "id" => 5, "name" => "X" } }
|
|
113
|
+
|
|
114
|
+
main = Main.new("/dev/null", [model])
|
|
115
|
+
normalized = main.send(:normalize_data, [model])
|
|
116
|
+
|
|
117
|
+
assert_equal "X", normalized[0]["name"]
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_extract_sections
|
|
121
|
+
template = write_temp_template(SIMPLE_TEMPLATE)
|
|
122
|
+
main = Main.new(template.path, @data, { company: "Acme" })
|
|
123
|
+
main.send(:parse)
|
|
124
|
+
|
|
125
|
+
assert_includes main.instance_variable_get(:@previous), "<!DOCTYPE html>"
|
|
126
|
+
assert_includes main.instance_variable_get(:@body), "<body>"
|
|
127
|
+
assert_includes main.instance_variable_get(:@report_xml), "<report>"
|
|
128
|
+
assert_includes main.instance_variable_get(:@after), "</html>"
|
|
129
|
+
ensure
|
|
130
|
+
template&.unlink
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def test_simple_report_html_structure
|
|
134
|
+
# Use higher row limit so headers/footers + 3 data rows fit in one page
|
|
135
|
+
template_content = SIMPLE_TEMPLATE.gsub('rows="5"', 'rows="20"')
|
|
136
|
+
template = write_temp_template(template_content)
|
|
137
|
+
main = Main.new(template.path, @data, { company: "Acme" })
|
|
138
|
+
main.run
|
|
139
|
+
html = main.content.join
|
|
140
|
+
doc = html_doc(html)
|
|
141
|
+
|
|
142
|
+
assert_includes html, "Acme"
|
|
143
|
+
assert_equal 1, page_tbodies(doc).size
|
|
144
|
+
assert_includes html, "1"
|
|
145
|
+
assert_includes html, "10"
|
|
146
|
+
assert_includes html, "Grand total"
|
|
147
|
+
# 10+20+15 = 45
|
|
148
|
+
assert_includes html, "45"
|
|
149
|
+
ensure
|
|
150
|
+
template&.unlink
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def test_group_breaks_on_attribute_change
|
|
154
|
+
template = write_temp_template(GROUP_TEMPLATE)
|
|
155
|
+
main = Main.new(template.path, @data)
|
|
156
|
+
main.run
|
|
157
|
+
html = main.content.join
|
|
158
|
+
|
|
159
|
+
assert_includes html, "State: SP"
|
|
160
|
+
assert_includes html, "State: RJ"
|
|
161
|
+
# SP total 30, RJ total 15
|
|
162
|
+
assert_includes html, "30"
|
|
163
|
+
assert_includes html, "15"
|
|
164
|
+
ensure
|
|
165
|
+
template&.unlink
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def test_page_break_with_small_row_limit
|
|
169
|
+
small_page = SIMPLE_TEMPLATE.gsub('rows="5"', 'rows="2"')
|
|
170
|
+
template = write_temp_template(small_page)
|
|
171
|
+
main = Main.new(template.path, @data, { company: "Test" })
|
|
172
|
+
main.run
|
|
173
|
+
html = main.content.join
|
|
174
|
+
doc = html_doc(html)
|
|
175
|
+
|
|
176
|
+
pages = page_tbodies(doc)
|
|
177
|
+
assert pages.size >= 2, "Expected multiple pages, got #{pages.size}"
|
|
178
|
+
assert_includes html, 'data-page="1"'
|
|
179
|
+
assert_includes html, 'data-page="2"'
|
|
180
|
+
ensure
|
|
181
|
+
template&.unlink
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def test_pagination_enabled_when_multiple_pages
|
|
185
|
+
paginated = SIMPLE_TEMPLATE.gsub("<report>", '<report pagination="true">').gsub('rows="5"', 'rows="2"')
|
|
186
|
+
template = write_temp_template(paginated)
|
|
187
|
+
main = Main.new(template.path, @data)
|
|
188
|
+
main.run
|
|
189
|
+
html = main.content.join
|
|
190
|
+
|
|
191
|
+
assert_includes html, 'id="report-pagination"'
|
|
192
|
+
assert_includes html, "change_report_page"
|
|
193
|
+
assert_includes html, "First"
|
|
194
|
+
ensure
|
|
195
|
+
template&.unlink
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def test_pagination_omitted_for_single_page
|
|
199
|
+
# High row limit guarantees a single page even with headers/footers
|
|
200
|
+
paginated = SIMPLE_TEMPLATE.gsub("<report>", '<report pagination="true">').gsub('rows="5"', 'rows="50"')
|
|
201
|
+
template = write_temp_template(paginated)
|
|
202
|
+
main = Main.new(template.path, @data)
|
|
203
|
+
main.run
|
|
204
|
+
html = main.content.join
|
|
205
|
+
|
|
206
|
+
refute_includes html, 'id="report-pagination"'
|
|
207
|
+
ensure
|
|
208
|
+
template&.unlink
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def test_custom_pagination_labels
|
|
212
|
+
paginated = <<~HTML
|
|
213
|
+
<!DOCTYPE html>
|
|
214
|
+
<html><body>
|
|
215
|
+
<table>
|
|
216
|
+
<report pagination="true" pagination-first="Primeira" pagination-last="Última" pagination-prev="«" pagination-next="»">
|
|
217
|
+
<report-page rows="2">
|
|
218
|
+
<report-row><tr><td><%= attr(:id) %></td></tr></report-row>
|
|
219
|
+
</report-page>
|
|
220
|
+
</report>
|
|
221
|
+
</table>
|
|
222
|
+
</body></html>
|
|
223
|
+
HTML
|
|
224
|
+
template = write_temp_template(paginated)
|
|
225
|
+
main = Main.new(template.path, @data)
|
|
226
|
+
main.run
|
|
227
|
+
html = main.content.join
|
|
228
|
+
|
|
229
|
+
assert_includes html, "Primeira"
|
|
230
|
+
assert_includes html, "Última"
|
|
231
|
+
assert_includes html, "«"
|
|
232
|
+
ensure
|
|
233
|
+
template&.unlink
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def test_orientation_and_paper_inject_print_css
|
|
237
|
+
oriented = SIMPLE_TEMPLATE.gsub("<report>", '<report orientation="landscape" paper="A4">')
|
|
238
|
+
template = write_temp_template(oriented)
|
|
239
|
+
main = Main.new(template.path, @data)
|
|
240
|
+
main.run
|
|
241
|
+
html = main.content.join
|
|
242
|
+
|
|
243
|
+
assert_includes html, "@media print"
|
|
244
|
+
assert_includes html, "size: A4 landscape"
|
|
245
|
+
ensure
|
|
246
|
+
template&.unlink
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def test_previous_and_after_are_rendered
|
|
250
|
+
# previous (before <body>) and after (after </report>) are ERB-processed.
|
|
251
|
+
# Content between <body> and <report> is stored in @body and inserted raw.
|
|
252
|
+
template_content = <<~HTML
|
|
253
|
+
<!DOCTYPE html>
|
|
254
|
+
<html>
|
|
255
|
+
<head><title><%= @title %></title></head>
|
|
256
|
+
<body>
|
|
257
|
+
<p>Before raw</p>
|
|
258
|
+
<table>
|
|
259
|
+
<report>
|
|
260
|
+
<report-page rows="10">
|
|
261
|
+
<report-row><tr><td><%= attr(:id) %></td></tr></report-row>
|
|
262
|
+
</report-page>
|
|
263
|
+
</report>
|
|
264
|
+
</table>
|
|
265
|
+
<p>After <%= @company %></p>
|
|
266
|
+
</body>
|
|
267
|
+
</html>
|
|
268
|
+
HTML
|
|
269
|
+
template = write_temp_template(template_content)
|
|
270
|
+
main = Main.new(template.path, @data, { title: "My Report", company: "Acme" })
|
|
271
|
+
main.run
|
|
272
|
+
html = main.content.join
|
|
273
|
+
|
|
274
|
+
assert_includes html, "My Report"
|
|
275
|
+
assert_includes html, "Before raw"
|
|
276
|
+
assert_includes html, "After Acme"
|
|
277
|
+
ensure
|
|
278
|
+
template&.unlink
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def test_reprint_header_on_page_break_attribute
|
|
282
|
+
reprint = GROUP_TEMPLATE.gsub(
|
|
283
|
+
'<report-group id="state" attr="state">',
|
|
284
|
+
'<report-group id="state" attr="state" reprint-header-on-page-break="true">'
|
|
285
|
+
)
|
|
286
|
+
template = write_temp_template(reprint)
|
|
287
|
+
main = Main.new(template.path, @data)
|
|
288
|
+
main.send(:parse)
|
|
289
|
+
|
|
290
|
+
groups = main.instance_variable_get(:@report).containers
|
|
291
|
+
# The page has the group as container
|
|
292
|
+
group = groups.first.containers.first
|
|
293
|
+
assert group.reprint_header_on_page_break
|
|
294
|
+
ensure
|
|
295
|
+
template&.unlink
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def test_group_header_reprinted_on_page_break
|
|
299
|
+
# Force a page break in the middle of the same group
|
|
300
|
+
data = [
|
|
301
|
+
{ "id" => 1, "name" => "Alice", "value" => 10.0, "state" => "SP" },
|
|
302
|
+
{ "id" => 2, "name" => "Bob", "value" => 20.0, "state" => "SP" },
|
|
303
|
+
{ "id" => 3, "name" => "Carol", "value" => 15.0, "state" => "SP" },
|
|
304
|
+
{ "id" => 4, "name" => "Dave", "value" => 25.0, "state" => "SP" },
|
|
305
|
+
{ "id" => 5, "name" => "Eve", "value" => 30.0, "state" => "SP" }
|
|
306
|
+
]
|
|
307
|
+
|
|
308
|
+
template_content = GROUP_TEMPLATE.gsub(
|
|
309
|
+
'<report-page rows="10">',
|
|
310
|
+
'<report-page rows="3">'
|
|
311
|
+
).gsub(
|
|
312
|
+
'<report-group id="state" attr="state">',
|
|
313
|
+
'<report-group id="state" attr="state" reprint-header-on-page-break="true">'
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
template = write_temp_template(template_content)
|
|
317
|
+
main = Main.new(template.path, data)
|
|
318
|
+
main.run
|
|
319
|
+
html = main.content.join
|
|
320
|
+
doc = html_doc(html)
|
|
321
|
+
|
|
322
|
+
pages = page_tbodies(doc)
|
|
323
|
+
assert pages.size >= 2, "Expected page break, got #{pages.size} page(s)"
|
|
324
|
+
|
|
325
|
+
# Group header should appear more than once because of reprint
|
|
326
|
+
header_count = html.scan(/State: SP/).size
|
|
327
|
+
assert header_count >= 2, "Expected group header to be reprinted, found #{header_count} time(s)"
|
|
328
|
+
ensure
|
|
329
|
+
template&.unlink
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def test_page_break_across_groups
|
|
333
|
+
# Page break happens while groups are changing
|
|
334
|
+
data = [
|
|
335
|
+
{ "id" => 1, "name" => "Alice", "value" => 10.0, "state" => "SP" },
|
|
336
|
+
{ "id" => 2, "name" => "Bob", "value" => 20.0, "state" => "SP" },
|
|
337
|
+
{ "id" => 3, "name" => "Carol", "value" => 15.0, "state" => "RJ" },
|
|
338
|
+
{ "id" => 4, "name" => "Dave", "value" => 25.0, "state" => "RJ" },
|
|
339
|
+
{ "id" => 5, "name" => "Eve", "value" => 30.0, "state" => "MG" }
|
|
340
|
+
]
|
|
341
|
+
|
|
342
|
+
template_content = GROUP_TEMPLATE.gsub(
|
|
343
|
+
'<report-page rows="10">',
|
|
344
|
+
'<report-page rows="2">'
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
template = write_temp_template(template_content)
|
|
348
|
+
main = Main.new(template.path, data)
|
|
349
|
+
main.run
|
|
350
|
+
html = main.content.join
|
|
351
|
+
doc = html_doc(html)
|
|
352
|
+
|
|
353
|
+
pages = page_tbodies(doc)
|
|
354
|
+
assert pages.size >= 2
|
|
355
|
+
|
|
356
|
+
assert_includes html, "State: SP"
|
|
357
|
+
assert_includes html, "State: RJ"
|
|
358
|
+
assert_includes html, "State: MG"
|
|
359
|
+
ensure
|
|
360
|
+
template&.unlink
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def test_make_columns_detects_numeric
|
|
364
|
+
main = Main.new("/dev/null", @data)
|
|
365
|
+
columns = main.send(:make_columns)
|
|
366
|
+
|
|
367
|
+
assert_includes columns, "id"
|
|
368
|
+
assert_includes columns, "value"
|
|
369
|
+
refute_includes columns, "name"
|
|
370
|
+
refute_includes columns, "state"
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def test_page_number_available_in_after_erb
|
|
374
|
+
after_erb = <<~HTML
|
|
375
|
+
<!DOCTYPE html>
|
|
376
|
+
<html><body>
|
|
377
|
+
<table>
|
|
378
|
+
<report>
|
|
379
|
+
<report-page rows="2">
|
|
380
|
+
<report-row><tr><td><%= attr(:id) %></td></tr></report-row>
|
|
381
|
+
</report-page>
|
|
382
|
+
</report>
|
|
383
|
+
</table>
|
|
384
|
+
<p>Total pages: <%= page_number %></p>
|
|
385
|
+
</body></html>
|
|
386
|
+
HTML
|
|
387
|
+
template = write_temp_template(after_erb)
|
|
388
|
+
main = Main.new(template.path, @data)
|
|
389
|
+
main.run
|
|
390
|
+
html = main.content.join
|
|
391
|
+
|
|
392
|
+
assert_match(/Total pages: \d+/, html)
|
|
393
|
+
ensure
|
|
394
|
+
template&.unlink
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
end
|
data/test/page_test.rb
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "test_helper"
|
|
4
|
+
|
|
5
|
+
module Repolho
|
|
6
|
+
class PageTest < Minitest::Test
|
|
7
|
+
def setup
|
|
8
|
+
@stats = { "value" => Stats.new }
|
|
9
|
+
@page = Page.new("page", { title: "Report" }, @stats)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_default_rows
|
|
13
|
+
assert_equal 60, @page.rows
|
|
14
|
+
assert_equal 1, @page.row_number
|
|
15
|
+
assert_equal 1, @page.page_number
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_rows_setter_updates_valid_rows
|
|
19
|
+
@page.rows = 20
|
|
20
|
+
|
|
21
|
+
assert_equal 20, @page.rows
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_append_raw_adds_content_without_counting
|
|
25
|
+
@page.append_raw("<html><body>")
|
|
26
|
+
|
|
27
|
+
assert_includes @page.content, "<html><body>"
|
|
28
|
+
assert_equal 1, @page.row_number # unchanged
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_shovel_counts_tr_and_opens_page
|
|
32
|
+
@page << "<tr><td>one</td></tr><tr><td>two</td></tr>"
|
|
33
|
+
|
|
34
|
+
assert_equal 3, @page.row_number # started at 1 + 2
|
|
35
|
+
assert_equal 2, @page.content.size # open_page tag + content
|
|
36
|
+
assert_includes @page.content.join, 'class="repolho-page"'
|
|
37
|
+
assert_includes @page.content.join, 'data-page="1"'
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_shovel_nil_is_ignored
|
|
41
|
+
@page << nil
|
|
42
|
+
|
|
43
|
+
assert_empty @page.content
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_open_and_close_page
|
|
47
|
+
@page.send(:open_page)
|
|
48
|
+
assert_includes @page.content.join, 'data-page="1"'
|
|
49
|
+
|
|
50
|
+
@page.send(:close_page)
|
|
51
|
+
assert_includes @page.content.join, "</tbody>"
|
|
52
|
+
assert_equal 2, @page.page_number
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_finalize_closes_open_page
|
|
56
|
+
@page << "<tr><td>x</td></tr>"
|
|
57
|
+
@page.finalize
|
|
58
|
+
|
|
59
|
+
assert_includes @page.content.join, "</tbody>"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_page_break_when_row_limit_reached
|
|
63
|
+
@page.rows = 3
|
|
64
|
+
@page.page = @page # required so << works inside check_page_break
|
|
65
|
+
# Force row_number high enough
|
|
66
|
+
@page.instance_variable_set(:@row_number, 3)
|
|
67
|
+
@page.footer = { content: "<tr><td>footer</td></tr>" }
|
|
68
|
+
|
|
69
|
+
@page.send(:check_page_break)
|
|
70
|
+
|
|
71
|
+
assert_equal Container::FINISHED, @page.status
|
|
72
|
+
assert_equal 0, @page.row_number
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_content_is_array_of_strings
|
|
76
|
+
@page.append_raw("before")
|
|
77
|
+
@page << "<tr><td>row</td></tr>"
|
|
78
|
+
@page.finalize
|
|
79
|
+
|
|
80
|
+
assert_kind_of Array, @page.content
|
|
81
|
+
assert @page.content.all? { |c| c.is_a?(String) }
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
data/test/report_test.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "test_helper"
|
|
4
|
+
|
|
5
|
+
module Repolho
|
|
6
|
+
class ReportTest < Minitest::Test
|
|
7
|
+
def test_report_is_a_container
|
|
8
|
+
stats = { "value" => Stats.new }
|
|
9
|
+
report = Report.new("report", {}, stats)
|
|
10
|
+
|
|
11
|
+
assert_kind_of Container, report
|
|
12
|
+
assert_equal "report", report.id
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_report_can_hold_page
|
|
16
|
+
stats = { "value" => Stats.new }
|
|
17
|
+
report = Report.new("report", {}, stats)
|
|
18
|
+
page = Page.new("page", {}, stats)
|
|
19
|
+
report.containers << page
|
|
20
|
+
report.page = page
|
|
21
|
+
|
|
22
|
+
assert_equal page, report.containers.first
|
|
23
|
+
assert_equal page, report.page
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
data/test/stats_test.rb
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "test_helper"
|
|
4
|
+
|
|
5
|
+
module Repolho
|
|
6
|
+
class StatsTest < Minitest::Test
|
|
7
|
+
def setup
|
|
8
|
+
@stats = Stats.new
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_initial_state
|
|
12
|
+
assert_equal 0.0, @stats.sum
|
|
13
|
+
assert_equal 0, @stats.count
|
|
14
|
+
assert_nil @stats.min
|
|
15
|
+
assert_nil @stats.max
|
|
16
|
+
assert_equal 0.0, @stats.avg
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_add_numeric
|
|
20
|
+
@stats.add(10)
|
|
21
|
+
@stats.add(20.5)
|
|
22
|
+
|
|
23
|
+
assert_equal 30.5, @stats.sum
|
|
24
|
+
assert_equal 2, @stats.count
|
|
25
|
+
assert_equal 10, @stats.min
|
|
26
|
+
assert_equal 20.5, @stats.max
|
|
27
|
+
assert_in_delta 15.25, @stats.avg, 0.001
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_add_string_numeric
|
|
31
|
+
@stats.add("15")
|
|
32
|
+
@stats.add("-5.5")
|
|
33
|
+
|
|
34
|
+
assert_equal 9.5, @stats.sum
|
|
35
|
+
assert_equal 2, @stats.count
|
|
36
|
+
assert_equal(-5.5, @stats.min)
|
|
37
|
+
assert_equal 15.0, @stats.max
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_add_nil_is_ignored
|
|
41
|
+
@stats.add(nil)
|
|
42
|
+
@stats.add(10)
|
|
43
|
+
|
|
44
|
+
assert_equal 10.0, @stats.sum
|
|
45
|
+
assert_equal 1, @stats.count
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_avg_zero_count
|
|
49
|
+
assert_equal 0.0, @stats.avg
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_reset
|
|
53
|
+
@stats.add(100)
|
|
54
|
+
@stats.reset
|
|
55
|
+
|
|
56
|
+
assert_equal 0.0, @stats.sum
|
|
57
|
+
assert_equal 0, @stats.count
|
|
58
|
+
assert_nil @stats.min
|
|
59
|
+
assert_nil @stats.max
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_min_max_single_value
|
|
63
|
+
@stats.add(42)
|
|
64
|
+
|
|
65
|
+
assert_equal 42, @stats.min
|
|
66
|
+
assert_equal 42, @stats.max
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
4
|
+
|
|
5
|
+
require "minitest/autorun"
|
|
6
|
+
require "repolho"
|
|
7
|
+
require "nokogiri"
|
|
8
|
+
require "tempfile"
|
|
9
|
+
require "stringio"
|
|
10
|
+
|
|
11
|
+
module TestHelpers
|
|
12
|
+
def fixture_path(name)
|
|
13
|
+
File.join(__dir__, "fixtures", name)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def write_temp_template(content)
|
|
17
|
+
file = Tempfile.new(["report", ".rep.erb"])
|
|
18
|
+
file.write(content)
|
|
19
|
+
file.close
|
|
20
|
+
file
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def html_doc(html)
|
|
24
|
+
Nokogiri::HTML(html)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def page_tbodies(doc)
|
|
28
|
+
doc.css("tbody.repolho-page")
|
|
29
|
+
end
|
|
30
|
+
end
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: repolho
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Eustáquio Rangel
|
|
@@ -51,6 +51,34 @@ dependencies:
|
|
|
51
51
|
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
53
|
version: '1.16'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: minitest
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '5.20'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '5.20'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: rake
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '13.0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - "~>"
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '13.0'
|
|
54
82
|
description: Repolho generates HTML reports from .rep.erb templates with support for
|
|
55
83
|
nested groupings, page breaks, headers/footers, and statistics (sum, avg, min, max).
|
|
56
84
|
Works with ActiveRecord::Result, Relation, or arrays of hashes.
|
|
@@ -70,6 +98,13 @@ files:
|
|
|
70
98
|
- lib/repolho/report.rb
|
|
71
99
|
- lib/repolho/stats.rb
|
|
72
100
|
- lib/repolho/version.rb
|
|
101
|
+
- test/container_test.rb
|
|
102
|
+
- test/fixtures/simple.rep.erb
|
|
103
|
+
- test/main_test.rb
|
|
104
|
+
- test/page_test.rb
|
|
105
|
+
- test/report_test.rb
|
|
106
|
+
- test/stats_test.rb
|
|
107
|
+
- test/test_helper.rb
|
|
73
108
|
homepage: https://github.com/taq/repolho
|
|
74
109
|
licenses:
|
|
75
110
|
- MIT
|
metadata.gz.sig
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
��
|
|
4
|
-
�
|
|
5
|
-
�/
|
|
1
|
+
�e�G%�XY��6�H�2@)����~1+ş]����,,x�J�O�Rh/�{#G�+K�I�Cem#���`�c
|
|
2
|
+
�Gf_���rPտ�e�t�E�>F�e����R9]�^�of&�%�l��N�x�ɔ�
|
|
3
|
+
���2������?��|T�����,����bɪ�A*��<�s�E]!f�����T��d�(��[�� �b���h�P a"��En�㦴a�\W�<J�B<�̙u��1�==�����-�����oB8�.r ����|@�'�����(q-O��S��Wۄ]&������kI�Sz��E������ۏ/ w�P&���E����sn��
|
|
4
|
+
�V�O/�w�)Zt��eY]�W�wG`�K�
|