odf-report 0.5.2 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/gem-push.yml +40 -0
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +62 -0
  5. data/README.md +220 -0
  6. data/bin/odt-extract +10 -0
  7. data/bin/odt-viewer +18 -0
  8. data/lib/odf-report.rb +11 -8
  9. data/lib/odf-report/data_source.rb +65 -0
  10. data/lib/odf-report/field.rb +7 -40
  11. data/lib/odf-report/image.rb +57 -0
  12. data/lib/odf-report/nestable.rb +65 -0
  13. data/lib/odf-report/report.rb +24 -28
  14. data/lib/odf-report/section.rb +15 -42
  15. data/lib/odf-report/table.rb +54 -59
  16. data/lib/odf-report/template.rb +88 -0
  17. data/lib/odf-report/text.rb +1 -3
  18. data/lib/odf-report/version.rb +1 -1
  19. data/odf-report.gemspec +4 -4
  20. data/spec/fields_spec.rb +4 -4
  21. data/spec/images/image_1.jpg +0 -0
  22. data/spec/images/image_2.jpg +0 -0
  23. data/spec/images/image_3.jpg +0 -0
  24. data/spec/images/piriapolis.jpg +0 -0
  25. data/spec/images/placeholder.jpg +0 -0
  26. data/spec/images/rails.png +0 -0
  27. data/spec/images_spec.rb +159 -0
  28. data/spec/sections_spec.rb +51 -0
  29. data/spec/spec_helper.rb +2 -4
  30. data/spec/tables_spec.rb +1 -1
  31. data/spec/template_spec.rb +45 -0
  32. data/spec/templates/images.odt +0 -0
  33. data/spec/templates/specs.odt +0 -0
  34. data/test/images_test.rb +32 -0
  35. data/test/templates/images/image_1.jpg +0 -0
  36. data/test/templates/images/image_2.jpg +0 -0
  37. data/test/templates/images/image_3.jpg +0 -0
  38. data/test/templates/images/placeholder.jpg +0 -0
  39. data/test/templates/images/placeholder.png +0 -0
  40. data/test/templates/test_images.odt +0 -0
  41. data/test/test.rb +262 -0
  42. metadata +77 -23
  43. data/README.textile +0 -223
  44. data/lib/odf-report/file.rb +0 -50
  45. data/lib/odf-report/images.rb +0 -44
  46. data/lib/odf-report/nested.rb +0 -62
  47. data/spec/specs.odt +0 -0
