acts_as_pdf 0.1.1 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8097fa288bd2b41f23f8f0a86893384c646055a4
4
- data.tar.gz: 164a123197257f6cc6c86cd70648aba91d6bf823
3
+ metadata.gz: 07f696acccba5069d6731c64603a9558574bc25d
4
+ data.tar.gz: dd2e2b09e636cf7bd159f7aa4df716ef043ccbaa
5
5
  SHA512:
6
- metadata.gz: 84e077731a89b240b753f198b76072896d242864d02d3e10718868b28f5a9d8cb5fc0132fad0f4c76a9cc4efc5bb3d6ba403b2c34acde798f79fe27878f34742
7
- data.tar.gz: d14abcbade8ba83e3ba6227e83d99731b1958384e80687cd092c05b9e8476a750d750f644043f0863f33dbba135cbd9319b4aee91267d4c3eadfb346b9f1563a
6
+ metadata.gz: 0c86878cd3f0af40d38792cf13648b3799ff56777187fd25f46c8fe8fce726bb3dea399bb178cb7d6c88e5ee6d6de2149e92942ddf0e1181202a72a24339829d
7
+ data.tar.gz: 283d5e0d4ad9840cc66b1ab11209bdc7779284238c074deb2e4c643b7f58819e53cac18dc01547b3726b3f6f6f3fc3480e812c9dd3696feda3d99db79e488e01
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # ActsAsPdf
2
- Short description and motivation.
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ Quick way to convert markdown to a template pdf.
6
4
 
7
5
  ## Installation
8
6
  Add this line to your application's Gemfile:
@@ -21,8 +19,81 @@ Or install it yourself as:
21
19
  $ gem install acts_as_pdf
