prawn_cocktail 0.4.0 → 0.5.0
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.
- data/README.md +18 -28
- data/lib/prawn_cocktail/version.rb +1 -1
- data/lib/prawn_cocktail.rb +0 -6
- data/prawn_cocktail.gemspec +2 -2
- metadata +7 -9
- data/lib/prawn_cocktail/controller.rb +0 -14
- data/lib/prawn_cocktail/railtie.rb +0 -9
data/README.md
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# PrawnCocktail
|
2
2
|
|
3
|
-
Simple documents, templates and helpers on top of Prawn
|
3
|
+
Simple documents, templates and helpers on top of Prawn.
|
4
4
|
|
5
5
|
Because writing Prawn documents PHP 4 style is no fun.
|
6
6
|
|
7
|
-
Ruby
|
7
|
+
If you're using this with Ruby on Rails, get [`PrawnCocktailRails`](http://github.com/barsoom/prawn_cocktail_rails).
|
8
|
+
|
9
|
+
Ruby 1.9 only since we use and love instance\_exec.
|
8
10
|
|
9
11
|
NOTE: Work in progress; well used but untested.
|
10
12
|
|
@@ -20,33 +22,17 @@ NOTE: Work in progress; well used but untested.
|
|
20
22
|
|
21
23
|
### Configuration
|
22
24
|
|
23
|
-
You can change where PrawnCocktail looks for its templates. This is the default:
|
25
|
+
You can change where PrawnCocktail looks for its templates. This is the default (suitable for Ruby on Rails):
|
24
26
|
|
25
27
|
``` ruby
|
26
28
|
PrawnCocktail.template_root = "app/views/documents"
|
27
29
|
```
|
28
30
|
|
29
|
-
### Controller
|
30
|
-
|
31
|
-
Your controllers get a `send_pdf` method:
|
32
|
-
|
33
|
-
``` ruby
|
34
|
-
class InvoicesController < ApplicationController
|
35
|
-
def show
|
36
|
-
invoice = Invoice.find(params[:id])
|
37
|
-
document = InvoiceDocument.new(invoice)
|
38
|
-
send_pdf document
|
39
|
-
end
|
40
|
-
end
|
41
|
-
```
|
42
|
-
|
43
31
|
### Document
|
44
32
|
|
45
33
|
The document class provides a data hash for the template, and optionally a filename:
|
46
34
|
|
47
35
|
``` ruby
|
48
|
-
# app/documents/invoice_document.rb
|
49
|
-
|
50
36
|
class InvoiceDocument < PrawnCocktail::Document
|
51
37
|
def initialize(invoice)
|
52
38
|
@invoice = invoice
|
@@ -68,9 +54,11 @@ class InvoiceDocument < PrawnCocktail::Document
|
|
68
54
|
end
|
69
55
|
```
|
70
56
|
|
57
|
+
In Ruby on Rails, we suggest putting this in `app/documents/invoice_document.rb`. But anywhere is fine as long as the file is loaded, automatically or otherwise.
|
58
|
+
|
71
59
|
The document has `render` and `render_file(name)` methods, just like `Prawn::Document`.
|
72
60
|
|
73
|
-
The filename
|
61
|
+
The filename is not required. Other libraries like `PrawnCocktailRails` may make use of it.
|
74
62
|
|
75
63
|
The `data` value is passed to the template as an `OpenStruct`.
|
76
64
|
|
@@ -78,11 +66,11 @@ If the data becomes complex, you are advised to extract one or many builder clas
|
|
78
66
|
|
79
67
|
### Template
|
80
68
|
|
81
|
-
Put the Prawn code in a template named after the document
|
69
|
+
Put the Prawn code in a template in the `PrawnCocktail.template_root` directory, named after the document, with a `.pdf.rb` extension.
|
82
70
|
|
83
|
-
|
84
|
-
# app/views/documents/invoice_document.pdf.rb
|
71
|
+
The defaults (suitable for Ruby on Rails) would make that e.g. `app/views/documents/invoice_document.pdf.rb`.
|
85
72
|
|
73
|
+
``` ruby
|
86
74
|
meta page_size: "A4"
|
87
75
|
|
88
76
|
content do |data|
|
@@ -103,7 +91,7 @@ The `content` block will be passed the data from the document as an `OpenStruct`
|
|
103
91
|
You are advised to extract any complexity or shared code out of the template and into helpers:
|
104
92
|
|
105
93
|
``` ruby
|
106
|
-
#
|
94
|
+
# Document
|
107
95
|
|
108
96
|
class InvoiceDocument
|
109
97
|
helper BaseDocumentHelper
|
@@ -112,7 +100,7 @@ end
|
|
112
100
|
```
|
113
101
|
|
114
102
|
``` ruby
|
115
|
-
#
|
103
|
+
# Template
|
116
104
|
|
117
105
|
content do |data|
|
118
106
|
common_header
|
@@ -121,7 +109,7 @@ end
|
|
121
109
|
```
|
122
110
|
|
123
111
|
``` ruby
|
124
|
-
#
|
112
|
+
# Helper
|
125
113
|
|
126
114
|
module BaseDocumentHelper
|
127
115
|
def common_header
|
@@ -130,9 +118,11 @@ module BaseDocumentHelper
|
|
130
118
|
end
|
131
119
|
```
|
132
120
|
|
133
|
-
|
121
|
+
Any loaded module can be used as a helper, as long as its code can be run in the context of a Prawn document.
|
122
|
+
|
123
|
+
If you use `PrawnCocktailRails`, you can put modules in `app/documents/helpers`, e.g. `app/documents/helpers/base_document_helper.rb`, and they will be autoloaded.
|
134
124
|
|
135
|
-
You can declare base helpers in a base document class, though, and
|
125
|
+
Note that you must explicitly declare the helpers you want. No helpers are automatically included. You can declare base helpers in a base document class, though, and they will be inherited.
|
136
126
|
|
137
127
|
### `initialize_document`
|
138
128
|
|
data/lib/prawn_cocktail.rb
CHANGED
data/prawn_cocktail.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = PrawnCocktail::VERSION
|
9
9
|
gem.authors = ["Henrik Nyh"]
|
10
10
|
gem.email = ["henrik@barsoom.se"]
|
11
|
-
gem.summary = "Simple
|
11
|
+
gem.summary = "Simple documents, templates and helpers on top of Prawn."
|
12
12
|
gem.homepage = ""
|
13
13
|
|
14
14
|
gem.files = `git ls-files`.split($/)
|
@@ -17,5 +17,5 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.require_paths = ["lib"]
|
18
18
|
|
19
19
|
gem.add_dependency "prawn"
|
20
|
-
gem.add_dependency "
|
20
|
+
gem.add_dependency "activesupport"
|
21
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prawn_cocktail
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-02-23 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: prawn
|
16
|
-
requirement: &
|
16
|
+
requirement: &70333264743560 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70333264743560
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70333264742900 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70333264742900
|
36
36
|
description:
|
37
37
|
email:
|
38
38
|
- henrik@barsoom.se
|
@@ -46,9 +46,7 @@ files:
|
|
46
46
|
- Rakefile
|
47
47
|
- lib/prawn_cocktail.rb
|
48
48
|
- lib/prawn_cocktail/configuration.rb
|
49
|
-
- lib/prawn_cocktail/controller.rb
|
50
49
|
- lib/prawn_cocktail/document.rb
|
51
|
-
- lib/prawn_cocktail/railtie.rb
|
52
50
|
- lib/prawn_cocktail/renderer.rb
|
53
51
|
- lib/prawn_cocktail/version.rb
|
54
52
|
- prawn_cocktail.gemspec
|
@@ -75,6 +73,6 @@ rubyforge_project:
|
|
75
73
|
rubygems_version: 1.8.5
|
76
74
|
signing_key:
|
77
75
|
specification_version: 3
|
78
|
-
summary: Simple
|
76
|
+
summary: Simple documents, templates and helpers on top of Prawn.
|
79
77
|
test_files: []
|
80
78
|
has_rdoc:
|