@@ -1,223 +0,0 @@
1
- h1. ODF-REPORT
2
-
3
- Gem for generating .odt files by making strings, images, tables and sections replacements in a previously created .odt file.
4
-
5
- h3. NEW
6
-
7
- * as per popular request, now uses rubyzip ~> 1.1.0
8
- * uses @Zip::OutputStream.write_buffer@ to generate the file. This should avoid corruption issues.
9
- * the @.generate@ method now returns the actual report binary, so you can send_data it directly
10
- * you can also use @.generate('file.odt')@, which saves the report to the specified file
11
-
12
- h2. INSTALL
13
-
14
- In your Gemfile
15
- <pre>gem 'odf-report' </pre>
16
-
17
- h2. USAGE
18
-
19
- h3. Step 1 -- the template
20
-
21
- First of all, you need to create a .odt file to serve as a template.
22
- Templates are normal .odt files with placeholders for *substitutions*.
23
- There are now *four* kinds of substitutions available: *fields*, *tables*, *images* and *sections*.
24
-
25
- h3. Fields placeholders
26
-
27
- It's just an upcase sentence, surrounded by brackets. It will be replaced for wathever value you supply.
28
-
29
- In the folowing example:
30
-
31
- <pre>
32
- report = ODFReport::Report.new("Users/john/my_template.odt") do |r|
33
-
34
- r.add_field :user_name, @user.name
35
- r.add_field :address, "My new address"
36
-
37
- end
38
- </pre>
39
-
40
- 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@
41
-
42
- It's as simple as that.
43
-
44
-
45
- h3. Table placeholders
46
-
47
- 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.
48
-
49
- 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. If you have more than one template row, they will be cycled. This is usefull for making zebra tables.
50
-
51
- As with Field placeholders, just insert a @[FIELD_NAME]@ in each cell and let the magic takes place.
52
-
53
- Taking the folowing example:
54
-
55
- <pre>
56
- report = ODFReport::Report.new("Users/john/my_template.odt") do |r|
57
-
58
- r.add_field "USER_NAME", @user.nome
59
- r.add_field "ADDRESS", @user.address
60
-
61
- r.add_table("TABLE_1", @list_of_itens, :header=>true) do |t|
62
- t.add_column(:item_id, :id)
63
- t.add_column(:description) { |item| "==> #{item.description}" }
64
- end
65
-
66
- end
67
- </pre>
68
-
69
- and considering you have a table like this in your template
70
-
71
- <pre>
72
- ---------------------------------
73
- | [ITEM_ID] | [DESCRIPTION] |
74
- ---------------------------------
75
-
76
- * this is my lame attempt to draw a table.
77
- you are not supposed to type this.
78
- you have to use an actual table.
79
- i don't know... just thought I should mention it ;-)
80
- </pre>
81
-
82
- and a collection @list_of_itens, it will create one row for each item in the collection, and the replacement will take place accordingly.
83
-
84
- Any format applied to the fields in the template will be preserved.
85
-
86
-
87
- h3. Images
88
-
89
- 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.
90
- You can also assign any properties you want to the mock image and they will be kept once the image is replaced.
91
-
92
- An image replace would look like this:
93
-
94
- <pre>
95
- report = ODFReport::Report.new("Users/john/my_template.odt") do |r|
96
-
97
- r.add_image :graphics1, "/path/to/the/image.jpg"
98
-
99
- end
100
- </pre>
101
-
102
-
103
- h3. Sections
104
-
105
- Sometimes, you have to repeat a whole chunk of a document, in a structure a lot more complex than a table. Now 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.
106
-
107
- *Section* 's 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 pass the appropriate data structure.
108
-
109
- Let's see an example:
110
-
111
- <pre>
112
-
113
- @invoices = Invoice.find(:all)
114
-
115
- report = ODFReport::Report.new("reports/invoice.odt") do |r|
116
-
117
- r.add_field(:title, "INVOICES REPORT")
118
- r.add_field(:date, Date.today)
119
-
120
- r.add_section("SC_INVOICE", @invoices) do |s|
121
-
122
- s.add_field(:number) { |invoice| invoice.number.to_s.rjust(5, '0') }
123
- s.add_field(:name, :customer_name)
124
- s.add_field(:address, :customer_address)
125
-
126
- s.add_table("TB_ITEMS", :items, :header => true) do |t|
127
- t.add_column(:id)
128
- t.add_column(:product) {|item| item.product.name }
129
- t.add_column(:value, :product_value)
130
- end
131
-
132
- s.add_field(:total) do |invoice|
133
- if invoice.status == 'CLOSED'
134
- invoice.total
135
- else
136
- invoice.items.sum('product_value')}
137
- end
138
- end
139
-
140
- s.add_section("SUB_NOTES", :notes) do |s1|
141
-
142
- s1.add_field(:note_title) { |n| n.title }
143
-
144
- s1.add_table ...
145
-
146
- end
147
-
148
- end
149
-
150
- end
151
- </pre>
152
-
153
- 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's gonna return the collection for that particular Table. Sounds complicated, huh? But once you get it, it's quite straightforward.
154
-
155
- In the above example, @s.add_table("TB_ITEMS", :items, :header => true) do |t|@, the @:items@ thing refers to a @invoice.items@. Easy, right?
156
-
157
- <hr/><br/>
158
-
159
- h3. Step 2 -- generating the document
160
-
161
- It's fairly simple to generate the document. You can use this inside a Rails application or in a standalone script.
162
-
163
- h4. Generating a document in a Rails application
164
-
165
- In a controller, you can have a code like this:
166
-
167
- <pre>
168
- def print
169
-
170
- @ticket = Ticket.find(params[:id])
171
-
172
- # For Rails 3 or latest replace #{RAILS_ROOT} to #{Rails.root}
173
- report = ODFReport::Report.new("#{RAILS_ROOT}/app/reports/ticket.odt") do |r|
174
-
175
- r.add_field(:id, @ticket.id.to_s)
176
- r.add_field(:created_by, @ticket.created_by)
177
- r.add_field(:created_at, @ticket.created_at.strftime("%d/%m/%Y - %H:%M"))
178
- r.add_field(:type, @ticket.type.name)
179
- r.add_field(:status, @ticket.status_text)
180
- r.add_field(:date, Time.now.strftime("%d/%m/%Y - %H:%M"))
181
- r.add_field(:solution, (@ticket.solution || ''))
182
-
183
- r.add_table("OPERATORS", @ticket.operators) do |t|
184
- t.add_column(:operator_name) { |op| "#{op.name} (#{op.department.short_name})" }
185
- end
186
-
187
- r.add_table("FIELDS", @ticket.fields) do |t|
188
- t.add_column(:field_name, :name)
189
- t.add_column(:field_value) { |field| field.text_value || "Empty" }
190
- end
191
-
192
- end
193
-
194
- send_data report.generate, type: 'application/vnd.oasis.opendocument.text',
195
- disposition: 'attachment',
196
- filename: 'report.odt'
197
-
198
- end
199
- </pre>
200
-
201
-
202
- _That's all I have to say about that._
203
-
204
- h4. Generating a document in a standalone script
205
-
206
- It's just the same as in a Rails app, but you can inform the path where the file will be saved.
207
-
208
- <pre>
209
- report = ODFReport::Report.new("ticket.odt") do |r|
210
-
211
- ... populates the report ...
212
-
213
- end
214
-
215
- report.generate("./documents/new_ticket.odt")
216
- </pre>
217
-
218
- <hr/>
219
-
220
- h3. REQUIREMENTS
221
-
222
- * rubyzip*: for manipulating the contents of the odt file, since it's actually a zip file.
223
- * nokogiri*: for parsing and manipulating the document xml files.
@@ -1,50 +0,0 @@
1
- module ODFReport
2
- class File
3
-
4
- attr_accessor :output_stream
5
-
6
- def initialize(template)
7
- raise "Template [#{template}] not found." unless ::File.exist? template
8
- @template = template
9
- end
10
-
11
- def update_content
12
- @buffer = Zip::OutputStream.write_buffer do |out|
13
- @output_stream = out
14
- yield self
15
- end
16
- end
17
-
18
- def update_files(*content_files, &block)
19
-
20
- Zip::File.open(@template) do |file|
21
-
22
- file.each do |entry|
23
-
24
- next if entry.directory?
25
-
26
- entry.get_input_stream do |is|
27
-
28
- data = is.sysread
29
-
30
- if content_files.include?(entry.name)
31
- yield data
32
- end
33
-
34
- @output_stream.put_next_entry(entry.name)
35
- @output_stream.write data
36
-
37
- end
38
-
39
- end
40
-
41
- end
42
-
43
- end
44
-
45
- def data
46
- @buffer.string
47
- end
48
-
49
- end
50
- end
@@ -1,44 +0,0 @@
1
- module ODFReport
2
-
3
- module Images
4
-
5
- IMAGE_DIR_NAME = "Pictures"
6
-
7
- def find_image_name_matches(content)
8
-
9
- @images.each_pair do |image_name, path|
10
- if node = content.xpath("//draw:frame[@draw:name='#{image_name}']/draw:image").first
11
- placeholder_path = node.attribute('href').value
12
- @image_names_replacements[path] = ::File.join(IMAGE_DIR_NAME, ::File.basename(placeholder_path))
13
- end
14
- end
15
-
16
- end
17
-
18
- def replace_images(file)
19
-
20
- return if @images.empty?
21
-
22
- @image_names_replacements.each_pair do |path, template_image|
23
-
24
- file.output_stream.put_next_entry(template_image)
25
- file.output_stream.write ::File.read(path)
26
-
27
- end
28
-
29
- end # replace_images
30
-
31
- # newer versions of LibreOffice can't open files with duplicates image names
32
- def avoid_duplicate_image_names(content)
33
-
34
- nodes = content.xpath("//draw:frame[@draw:name]")
35
-
36
- nodes.each_with_index do |node, i|
37
- node.attribute('name').value = "pic_#{i}"
38
- end
39
-
40
- end
41
-
42
- end
43
-
44
- end
@@ -1,62 +0,0 @@
1
- module ODFReport
2
-
3
- module Nested
4
-
5
- def add_field(name, data_field=nil, &block)
6
- opts = {:name => name, :data_field => data_field}
7
- field = Field.new(opts, &block)
8
- @fields << field
9
-
10
- end
11
- alias_method :add_column, :add_field
12
-
13
- def add_text(name, data_field=nil, &block)
14
- opts = {:name => name, :data_field => data_field}
15
- field = Text.new(opts, &block)
16
- @texts << field
17
-
18
- end
19
-
20
- def add_table(table_name, collection_field, opts={})
21
- opts.merge!(:name => table_name, :collection_field => collection_field)
22
- tab = Table.new(opts)
23
- @tables << tab
24
-
25
- yield(tab)
26
- end
27
-
28
- def add_section(section_name, collection_field, opts={})
29
- opts.merge!(:name => section_name, :collection_field => collection_field)
30
- sec = Section.new(opts)
31
- @sections << sec
32
-
33
- yield(sec)
34
- end
35
-
36
-
37
- def get_collection_from_item(item, collection_field)
38
-
39
- return item[collection_field] if item.is_a?(Hash)
40
-
41
- if collection_field.is_a?(Array)
42
- tmp = item.dup
43
- collection_field.each do |f|
44
- if f.is_a?(Hash)
45
- tmp = tmp.send(f.keys[0], f.values[0])
46
- else
47
- tmp = tmp.send(f)
48
- end
49
- end
50
- collection = tmp
51
- elsif collection_field.is_a?(Hash)
52
- collection = item.send(collection_field.keys[0], collection_field.values[0])
53
- else
54
- collection = item.send(collection_field)
55
- end
56
-
57
- return collection
58
- end
59
-
60
- end
61
-
62
- end
Binary file