prawnto_2 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ **/*.swp
3
+ *.gem
4
+ rdoc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in my_project.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 cracklabs.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ = Prawnto (prawnto_2)
2
+
3
+ A rails plugin providing templating abilities for generating pdf files leveraging the new kick-ass prawn library (http://prawn.majesticseacreature.com/)
4
+
5
+ Full (slightly out of date) documentation/demos at: http://cracklabs.com/prawnto
6
+
7
+ This is my attempt to merge the various forks of the prawnto gem, and <b>update it for Rails 3.1</b>. I want to thank the many developers who's code I collected together for the gem. Main credit goes to the developer of the initial Prawnto plugin - smecsia.
8
+
9
+ <b>Note:</b> It has not been tested with earlier versions of Rails.
10
+
11
+ == Installation in Rails 3.1
12
+
13
+ In your <code>Gemfile</code>:
14
+
15
+ gem 'prawnto_2'
16
+
17
+ == Usage
18
+
19
+ On it's way...
20
+
21
+
22
+
23
+ ---
24
+
25
+
26
+ Copyright on Updates - Copyright (c) 2011 getjobber.com, released under the MIT license
27
+
28
+ Original Copyright - Copyright (c) 2008 cracklabs.com, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Generate documentation for the prawnto plugin.'
10
+ RDoc::Task.new do |rdoc|
11
+ rdoc.rdoc_dir = 'rdoc'
12
+ rdoc.title = 'Prawnto'
13
+
14
+ rdoc.main = "README.rdoc"
15
+ rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb")
16
+ end
17
+
18
+ desc 'Test the prawnto gem'
19
+ Rake::TestTask.new(:test) do |t|
20
+ t.libs << 'lib'
21
+ t.pattern = 'test/*_test.rb'
22
+ # t.verbose = true
23
+ end
data/lib/prawnto.rb ADDED
@@ -0,0 +1,39 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'prawnto/railtie' if defined?(Rails)
5
+
6
+ module Prawnto
7
+ VERSION='0.0.7'
8
+ autoload :ActionControllerMixin, 'prawnto/action_controller_mixin'
9
+ autoload :ActionViewMixin, 'prawnto/action_view_mixin'
10
+ module TemplateHandlers
11
+ autoload :Base, 'prawnto/template_handlers/base'
12
+ autoload :Dsl, 'prawnto/template_handlers/dsl'
13
+ end
14
+
15
+ module TemplateHandler
16
+ autoload :CompileSupport, 'prawnto/template_handler/compile_support'
17
+ end
18
+
19
+ class << self
20
+
21
+ def run_includes
22
+ ActionController::Base.send :include, Prawnto::ActionControllerMixin
23
+ ActionView::Base.send :include, Prawnto::ActionViewMixin
24
+ end
25
+
26
+ def register_handlers
27
+ Mime::Type.register "application/pdf", :pdf unless defined?(Mime::PDF)
28
+ ActionView::Template.register_template_handler 'prawn', Prawnto::TemplateHandlers::Base
29
+ ActionView::Template.register_template_handler 'prawn_dsl', Prawnto::TemplateHandlers::Dsl
30
+ end
31
+
32
+ def init_both
33
+ run_includes
34
+ register_handlers
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,54 @@
1
+ module Prawnto
2
+ module ActionControllerMixin
3
+
4
+ DEFAULT_PRAWNTO_OPTIONS = {:inline=>true}
5
+
6
+ def self.included(base)
7
+ base.class_attribute :prawn_hash, :prawnto_hash
8
+ base.prawn_hash = {}
9
+ base.prawnto_hash = {}
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ # Sets options in the class attributes. Can be pulled into the instance variable with :compute_prawnto_options
16
+ def prawnto(options)
17
+ prawn_options, prawnto_options = breakdown_prawnto_options options
18
+ self.prawn_hash = prawn_options
19
+ self.prawnto_hash = DEFAULT_PRAWNTO_OPTIONS.dup.merge(prawnto_options)
20
+ end
21
+
22
+ private
23
+
24
+ # splits the :prawn key out into a seperate hash
25
+ def breakdown_prawnto_options(options)
26
+ prawnto_options = options.dup
27
+ prawn_options = (prawnto_options.delete(:prawn) || {}).dup
28
+ [prawn_options, prawnto_options]
29
+ end
30
+ end
31
+
32
+
33
+ # Sets options directly on the instance
34
+ def prawnto(options)
35
+ @prawnto_options ||= {}
36
+ @prawnto_options.merge! options
37
+ end
38
+
39
+
40
+ private
41
+
42
+ # Used to set the @prawnto_options variable before rendering. Called from compile_support just before rendering.
43
+ def compute_prawnto_options
44
+ @prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
45
+ @prawnto_options[:prawn] ||= {}
46
+ @prawnto_options[:prawn].merge!(self.class.prawn_hash || {}) {|k,o,n| o}
47
+ @prawnto_options.merge!(self.class.prawnto_hash || {}) {|k,o,n| o}
48
+ @prawnto_options
49
+ end
50
+
51
+ end
52
+ end
53
+
54
+
@@ -0,0 +1,12 @@
1
+ module Prawnto
2
+ module ActionViewMixin
3
+
4
+ private
5
+ def _prawnto_compile_setup
6
+ compile_support = Prawnto::TemplateHandler::CompileSupport.new(controller)
7
+ @prawnto_options = compile_support.options
8
+ end
9
+
10
+ end
11
+ end
12
+
@@ -0,0 +1,16 @@
1
+ module Prawnto
2
+ class Railtie < Rails::Railtie
3
+
4
+ config.to_prepare do
5
+ puts "running railtie initializer"
6
+ Prawnto.register_handlers
7
+ end
8
+
9
+ #switching from an initializer to a :to_prepare will run it once in production and before each load in development
10
+ config.to_prepare do
11
+ puts "running railtie includes"
12
+ Prawnto.run_includes
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,82 @@
1
+ module Prawnto
2
+ module TemplateHandler
3
+
4
+ class CompileSupport
5
+ extend ActiveSupport::Memoizable
6
+
7
+ attr_reader :options
8
+
9
+ def initialize(controller)
10
+ @controller = controller
11
+ @options = pull_options
12
+ set_headers
13
+ end
14
+
15
+ # This sets the @options hash which is used for rendering.
16
+ def pull_options
17
+ @controller.send :compute_prawnto_options || {}
18
+ end
19
+
20
+ def set_headers
21
+ unless defined?(ActionMailer) && defined?(ActionMailer::Base) && @controller.is_a?(ActionMailer::Base)
22
+ set_pragma
23
+ set_cache_control
24
+ set_content_type
25
+ set_disposition
26
+ set_other_headers_for_ie_ssl
27
+ end
28
+ end
29
+
30
+ # TODO: kept around from railspdf-- maybe not needed anymore? should check.
31
+ def ie_request?
32
+ @controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
33
+ end
34
+ memoize :ie_request?
35
+
36
+ # added to make ie happy with ssl pdf's (per naisayer)
37
+ def ssl_request?
38
+ @controller.request.ssl?
39
+ end
40
+ memoize :ssl_request?
41
+
42
+ def set_other_headers_for_ie_ssl
43
+ return unless ssl_request? && ie_request?
44
+ @controller.headers['Content-Description'] = 'File Transfer'
45
+ @controller.headers['Content-Transfer-Encoding'] = 'binary'
46
+ @controller.headers['Expires'] = '0'
47
+ end
48
+
49
+ # TODO: kept around from railspdf-- maybe not needed anymore? should check.
50
+ def set_pragma
51
+ if ssl_request? && ie_request?
52
+ @controller.headers['Pragma'] = 'public' # added to make ie ssl pdfs work (per naisayer)
53
+ else
54
+ @controller.headers['Pragma'] ||= ie_request? ? 'no-cache' : ''
55
+ end
56
+ end
57
+
58
+ # TODO: kept around from railspdf-- maybe not needed anymore? should check.
59
+ def set_cache_control
60
+ if ssl_request? && ie_request?
61
+ @controller.headers['Cache-Control'] = 'maxage=1' # added to make ie ssl pdfs work (per naisayer)
62
+ else
63
+ @controller.headers['Cache-Control'] ||= ie_request? ? 'no-cache, must-revalidate' : ''
64
+ end
65
+ end
66
+
67
+ def set_content_type
68
+ @controller.response.content_type ||= Mime::PDF
69
+ end
70
+
71
+ def set_disposition
72
+ inline = options[:inline] ? 'inline' : 'attachment'
73
+ filename = options[:filename] ? "filename=\"#{options[:filename]}\"" : nil
74
+ @controller.headers["Content-Disposition"] = [inline,filename].compact.join(';')
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+
@@ -0,0 +1,15 @@
1
+ module Prawnto
2
+ module TemplateHandlers
3
+ class Base
4
+
5
+ def self.call(template)
6
+ "_prawnto_compile_setup;" +
7
+ "pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
8
+ "#{template.source}\n" +
9
+ "raw pdf.render;"
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+
@@ -0,0 +1,16 @@
1
+ module Prawnto
2
+ module TemplateHandlers
3
+ class Dsl < Base
4
+
5
+ def self.call(template)
6
+ "_prawnto_compile_setup;" +
7
+ "pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
8
+ "pdf.instance_eval do; #{template.source}\nend;" +
9
+ "raw pdf.render;"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+
16
+
data/prawnto.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ require File.expand_path("../lib/prawnto.rb", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "prawnto_2"
5
+ s.version = Prawnto::VERSION
6
+ s.author = ["Forrest"]
7
+ s.email = ["development@forrestzeisler.com"]
8
+ s.date = Time.now.utc.strftime("%Y-%m-%d")
9
+
10
+
11
+ s.description = 'Simple PDF generation using the prawn library.'
12
+ s.summary = "This gem allows you to use the PDF mime-type and the simple prawn syntax to generate impressive looking PDFs."
13
+
14
+ s.homepage = "http://cracklabs.com/prawnto"
15
+
16
+ s.required_rubygems_version = ">= 1.3.6"
17
+ s.platform = Gem::Platform::RUBY
18
+ s.add_dependency('rails', '>=3.1')
19
+ s.add_dependency('prawn', '>= 0.12.0')
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.require_path = "lib"
24
+
25
+
26
+ s.has_rdoc = true
27
+ s.extra_rdoc_files = ["README.rdoc"]
28
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), '/../lib/prawnto.rb')
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/test_helper.rb")
2
+
3
+ class PrawntoControllerTest < ActionController::TestCase
4
+
5
+ class PrawntoControllerWithInlineTrue < PrawntoController
6
+ prawnto :inline=>true, :prawn=>{:page_orientation=>:landscape}
7
+ end
8
+
9
+ class PrawntoControllerWithInlineFalse < PrawntoController
10
+ prawnto :inline=>false
11
+ end
12
+
13
+
14
+ def test_inheritable_options
15
+ assert_equal({:page_orientation=>:landscape}, PrawntoControllerWithInlineTrue.prawn_hash)
16
+ assert_equal({:inline=>true}, PrawntoControllerWithInlineTrue.prawnto_hash)
17
+
18
+ #send a change and make sure it doesn't effect the others
19
+ @controller.send(:prawnto, {:inline=>false})
20
+ assert_equal({:inline=>false, :prawn => {}}, @controller.send(:compute_prawnto_options))
21
+ assert_equal({:inline=>true, :prawn=>{:page_orientation=>:landscape}}, PrawntoControllerWithInlineTrue.new.send(:compute_prawnto_options))
22
+ end
23
+
24
+ def test_computed_options
25
+ # controller = PrawntoController.new
26
+ @controller.send(:prawnto, {:inline=>false, :prawn=>{:page_orientation=>:landscape, :page_size=>'A4'}})
27
+ assert_equal({:inline=>false, :prawn=>{:page_orientation=>:landscape, :page_size=>'A4'}}, @controller.send(:compute_prawnto_options))
28
+ end
29
+
30
+
31
+ def test_default_settings
32
+ # controller = PrawntoController.new
33
+ assert_equal({:inline=>true, :prawn => {}}, @controller.send(:compute_prawnto_options))
34
+ end
35
+
36
+ end
37
+
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/test_helper.rb")
2
+ require 'template_handler_test_mocks'
3
+
4
+ class PrawntoControllerTest < ActionController::TestCase
5
+ include TemplateHandlerTestMocks
6
+
7
+ def setup
8
+ @view = ActionView.new
9
+ @handler = Prawnto::TemplateHandlers::Base.new(@view)
10
+ @controller = @view.controller
11
+ end
12
+
13
+ def test_headers_disposition_inline_and_filename
14
+ @controller.prawnto :filename=>'xxx.pdf', :inline=>true
15
+ @view.public_prawnto_compile_setup
16
+
17
+ assert_equal 'inline;filename="xxx.pdf"', @view.headers['Content-Disposition']
18
+ end
19
+
20
+ def test_headers_disposition_attachment_and_filename
21
+ @controller.prawnto :filename=>'xxx.pdf', :inline=>false
22
+ @view.public_prawnto_compile_setup
23
+
24
+ assert_equal 'attachment;filename="xxx.pdf"', @view.headers['Content-Disposition']
25
+ end
26
+
27
+ def test_headers_disposition_default
28
+ @view.public_prawnto_compile_setup
29
+
30
+ assert_equal 'inline', @view.headers['Content-Disposition']
31
+ end
32
+
33
+ end
34
+
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/test_helper.rb")
2
+ require 'template_handler_test_mocks'
3
+
4
+ class PrawntoControllerTest < ActionController::TestCase
5
+ include TemplateHandlerTestMocks
6
+
7
+ def setup
8
+ @view = ActionView.new
9
+ @handler = Prawnto::TemplateHandlers::Base.new(@view)
10
+ @controller = @view.controller
11
+ end
12
+
13
+ # I'm not sure what these are suppose to test, and they aren't currently working. - Forrest
14
+
15
+ # def test_prawnto_options_dsl_hash
16
+ # @y = 3231; @x = 5322
17
+ # @controller.prawnto :dsl=> {'x'=>:@x, :y=>'@y'}
18
+ # @view.public_prawnto_compile_setup
19
+ #
20
+ # source = @handler.build_source_to_establish_locals(Template.new(""))
21
+ #
22
+ # assert_equal @x, eval(source + "\nx")
23
+ # assert_equal @y, eval(source + "\ny")
24
+ # end
25
+ #
26
+ # def test_prawnto_options_dsl_array
27
+ # @y = 3231; @x = 5322
28
+ # @controller.prawnto :dsl=> ['x', :@y]
29
+ # @handler.pull_prawnto_options
30
+ # source = @handler.build_source_to_establish_locals(Template.new(""))
31
+ #
32
+ # assert_equal @x, eval(source + "\nx")
33
+ # assert_equal @y, eval(source + "\ny")
34
+ # end
35
+
36
+ end
37
+
@@ -0,0 +1,72 @@
1
+ module TemplateHandlerTestMocks
2
+ #
3
+ # class Template
4
+ # attr_reader :source, :locals, :filename
5
+ #
6
+ # def initialize(source, locals={})
7
+ # @source = source
8
+ # @locals = locals
9
+ # @filename = "blah.pdf"
10
+ # end
11
+ # end
12
+ #
13
+
14
+
15
+ class Request
16
+ def env
17
+ {}
18
+ end
19
+
20
+ def ssl?
21
+ false
22
+ end
23
+ end
24
+
25
+ class ActionController
26
+ include Prawnto::ActionControllerMixin
27
+
28
+ def response
29
+ @response ||= ActionDispatch::TestResponse.new
30
+ end
31
+
32
+ def request
33
+ @request ||= Request.new
34
+ end
35
+
36
+ def headers
37
+ response.headers
38
+ end
39
+ end
40
+
41
+ class ActionView
42
+ include Prawnto::ActionViewMixin
43
+
44
+ def controller
45
+ @controller ||= ActionController.new
46
+ end
47
+
48
+ def response
49
+ controller.response
50
+ end
51
+
52
+ def request
53
+ controller.request
54
+ end
55
+
56
+ def headers
57
+ controller.headers
58
+ end
59
+
60
+ def prawnto_options
61
+ controller.get_instance_variable(:@prawnto_options)
62
+ end
63
+
64
+ def public_prawnto_compile_setup
65
+ _prawnto_compile_setup
66
+ end
67
+
68
+ end
69
+
70
+
71
+ end
72
+
@@ -0,0 +1,17 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'tempfile'
6
+
7
+ require 'active_support'
8
+ require 'action_controller'
9
+ require 'action_view'
10
+
11
+ require 'test/unit'
12
+
13
+ require File.join(File.dirname(__FILE__), '/../lib/prawnto.rb')
14
+ Prawnto.init_both
15
+
16
+ class PrawntoController < ActionController::Base
17
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawnto_2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Forrest
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &2162199040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2162199040
25
+ - !ruby/object:Gem::Dependency
26
+ name: prawn
27
+ requirement: &2162198580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.12.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2162198580
36
+ description: Simple PDF generation using the prawn library.
37
+ email:
38
+ - development@forrestzeisler.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - MIT-LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - lib/prawnto.rb
50
+ - lib/prawnto/action_controller_mixin.rb
51
+ - lib/prawnto/action_view_mixin.rb
52
+ - lib/prawnto/railtie.rb
53
+ - lib/prawnto/template_handler/compile_support.rb
54
+ - lib/prawnto/template_handlers/base.rb
55
+ - lib/prawnto/template_handlers/dsl.rb
56
+ - prawnto.gemspec
57
+ - rails/init.rb
58
+ - test/action_controller_test.rb
59
+ - test/base_template_handler_test.rb
60
+ - test/dsl_template_handler_test.rb
61
+ - test/template_handler_test_mocks.rb
62
+ - test/test_helper.rb
63
+ homepage: http://cracklabs.com/prawnto
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: 1.3.6
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.10
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: This gem allows you to use the PDF mime-type and the simple prawn syntax
87
+ to generate impressive looking PDFs.
88
+ test_files:
89
+ - test/action_controller_test.rb
90
+ - test/base_template_handler_test.rb
91
+ - test/dsl_template_handler_test.rb
92
+ - test/template_handler_test_mocks.rb
93
+ - test/test_helper.rb