22
20
  ```
23
21
 
24
- ## Contributing
25
- Contribution directions go here.
22
+ ## Usage
23
+
24
+ **bill.rb**
25
+ ```ruby
26
+ class Bill < ApplicationRecord
27
+
28
+ acts_as_pdf :field, {method: :generate_bill, font_size: '10', font_name: 'Arial', line_height: '180%'}
29
+
30
+ def generate_bill user, company, obj # has many arguments or no one
31
+ {
32
+ "//COMPANY_NAME//": company.company_name,
33
+ "//COMPANY_FULL_ADDRESS//": company.company_full_address,
34
+ "//COMPANY_USER_POST//": company.company_user_post,
35
+ "//USER_FIRST_NAME//": user.first_name,
36
+ "//USER_LAST_NAME//": user.last_name,
37
+ "//OBJECT_PRICE//": obj.salary,
38
+ "//OBJECT_CURRENCY//": obj.currency
39
+ }
40
+ end
41
+
42
+ end
43
+ ```
44
+
45
+ **bills_controller.rb**
46
+ ```ruby
47
+ class BillsController < ApplicationController
48
+ preview_pdf
49
+ # preview_pdf {view: 'preview.pdf'}
50
+ end
51
+ ```
52
+
53
+ **views/bills/preview.pdf.prawn**
54
+ ```ruby
55
+ @pdf
56
+ ```
57
+
58
+ **views/bills/_form.html.haml**
59
+ ```ruby
60
+ = simple_form_for resource do |f|
61
+ = f.text :field # with the markdown gem you want (be aware of whitespaces)
62
+ = f.submit 'preview', name: 'preview', action: 'preview', formtarget: "_blank", id: "first"
63
+ = f.submit class: 'btn btn-custom inline', id: "second"
64
+
65
+ :javascript # in other file
66
+ $('#first').removeAttr('data-disable-with')
67
+ $('#second').removeAttr('data-disable-with')
68
+
69
+ ```
70
+
71
+ **routes.rb**
72
+ ```ruby
73
+ resources :bills do
74
+ post :create, on: :member, action: :preview
75
+ patch :update, on: :member, action: :preview
76
+ end
77
+ ```
78
+
79
+ Now you can use it at runtime:
80
+
81
+ **users_controller.rb**
82
+ ```ruby
83
+ class BillsController < ApplicationController
84
+
85
+ def after_buying_something
86
+ bill = Bill.find(param[:bill_id])
87
+ product = Product.find(param[:product_id])
88
+ @pdf = bill.generate_bill(@user, product.company, product)
89
+ end
90
+ end
91
+ ```
92
+
93
+ **views/bills/after_buying_something.pdf.prawn**
94
+ ```ruby
95
+ @pdf
96
+ ```
26
97
 
27
98
  ## License
28
99
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/lib/acts_as_pdf.rb CHANGED
@@ -1,17 +1,22 @@
1
+ require 'preview_pdf'
2
+
1
3
  module ActsAsPdf
2
4
  extend ActiveSupport::Concern
3
5
 
4
- @@pdf_options = Hash.new { |hash, key| hash[key] = Hash.new { |hash, key| hash[key] = Hash.new } }
6
+ @@pdf_options = {}
5
7
  cattr_accessor :pdf_options
6
8
 
7
9
  def md_pdf args = []
8
10
  hash = self.send(ActsAsPdf.pdf_options[self.class.to_s.downcase][:opts][:method], *args) rescue {}
9
11
  output = md_to_pdf
10
12
 
11
- font_size = ActsAsPdf.pdf_options[self.class.to_s.downcase][:opts][:font_size] || '10'
13
+ opts = ActsAsPdf.pdf_options[self.class.to_s.downcase][:opts]
14
+ font_name = opts[:font_name] || 'Futura'
15
+ font_size = opts[:font_size] || '10'
16
+ line_height = opts[:line_height] || '180%'
12
17
 
13
18
  hash.each { |k, v| output.gsub!(k.to_s, v.to_s) }
14
- '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' + "<style type = 'text/css'> body { font-family: 'Futura'; font-size: #{font_size}; line-height:180%; }</style>" + output
19
+ "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'> <style type = 'text/css'> body { font-family: #{font_name}; font-size: #{font_size}; line-height: #{line_height}; }</style> " + output
15
20
  end
16
21
 
17
22
  def md_to_pdf
@@ -22,6 +27,10 @@ module ActsAsPdf
22
27
  result[:output].to_s
23
28
  end
24
29
 
30
+ def name_method
31
+ ActsAsPdf.pdf_options[self.class.to_s.downcase][:opts][:method] rescue nil
32
+ end
33
+
25
34
  def preview params
26
35
  return self unless params[:preview]
27
36
 
@@ -34,13 +43,14 @@ module ActsAsPdf
34
43
  end
35
44
 
36
45
  def generate_pdf params, args = []
37
- convention = self.preview params
38
- WickedPdf.new.pdf_from_string(convention.md_pdf(args))
46
+ model = self.preview params
47
+ WickedPdf.new.pdf_from_string(model.md_pdf(args), ActsAsPdf.pdf_options[self.class.to_s.downcase][:opts])
39
48
  end
40
49
 
41
50
  module ClassMethods
42
51
 
43
52
  def acts_as_pdf field, opts = {}
53
+ ActsAsPdf.pdf_options[self.to_s.downcase] ||= {}
44
54
  ActsAsPdf.pdf_options[self.to_s.downcase][:opts] = opts
45
55
  ActsAsPdf.pdf_options[self.to_s.downcase][:field] = field
46
56
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsPdf
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.6'
3
3
  end
@@ -0,0 +1,34 @@
1
+ module PreviewPdfAddOn
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def preview_pdf opts = {}
6
+
7
+ define_method :preview do
8
+
9
+ name_view = opts[:view] || 'preview.pdf'
10
+ @pdf = resource.generate_pdf params
11
+ if params[:preview]
12
+ render name_view
13
+ else
14
+ route = params[:commit].split(' ').first.downcase.to_sym
15
+ send(route) if [:create, :update].include? route
16
+ end
17
+ end
18
+
19
+ define_method :create do
20
+
21
+ if params[:preview]
22
+ send('preview')
23
+ else
24
+ super()
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+ class ActionController::Base
33
+ include PreviewPdfAddOn
34
+ end
metadata CHANGED
@@ -1,15 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oren Carta-Lag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-15 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2017-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: html-pipeline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: wicked_pdf
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: wkhtmltopdf-binary
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: prawn_rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
13
69
  description: Description of ActsAsPdf.
14
70
  email:
15
71
  - oren.carta-lag@hotmail.fr
@@ -22,6 +78,7 @@ files:
22
78
  - Rakefile
23
79
  - lib/acts_as_pdf.rb
24
80
  - lib/acts_as_pdf/version.rb
81
+ - lib/preview_pdf.rb
25
82
  - lib/tasks/acts_as_pdf_tasks.rake
26
83
  homepage: http://rubygems.org/gems/acts_as_pdf
27
84
  licenses: