Permalink
Browse files

major refactoring of the layout and method names.

  • Loading branch information...
1 parent a9e025a commit 4a3a7bebb29c03eb43d286700fdd6f8a553840eb @forrest committed Sep 18, 2011
View
@@ -1,4 +1,5 @@
-Copyright (c) 2008 cracklabs.com
+Copyright on Updates - Copyright (c) 2011 OctopusApp Inc. (http://getjobber.com), released under the MIT license
+Original Copyright - Copyright (c) 2008 cracklabs.com, released under the MIT license
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -2,8 +2,6 @@
A rails plugin providing templating abilities for generating pdf files leveraging the new kick-ass prawn library (http://prawn.majesticseacreature.com/)
-Full (slightly out of date) documentation/demos at: http://cracklabs.com/prawnto
-
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.
<b>Note:</b> It has not been tested with earlier versions of Rails.
@@ -18,6 +16,9 @@ In your <code>Gemfile</code>:
On it's way... This will be added to the {wiki}[http://github.com/forrest/prawnto/wiki]
+In the meantime you can read the original (slightly out of date) documentation/demos at: http://cracklabs.com/prawnto
+
+
== Contributing & Reporting Issues
While I can't promise the most speedy response, I do plan to continue to maintain this gem. Please feel free to report any issues, provide feedback, or make suggestions on the {Issue Tracker}[http://github.com/forrest/prawnto/issues]
@@ -26,6 +27,6 @@ While I can't promise the most speedy response, I do plan to continue to maintai
---
-Copyright on Updates - Copyright (c) 2011 getjobber.com, released under the MIT license
+Copyright on Updates - Copyright (c) 2011 OctopusApp Inc. (http://getjobber.com)
Original Copyright - Copyright (c) 2008 cracklabs.com, released under the MIT license
View
@@ -5,33 +5,33 @@
module Prawnto
VERSION='0.1.0'
+
autoload :ActionControllerMixin, 'prawnto/action_controller_mixin'
autoload :ActionViewMixin, 'prawnto/action_view_mixin'
module TemplateHandlers
autoload :Base, 'prawnto/template_handlers/base'
autoload :Dsl, 'prawnto/template_handlers/dsl'
end
- module TemplateHandler
- autoload :CompileSupport, 'prawnto/template_handler/compile_support'
- end
-
class << self
- def run_includes
- ActionController::Base.send :include, Prawnto::ActionControllerMixin
- ActionView::Base.send :include, Prawnto::ActionViewMixin
- end
-
- def register_handlers
+ # Register the MimeType and the two template handlers.
+ def on_init
Mime::Type.register "application/pdf", :pdf unless defined?(Mime::PDF)
ActionView::Template.register_template_handler 'prawn', Prawnto::TemplateHandlers::Base
ActionView::Template.register_template_handler 'prawn_dsl', Prawnto::TemplateHandlers::Dsl
end
+ # Includes the mixins for ActionController and ActionView.
+ def on_load
+ ActionController::Base.send :include, Prawnto::ActionControllerMixin
+ ActionView::Base.send :include, Prawnto::ActionViewMixin
+ end
+
+ # Runs the registration and include methods. This is used for testing only as the Railtie.rb usually runs these individually.
def init_both
- run_includes
- register_handlers
+ on_init
+ on_load
end
end
@@ -1,53 +1,15 @@
+require "prawnto/action_controller_mixins/options_manager"
+require "prawnto/action_controller_mixins/header_methods"
+
module Prawnto
module ActionControllerMixin
-
- DEFAULT_PRAWNTO_OPTIONS = {:inline=>true}
-
def self.included(base)
- base.class_attribute :prawn_hash, :prawnto_hash
- base.prawn_hash = {}
- base.prawnto_hash = {}
- base.extend ClassMethods
- end
-
- module ClassMethods
-
- # Sets options in the class attributes. Can be pulled into the instance variable with :compute_prawnto_options
- def prawnto(options)
- prawn_options, prawnto_options = breakdown_prawnto_options options
- self.prawn_hash = prawn_options
- self.prawnto_hash = DEFAULT_PRAWNTO_OPTIONS.dup.merge(prawnto_options)
+ base.class_eval do
+ include HeaderMethods
+ include OptionsManager
end
-
- private
-
- # splits the :prawn key out into a seperate hash
- def breakdown_prawnto_options(options)
- prawnto_options = options.dup
- prawn_options = (prawnto_options.delete(:prawn) || {}).dup
- [prawn_options, prawnto_options]
- end
- end
-
-
- # Sets options directly on the instance
- def prawnto(options)
- @prawnto_options ||= {}
- @prawnto_options.merge! options
- end
-
-
- private
-
- # Used to set the @prawnto_options variable before rendering. Called from compile_support just before rendering.
- def compute_prawnto_options
- @prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
- @prawnto_options[:prawn] ||= {}
- @prawnto_options[:prawn].merge!(self.class.prawn_hash || {}) {|k,o,n| o}
- @prawnto_options.merge!(self.class.prawnto_hash || {}) {|k,o,n| o}
- @prawnto_options
end
-
+
end
end
@@ -0,0 +1,68 @@
+module Prawnto
+ module ActionControllerMixin
+ module HeaderMethods
+
+
+ def set_headers
+ compute_prawnto_options
+
+ unless defined?(ActionMailer) && defined?(ActionMailer::Base) && self.is_a?(ActionMailer::Base)
+ set_pragma
+ set_cache_control
+ set_content_type
+ set_disposition
+ set_other_headers_for_ie_ssl
+ end
+ end
+
+ private
+
+ # TODO: kept around from railspdf-- maybe not needed anymore? should check.
+ def ie_request?
+ request.env['HTTP_USER_AGENT'] =~ /msie/i
+ end
+ # memoize :ie_request?
+
+ # added to make ie happy with ssl pdf's (per naisayer)
+ def ssl_request?
+ request.ssl?
+ end
+ # memoize :ssl_request?
+
+ def set_other_headers_for_ie_ssl
+ return unless ssl_request? && ie_request?
+ headers['Content-Description'] = 'File Transfer'
+ headers['Content-Transfer-Encoding'] = 'binary'
+ headers['Expires'] = '0'
+ end
+
+ # TODO: kept around from railspdf-- maybe not needed anymore? should check.
+ def set_pragma
+ if ssl_request? && ie_request?
+ headers['Pragma'] = 'public' # added to make ie ssl pdfs work (per naisayer)
+ else
+ headers['Pragma'] ||= ie_request? ? 'no-cache' : ''
+ end
+ end
+
+ # TODO: kept around from railspdf-- maybe not needed anymore? should check.
+ def set_cache_control
+ if ssl_request? && ie_request?
+ headers['Cache-Control'] = 'maxage=1' # added to make ie ssl pdfs work (per naisayer)
+ else
+ headers['Cache-Control'] ||= ie_request? ? 'no-cache, must-revalidate' : ''
+ end
+ end
+
+ def set_content_type
+ response.content_type ||= Mime::PDF
+ end
+
+ def set_disposition
+ inline = @prawnto_options[:inline] ? 'inline' : 'attachment'
+ filename = @prawnto_options[:filename] ? "filename=\"#{@prawnto_options[:filename]}\"" : nil
+ headers["Content-Disposition"] = [inline,filename].compact.join(';')
+ end
+ end
+ end
+end
@@ -0,0 +1,56 @@
+module Prawnto
+ module ActionControllerMixin
+ module OptionsManager
+ DEFAULT_PRAWNTO_OPTIONS = {:inline=>true}
+
+ def self.included(base)
+ base.send :attr_reader, :prawnto_options
+ base.class_attribute :prawn_hash, :prawnto_hash
+ base.prawn_hash = {}
+ base.prawnto_hash = {}
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+
+ # This is the class setter. It lets you set default options for all prawn actions within a controller.
+ def prawnto(options)
+ prawn_options, prawnto_options = breakdown_prawnto_options options
+ self.prawn_hash = prawn_options
+ self.prawnto_hash = DEFAULT_PRAWNTO_OPTIONS.dup.merge(prawnto_options)
+ end
+
+ private
+
+ # splits the :prawn key out into a seperate hash
+ def breakdown_prawnto_options(options)
+ prawnto_options = options.dup
+ prawn_options = (prawnto_options.delete(:prawn) || {}).dup
+ [prawn_options, prawnto_options]
+ end
+ end
+
+ # Sets the prawn options. Use in the controller method.
+ #
+ # respond_to {|format|
+ # format.pdf { prawnto(:page_orientation => :landscape) }
+ # }
+ def prawnto(options)
+ @prawnto_options ||= {}
+ @prawnto_options.merge! options
+ end
+
+ private
+
+ # this merges the default prawnto options, the controller prawnto options, and the instance prawnto options, and the splits out then joins in the :prawn options.
+ # This is called when setting the header information just before render.
+ def compute_prawnto_options
+ @prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
+ @prawnto_options[:prawn] ||= {}
+ @prawnto_options[:prawn].merge!(self.class.prawn_hash || {}) {|k,o,n| o}
+ @prawnto_options.merge!(self.class.prawnto_hash || {}) {|k,o,n| o}
+ @prawnto_options
+ end
+ end
+ end
+end
@@ -1,10 +1,11 @@
module Prawnto
module ActionViewMixin
- private
+ private
+
def _prawnto_compile_setup
- compile_support = Prawnto::TemplateHandler::CompileSupport.new(controller)
- @prawnto_options = compile_support.options
+ controller.set_headers
+ @prawnto_options = controller.prawnto_options
end
end
View
@@ -3,12 +3,12 @@ class Railtie < Rails::Railtie
# This runs once during initialization.
initializer "prawnto.register_handlers" do
- Prawnto.register_handlers
+ Prawnto.on_init
end
# This will run it once in production and before each load in development.
config.to_prepare do
- Prawnto.run_includes
+ Prawnto.on_load
end
end
@@ -1,82 +0,0 @@
-module Prawnto
- module TemplateHandler
-
- class CompileSupport
- extend ActiveSupport::Memoizable
-
- attr_reader :options
-
- def initialize(controller)
- @controller = controller
- @options = pull_options
- set_headers
- end
-
- # This sets the @options hash which is used for rendering.
- def pull_options
- @controller.send :compute_prawnto_options || {}
- end
-
- def set_headers
- unless defined?(ActionMailer) && defined?(ActionMailer::Base) && @controller.is_a?(ActionMailer::Base)
- set_pragma
- set_cache_control
- set_content_type
- set_disposition
- set_other_headers_for_ie_ssl
- end
- end
-
- # TODO: kept around from railspdf-- maybe not needed anymore? should check.
- def ie_request?
- @controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
- end
- memoize :ie_request?
-
- # added to make ie happy with ssl pdf's (per naisayer)
- def ssl_request?
- @controller.request.ssl?
- end
- memoize :ssl_request?
-
- def set_other_headers_for_ie_ssl
- return unless ssl_request? && ie_request?
- @controller.headers['Content-Description'] = 'File Transfer'
- @controller.headers['Content-Transfer-Encoding'] = 'binary'
- @controller.headers['Expires'] = '0'
- end
-
- # TODO: kept around from railspdf-- maybe not needed anymore? should check.
- def set_pragma
- if ssl_request? && ie_request?
- @controller.headers['Pragma'] = 'public' # added to make ie ssl pdfs work (per naisayer)
- else
- @controller.headers['Pragma'] ||= ie_request? ? 'no-cache' : ''
- end
- end
-
- # TODO: kept around from railspdf-- maybe not needed anymore? should check.
- def set_cache_control
- if ssl_request? && ie_request?
- @controller.headers['Cache-Control'] = 'maxage=1' # added to make ie ssl pdfs work (per naisayer)
- else
- @controller.headers['Cache-Control'] ||= ie_request? ? 'no-cache, must-revalidate' : ''
- end
- end
-
- def set_content_type
- @controller.response.content_type ||= Mime::PDF
- end
-
- def set_disposition
- inline = options[:inline] ? 'inline' : 'attachment'
- filename = options[:filename] ? "filename=\"#{options[:filename]}\"" : nil
- @controller.headers["Content-Disposition"] = [inline,filename].compact.join(';')
- end
-
- end
-
- end
-end
-
-
View
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.description = 'Simple PDF generation using the prawn library.'
s.summary = "This gem allows you to use the PDF mime-type and the simple prawn syntax to generate impressive looking PDFs."
- s.homepage = "http://cracklabs.com/prawnto"
+ # s.homepage = "http://cracklabs.com/prawnto"
s.required_rubygems_version = ">= 1.3.6"
s.platform = Gem::Platform::RUBY

0 comments on commit 4a3a7be

Please sign in to comment.