odf-report 0.5.1 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +5 -13
  2. data/.github/workflows/gem-push.yml +40 -0
  3. data/.gitignore +2 -0
  4. data/.rspec +4 -0
  5. data/CHANGELOG.md +56 -0
  6. data/README.md +220 -0
  7. data/Rakefile +8 -0
  8. data/bin/odt-extract +10 -0
  9. data/bin/odt-viewer +18 -0
  10. data/lib/odf-report.rb +11 -9
  11. data/lib/odf-report/data_source.rb +65 -0
  12. data/lib/odf-report/field.rb +35 -36
  13. data/lib/odf-report/image.rb +57 -0
  14. data/lib/odf-report/nestable.rb +65 -0
  15. data/lib/odf-report/parser/default.rb +5 -4
  16. data/lib/odf-report/report.rb +29 -57
  17. data/lib/odf-report/section.rb +17 -80
  18. data/lib/odf-report/table.rb +52 -81
  19. data/lib/odf-report/template.rb +88 -0
  20. data/lib/odf-report/text.rb +2 -4
  21. data/lib/odf-report/version.rb +1 -1
  22. data/odf-report.gemspec +7 -4
  23. data/spec/fields_spec.rb +77 -0
  24. data/spec/images/image_1.jpg +0 -0
  25. data/spec/images/image_2.jpg +0 -0
  26. data/spec/images/image_3.jpg +0 -0
  27. data/{test → spec/images}/piriapolis.jpg +0 -0
  28. data/spec/images/placeholder.jpg +0 -0
  29. data/{test → spec/images}/rails.png +0 -0
  30. data/spec/images_spec.rb +159 -0
  31. data/spec/sections_spec.rb +51 -0
  32. data/spec/spec_helper.rb +43 -0
  33. data/spec/tables_spec.rb +39 -0
  34. data/spec/template_spec.rb +45 -0
  35. data/spec/templates/images.odt +0 -0
  36. data/spec/templates/specs.odt +0 -0
  37. data/test/fields_inside_text_test.rb +38 -0
  38. data/test/images_test.rb +32 -0
  39. data/test/nested_tables_test.rb +43 -0
  40. data/test/sections_test.rb +44 -0
  41. data/test/sub_sections_test.rb +58 -0
  42. data/test/table_headers_test.rb +41 -0
  43. data/test/tables_test.rb +67 -0
  44. data/test/templates/images/image_1.jpg +0 -0
  45. data/test/templates/images/image_2.jpg +0 -0
  46. data/test/templates/images/image_3.jpg +0 -0
  47. data/test/templates/images/placeholder.jpg +0 -0
  48. data/test/templates/images/placeholder.png +0 -0
  49. data/test/templates/piriapolis.jpg +0 -0
  50. data/test/templates/rails.png +0 -0
  51. data/test/templates/test_images.odt +0 -0
  52. data/test/templates/test_sub_sections.odt +0 -0
  53. data/test/templates/test_text.odt +0 -0
  54. data/test/test.rb +262 -0
  55. data/test/text_test.rb +56 -0
  56. metadata +151 -46
  57. data/README.textile +0 -225
  58. data/lib/odf-report/fields.rb +0 -40
  59. data/lib/odf-report/file.rb +0 -50
  60. data/lib/odf-report/images.rb +0 -44
  61. data/lib/odf-report/nested.rb +0 -34
  62. data/test/test_fields_inside_text.rb +0 -37
  63. data/test/test_nested_tables.rb +0 -39
  64. data/test/test_sections.rb +0 -39
  65. data/test/test_sub_sections.rb +0 -57
  66. data/test/test_table_headers.rb +0 -39
  67. data/test/test_tables.rb +0 -62
  68. data/test/test_text.rb +0 -48
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YzhiYWE2MTU5OTRjZWQ2NGExZDc1ZDRjMGUzM2ZlZWU1ZWY4ZWUwNg==
5
- data.tar.gz: !binary |-
6
- YTBkNGEzZjE1NmNkMjc3MTdiNjdmNjZhMDI4ZjAzYjg5NTU0NjJjZA==
2
+ SHA256:
3
+ metadata.gz: b953733bc99e0c7e77fc57a5b9e53f38756cc926794df2a8bcfa66156469e1e2
4
+ data.tar.gz: 69d20e56f5c14cf55c8ddad18019dc656a35a1b247f2d8b07ad0d64ea90cfad0
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZjU1YzA0ZjY5YmRlMmJhZTE4YmM0NjY5MDQ0MDkyNGFlYjU1MGYzOTM1NTk0
10
- Y2IyODBlMmEwNWRiODgzZmViMDI1MGE1MmJhODUxYzc3MWY4NDdlNzZmYTEw
11
- YzVmZDA5NDIwNTVlYTRjMTQ0YTM1ZTQ0N2JkOTE4MDIyYThkOTY=
12
- data.tar.gz: !binary |-
13
- MDNlY2U4ZTU2NTUxZTYzYTZlMWZkN2YxYjI1YTE0YTFlODBmMzFiOTdmMThk
14
- YzYyMWQwN2QzZDcxZDhmZjE3ZmQ1ZjFhY2E0NTY5MzcxM2M4NWE3NmEzZTBk
15
- ZTI1MjI2OGJmZGMzZGIzYTcwZDYxNjYzYzYyYTgzYjhkNDFmNDc=
6
+ metadata.gz: 8e14bbf17762213635726cf0a6c8270ce9d701e7315e180a1d46b142ae616ddb20e327aa57f67effc02700da73ce452e310c0c77eb861666039b92b83ffcd6a8
7
+ data.tar.gz: 18de0c6ca80d7740bbfee94d02cf99e2ea1a6153861b68bdd41a0303b9159dca09899b3123b18a965b608341e754f8ca018599e4ad985a18166832beb974314b
@@ -0,0 +1,40 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ build:
9
+ name: Build + Publish
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Set up Ruby 2.6
15
+ uses: actions/setup-ruby@v1
16
+ with:
17
+ version: 2.6.x
18
+
19
+ # - name: Publish to GPR
20
+ # run: |
21
+ # mkdir -p $HOME/.gem
22
+ # touch $HOME/.gem/credentials
23
+ # chmod 0600 $HOME/.gem/credentials
24
+ # printf -- "---\n:github: Bearer ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
25
+ # gem build *.gemspec
26
+ # gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
27
+ # env:
28
+ # GEM_HOST_API_KEY: ${{secrets.GPR_AUTH_TOKEN}}
29
+ # OWNER: username
30
+
31
+ - name: Publish to RubyGems
32
+ run: |
33
+ mkdir -p $HOME/.gem
34
+ touch $HOME/.gem/credentials
35
+ chmod 0600 $HOME/.gem/credentials
36
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
37
+ gem build *.gemspec
38
+ gem push *.gem
39
+ env:
40
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
data/.gitignore CHANGED
@@ -3,4 +3,6 @@ pckg
3
3
  doc
4
4
  *.gem
5
5
  test/result
6
+ spec/result
6
7
  Gemfile.lock
8
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
4
+ --format documentation
@@ -0,0 +1,56 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
+
7
+ ## Unreleased
8
+
9
+ ### Breaking Changes
10
+
11
+ - None
12
+
13
+ ### Added
14
+
15
+ - None
16
+
17
+ ### Fixed
18
+
19
+ - None
20
+
21
+
22
+ ## 0.7.1
23
+
24
+ ### Added
25
+
26
+ - remove image if path is null
27
+ - remove section if collection is empty/null
28
+
29
+
30
+ ## 0.7.0
31
+
32
+ ### Added
33
+
34
+ - allow nested images inside tables and sections
35
+ - allow sections inside tables
36
+
37
+ ### Dependencies
38
+
39
+ - rubyzip >= 1.3.0 (was ~> 1.2.0)
40
+
41
+
42
+ ## 0.6.0
43
+
44
+ ### Breaking Changes
45
+
46
+ - `ODFReport::File` renamed to `ODFReport::Template`
47
+ - `ODFReport::Report` constructor signature changed
48
+
49
+ ### Dependencies
50
+
51
+ - rubyzip ~> 1.2.0 (was ~> 1.1.0)
52
+
53
+
54
+ ## Earlier Versions
55
+
56
+ - No docs yet. Contributions welcome!
@@ -0,0 +1,220 @@
1
+
2
+ # ODF-REPORT
3
+
4
+ Gem for generating .odt files by making strings, images, tables and sections replacements in a previously created .odt file.
5
+
6
+ ## INSTALL
7
+
8
+ In your Gemfile
9
+ ```ruby
10
+ gem 'odf-report'
11
+ ```
12
+
13
+ ## USAGE
14
+
15
+ ### Step 1 -- the template
16
+
17
+ First of all, you need a `.odt` file to serve as a template.
18
+ Templates are normal .odt files with `[PLACEHOLDERS]` for *substitutions*.
19
+ There are *four* kinds of substitutions available:
20
+ * fields
21
+ * tables
22
+ * images
23
+ * sections
24
+
25
+ #### Fields
26
+
27
+ It's just an upcase sentence, surrounded by brackets. It will be replaced by the value you supply.
28
+
29
+ In the folowing example:
30
+
31
+ ```ruby
32
+ report = ODFReport::Report.new("Users/john/my_template.odt") do |r|
33
+ r.add_field :user_name, @user.name
34
+ r.add_field :address, "My new address"
35
+ end
36
+ ```
37
+
38
+ All occurences of `[USER_NAME]` found in the file will be replaced by the value of `@user.name` whereas all `[ADDRESS]` 'es will contains `My new address`
39
+
40
+
41
+ #### Tables
42
+
43
+ To use table placeholders, you should create a Table in your document and give it a name. In OpenOffice, it's just a matter of right-clicking the table you just created, choose `Table Properties...` and type a name in the Name field.
44
+
45
+ If you inform `header: true`, the first row will be treated as a *header* and left untouched. The remaining rows will be used as the template for the table.
46
+
47
+ If you have more than one template row, they will be cycled. This is usefull for making zebra tables.
48
+
49
+ As with **Field placeholders**, just insert a `[FIELD_NAME]` in each cell and let the magic takes place.
50
+
51
+ Taking the folowing example:
52
+
53
+ ```ruby
54
+ report = ODFReport::Report.new("Users/john/my_template.odt") do |r|
55
+
56
+ r.add_field "USER_NAME", @user.nome
57
+ r.add_field "ADDRESS", @user.address
58
+
59
+ r.add_table("TABLE_1", @list_of_itens, :header=>true) do |t|
60
+ t.add_column(:item_id, :id)
61
+ t.add_column(:description) { |item| "==> #{item.description}" }
62
+ end
63
+
64
+ end
65
+ ```
66
+
67
+ and considering you have a table like this in your template
68
+
69
+ | #ID | Description |
70
+ |--|--|
71
+ | [ITEM_ID] | [DESCRIPTION] |
72
+
73
+
74
+ and a collection `@list_of_itens`, it will create one row for each item in the collection, and the replacement will take place accordingly.
75
+
76
+ Any format applied to the fields in the template will be preserved.
77
+
78
+ #### Sections
79
+
80
+ Sometimes, you have to repeat a whole chunk of a document, in a structure a lot more complex than a table. You can make a Section in your template and use it in this situations. Creating a Section in OpenOffice is as easy as select menu *Insert* and then *Section...*, and then choose a name for it.
81
+
82
+ Sections are lot like Tables, in the sense that you can pass a collection and have that section repeated for each member of the collection. *But*, Sections can have anything inside it, even Tables *and nested Sections*, as long as you provide the appropriate data structure.
83
+
84
+ Let's see an example:
85
+
86
+ ```ruby
87
+ @invoices = Invoice.find(:all)
88
+
89
+ report = ODFReport::Report.new("reports/invoice.odt") do |r|
90
+
91
+ r.add_field(:title, "INVOICES REPORT")
92
+ r.add_field(:date, Date.today)
93
+
94
+ r.add_section("SC_INVOICE", @invoices) do |s|
95
+
96
+ s.add_field(:number) { |invoice| invoice.number.to_s.rjust(5, '0') }
97
+ s.add_field(:name, :customer_name)
98
+ s.add_field(:address, :customer_address)
99
+
100
+ s.add_table("TB_ITEMS", :items, header: true) do |t|
101
+ t.add_column(:id)
102
+ t.add_column(:product) {|item| item.product.name }
103
+ t.add_column(:value, :product_value)
104
+ end
105
+
106
+ s.add_field(:total) do |invoice|
107
+ if invoice.status == 'CLOSED'
108
+ invoice.total
109
+ else
110
+ invoice.items.sum('product_value')}
111
+ end
112
+ end
113
+
114
+ s.add_section("SUB_NOTES", :notes) do |s1|
115
+
116
+ s1.add_field(:note_title) { |n| n.title }
117
+
118
+ s1.add_table ...
119
+
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+ ```
126
+
127
+ Note that when you add a Table to a Section, you don't pass the collection itself, but the attribute of the item of that section that will return the collection for that particular Table. Sounds complicated, huh? But once you get it, it's quite straightforward.
128
+
129
+ In the above example, `s.add_table("TB_ITEMS", :items, header: true) do |t|`, the `:items` thing refers to a `invoice.items`. Easy, right?
130
+
131
+
132
+ #### Images
133
+
134
+ You must put a mock image in your `.odt` template and give it a name. That name will be used to replace the mock image for the actual image.
135
+ You can also assign any properties you want to the mock image and they will be kept once the image is replaced.
136
+
137
+ An image replace would look like this:
138
+
139
+ ```ruby
140
+ report = ODFReport::Report.new("my_template.odt") do |r|
141
+ r.add_image :graphic1, "/path/to/the/image.jpg"
142
+
143
+ r.add_table("TABLE_WITH_IMAGES", @items) do |t|
144
+ t.add_column(:id)
145
+ t.add_column(:product, :product_name)
146
+ t.add_image('PRODUCT_IMAGE') { |item| item.image_path }
147
+ end
148
+ end
149
+ ```
150
+
151
+ <hr/>
152
+
153
+ ### Step 2 -- generating the document
154
+
155
+ It's fairly simple to generate the document. You can use this inside a Rails application or in a standalone script.
156
+
157
+ #### Generating a document in a Rails application
158
+
159
+ In a controller, you can have a code like this:
160
+
161
+ ```ruby
162
+ def print
163
+
164
+ @ticket = Ticket.find(params[:id])
165
+
166
+ report = ODFReport::Report.new(Rails.root.join("/app/reports/ticket.odt")) do |r|
167
+
168
+ r.add_field(:id, @ticket.id.to_s)
169
+ r.add_field(:created_by, @ticket.created_by)
170
+ r.add_field(:created_at, @ticket.created_at.strftime("%d/%m/%Y - %H:%M"))
171
+ r.add_field(:type, @ticket.type.name)
172
+ r.add_field(:status, @ticket.status_text)
173
+ r.add_field(:date, Time.now.strftime("%d/%m/%Y - %H:%M"))
174
+ r.add_field(:solution, (@ticket.solution || ''))
175
+
176
+ r.add_table("OPERATORS", @ticket.operators) do |t|
177
+ t.add_column(:name) { |op| "#{op.name} (#{op.department.short_name})" }
178
+ end
179
+
180
+ r.add_table("FIELDS", @ticket.fields) do |t|
181
+ t.add_column(:field_name, :name)
182
+ t.add_column(:field_value) { |field| field.text_value || "Empty" }
183
+ end
184
+
185
+ end
186
+
187
+ send_data report.generate,
188
+ type: 'application/vnd.oasis.opendocument.text',
189
+ disposition: 'attachment',
190
+ filename: 'report.odt'
191
+
192
+ end
193
+ ```
194
+
195
+ #### Generating a document in a standalone script
196
+
197
+ It's very similar to a Rails app, but you can inform the path where the file will be saved.
198
+
199
+ ```ruby
200
+ report = ODFReport::Report.new("ticket.odt") do |r|
201
+ ... populates the report ...
202
+ end
203
+
204
+ report.generate("./documents/new_ticket.odt")
205
+ ```
206
+
207
+ #### Using a template stored in the database (or anywhere besides the file system)
208
+ You can provide an `io:` param, containing the actual file read into a String.
209
+
210
+ ```ruby
211
+ report = ODFReport::Report.new(io: @template.attachment.read) do |r|
212
+ ```
213
+
214
+ <hr/>
215
+
216
+ #### REQUIREMENTS
217
+
218
+ **rubyzip**: manipulating the contents of the odt file, since it's actually a zip file.
219
+ **nokogiri**: parsing and manipulating the document xml files.
220
+ **mime-types**: identify images mime types
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'launchy'
3
+ task :test do
4
+ Dir.glob('./test/*_test.rb').each { |file| require file}
5
+ end
6
+
7
+ task :open do
8
+ Dir.glob('./test/result/*.odt').each { |file| Launchy.open(file) }
9
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'launchy'
4
+
5
+ arg = ARGV[0]
6
+ dir = File.basename(arg, File.extname(arg))
7
+
8
+ %x( rm -rf #{dir}; unzip -d #{dir} #{arg} )
9
+
10
+ Launchy.open dir
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'launchy'
4
+ require "nokogiri"
5
+ require "zip"
6
+
7
+ xml = ""
8
+
9
+ Zip::File.open(ARGV[0]) do |zip|
10
+ content = zip.get_entry('content.xml').get_input_stream.read
11
+ xml = Nokogiri::XML(content).to_xml
12
+ end
13
+
14
+ filename = File.join(Dir.mktmpdir, "#{File.basename(ARGV[0])}.result.xml")
15
+
16
+ File.open(filename, 'w') { |f| f.write xml }
17
+
18
+ Launchy.open filename
@@ -2,15 +2,17 @@ require 'rubygems'
2
2
  require 'zip'
3
3
  require 'fileutils'
4
4
  require 'nokogiri'
5
+ require 'mime/types'
6
+ require 'securerandom'
5
7
 
6
8
  require File.expand_path('../odf-report/parser/default', __FILE__)
7
9
 
8
- require File.expand_path('../odf-report/images', __FILE__)
9
- require File.expand_path('../odf-report/field', __FILE__)
10
- require File.expand_path('../odf-report/text', __FILE__)
11
- require File.expand_path('../odf-report/file', __FILE__)
12
- require File.expand_path('../odf-report/fields', __FILE__)
13
- require File.expand_path('../odf-report/nested', __FILE__)
14
- require File.expand_path('../odf-report/section', __FILE__)
15
- require File.expand_path('../odf-report/table', __FILE__)
16
- require File.expand_path('../odf-report/report', __FILE__)
10
+ require File.expand_path('../odf-report/data_source', __FILE__)
11
+ require File.expand_path('../odf-report/field', __FILE__)
12
+ require File.expand_path('../odf-report/text', __FILE__)
13
+ require File.expand_path('../odf-report/template', __FILE__)
14
+ require File.expand_path('../odf-report/image', __FILE__)
15
+ require File.expand_path('../odf-report/nestable', __FILE__)
16
+ require File.expand_path('../odf-report/section', __FILE__)
17
+ require File.expand_path('../odf-report/table', __FILE__)
18
+ require File.expand_path('../odf-report/report', __FILE__)
@@ -0,0 +1,65 @@
1
+ module ODFReport
2
+ class DataSource
3
+
4
+ attr_reader :value
5
+
6
+ def initialize(opts, &block)
7
+ @value = opts[:value] || opts[:collection]
8
+ @data_field = opts[:data_field] || opts[:collection_field] || opts[:name]
9
+ @block = block
10
+ end
11
+
12
+ def set_source(record)
13
+ @value = extract_value_from_item(record)
14
+ end
15
+
16
+ def each(&block)
17
+ return unless @value
18
+ @value.each(&block)
19
+ end
20
+
21
+ def empty?
22
+ @value.nil? || @value.empty?
23
+ end
24
+
25
+ private
26
+
27
+ def extract_value_from_item(record)
28
+
29
+ if @block
30
+ @block.call(record)
31
+
32
+ elsif record.is_a?(Hash)
33
+ key = @data_field
34
+ record[key] || record[key.to_s.downcase] || record[key.to_s.upcase] || record[key.to_s.downcase.to_sym]
35
+
36
+ elsif @data_field.is_a?(Array)
37
+ execute_methods_on_item(record)
38
+
39
+ elsif @data_field.is_a?(Hash) && record.respond_to?(@data_field.keys[0])
40
+ record.send(@data_field.keys[0], @data_field.values[0])
41
+
42
+ elsif record.respond_to?(@data_field)
43
+ record.send(@data_field)
44
+
45
+ else
46
+ raise "Can't find [#{@data_field.to_s}] in this #{record.class}"
47
+
48
+ end
49
+
50
+ end
51
+
52
+ def execute_methods_on_item(record)
53
+ tmp = record.dup
54
+ @data_field.each do |f|
55
+ if f.is_a?(Hash)
56
+ tmp = tmp.send(f.keys[0], f.values[0])
57
+ else
58
+ tmp = tmp.send(f)
59
+ end
60
+ end
61
+ tmp
62
+ end
63
+
64
+ end
65
+ end