Replaced prawnto plugin with rails31 compatible version.

This commit is contained in:
benni 2012-09-30 21:48:57 +02:00
parent 0a1c326f14
commit 78cc423a46
20 changed files with 5 additions and 678 deletions

View file

@ -20,6 +20,7 @@ gem 'jquery-rails'
gem 'mysql2'
gem "fastercsv"
gem "prawn", '<=0.6.3'
gem 'prawnto_2', :require => "prawnto" # Used for prawn view templates
gem 'haml-rails'
gem "will_paginate", "~> 3.0.pre2"
gem 'client_side_validations'

View file

@ -100,6 +100,9 @@ GEM
prawn-core
prawn-layout (0.3.2)
prawn-security (0.1.1)
prawnto_2 (0.2.5)
prawn (>= 0.6.0)
rails (>= 3.1)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
@ -174,6 +177,7 @@ DEPENDENCIES
meta_search
mysql2
prawn (<= 0.6.3)
prawnto_2
rails (= 3.2.8)
rails3_acts_as_paranoid (~> 0.1.4)
sass-rails (~> 3.2.3)

View file

@ -1,3 +0,0 @@
.DS_Store
**/*.swp
reference_pdfs

View file

@ -1,20 +0,0 @@
Copyright (c) 2008 cracklabs.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -1,12 +0,0 @@
Prawnto
=======
a rails (2.1) plugin, providing templating abilities
for generating pdf files leveraging the new kick-ass prawn library
full documentation/demos at: http://cracklabs.com/prawnto
Copyright (c) 2008 cracklabs.com, released under the MIT license

View file

@ -1,22 +0,0 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
desc 'Test the prawnto plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
desc 'Generate documentation for the prawnto plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Prawnto'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

View file

@ -1,7 +0,0 @@
require 'prawnto'
Mime::Type.register "application/pdf", :pdf
ActionView::Template.register_template_handler 'prawn', Prawnto::TemplateHandlers::Base
ActionView::Template.register_template_handler 'prawn_dsl', Prawnto::TemplateHandlers::Dsl
ActionView::Template.register_template_handler 'prawn_xxx', Prawnto::TemplateHandlers::Raw

View file

@ -1,28 +0,0 @@
require 'action_controller'
require 'action_view'
require 'prawn'
require 'prawnto/action_controller'
require 'prawnto/action_view'
require 'prawnto/template_handler/compile_support'
require 'prawnto/template_handlers/base'
require 'prawn/layout' # Added by benni
#require 'prawnto/template_handlers/raw'
# for now applying to all Controllers
# however, could reduce footprint by letting user mixin (i.e. include) only into controllers that need it
# but does it really matter performance wise to include in a controller that doesn't need it? doubtful-- depends how much of a hit the before_filter is i guess..
#
class ActionController::Base
include Prawnto::ActionController
end
class ActionView::Base
include Prawnto::ActionView
end

View file

@ -1,45 +0,0 @@
module Prawnto
module ActionController
DEFAULT_PRAWNTO_OPTIONS = {:inline=>true}
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def prawnto(options)
prawn_options, prawnto_options = breakdown_prawnto_options options
write_inheritable_hash(:prawn, prawn_options)
write_inheritable_hash(:prawnto, prawnto_options)
end
private
def breakdown_prawnto_options(options)
prawnto_options = options.dup
prawn_options = (prawnto_options.delete(:prawn) || {}).dup
[prawn_options, prawnto_options]
end
end
def prawnto(options)
@prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
@prawnto_options.merge! options
end
private
def compute_prawnto_options
@prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
@prawnto_options[:prawn] ||= {}
@prawnto_options[:prawn].merge!(self.class.read_inheritable_attribute(:prawn) || {}) {|k,o,n| o}
@prawnto_options.merge!(self.class.read_inheritable_attribute(:prawnto) || {}) {|k,o,n| o}
@prawnto_options
end
end
end

View file

@ -1,12 +0,0 @@
module Prawnto
module ActionView
private
def _prawnto_compile_setup(dsl_setup = false)
compile_support = Prawnto::TemplateHandler::CompileSupport.new(controller)
@prawnto_options = compile_support.options
end
end
end

View file

@ -1,72 +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
def pull_options
@controller.send :compute_prawnto_options || {}
end
def set_headers
set_pragma
set_cache_control
set_content_type
set_disposition
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.env['SERVER_PROTOCOL'].downcase == "https"
end
memoize :ssl_request?
# 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 file

@ -1,16 +0,0 @@
module Prawnto
module TemplateHandlers
class Base < ::ActionView::TemplateHandler
include ::ActionView::TemplateHandlers::Compilable
def compile(template)
"_prawnto_compile_setup;" +
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
"#{template.source}\n" +
"pdf.render;"
end
end
end
end

View file

@ -1,16 +0,0 @@
module Prawnto
module TemplateHandlers
class Dsl < Base
def compile(template)
"_prawnto_compile_setup(true);" +
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
"pdf.instance_eval do; #{template.source}\nend;" +
"pdf.render;"
end
end
end
end

View file

@ -1,64 +0,0 @@
module Prawnto
module TemplateHandlers
class Raw < Base
def compile(template)
#TODO: what's up with filename here? not used is it?
source,filename = massage_template_source(template)
"_prawnto_compile_setup;" +
# (filename ? "@prawnto_options[:filename] = filename" : "") +
source
end
# attr_reader :run_environment
GENERATE_REGULAR_EXPRESSION = /^\s*Prawn\:\:Document\.generate(\(?)(.*?)(\,(.*))?(\s*\)?\s+do(.*?))$/m
RENDER_FILE_REGULAR_EXPRESSION = /(\w+)\.render_file\(?(.*?)\)?\s*$/
=begin
def render(template)
setup_run_environment
pull_prawnto_options
source,filename = massage_template_source(template)
@prawnto_options[:filename] = filename if filename
build_headers
@run_environment.instance_eval(source, template.filename, 0) #run in anonymous class
end
protected
def setup_run_environment
@run_environment = Object.new
end
=end
protected
def massage_template_source(template)
source = template.source.dup
variable_name = '_pdf'
filename = nil
source.gsub! /^(\s*?)(\$LOAD_PATH)/, '\1#\2'
source.gsub! /^(\s*?)(require\(?\s*['"]rubygems['"]\s*\)?\s*)$/, '\1#\2'
source.gsub! /^(\s*?)(require\(?\s*['"]prawn['"]\s*\)?\s*)$/, '\1#\2'
if (source =~ GENERATE_REGULAR_EXPRESSION)
filename = $2
source.sub! GENERATE_REGULAR_EXPRESSION, "#{variable_name} = Prawn::Document.new\\1\\4\\5"
elsif (source =~ RENDER_FILE_REGULAR_EXPRESSION)
variable_name = $1
filename = $2
source.sub! RENDER_FILE_REGULAR_EXPRESSION, '#\0'
end
source.gsub! /^(\s*)(class\s|def\s).*?\n\1end/m do |match|
eval "class <<@run_environment; #{match}; end;"
"\n" * match.count("\n")
end
source += "\n[#{variable_name}.render,#{filename}]\n"
source
end
end
end
end

View file

@ -1,4 +0,0 @@
# desc "Explaining what the task does"
# task :prawnto do
# # Task goes here
# end

View file

@ -1,38 +0,0 @@
require 'rubygems'
require 'action_controller'
require 'action_controller/test_process'
require 'action_view'
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/prawnto'
class ActionControllerTest < Test::Unit::TestCase
class PrawntoController < ActionController::Base
prawnto :inline=>true, :prawn=>{:page_orientation=>:landscape}
def test
prawnto :inline=>false, :prawn=>{:page_size=>'A4'}
end
end
def test_inheritable_options
assert_equal({:page_orientation=>:landscape}, PrawntoController.read_inheritable_attribute(:prawn))
assert_equal({:inline=>true}, PrawntoController.read_inheritable_attribute(:prawnto))
end
def test_computed_options
controller = PrawntoController.new
test_process(controller)
assert_equal({:inline=>false, :prawn=>{:page_orientation=>:landscape, :page_size=>'A4'}}, controller.send(:compute_prawnto_options))
end
protected
def test_process(controller, action = "test")
request = ActionController::TestRequest.new
request.action = action
controller.process(request, ActionController::TestResponse.new)
end
end

View file

@ -1,39 +0,0 @@
require 'rubygems'
require 'test/unit'
require File.dirname(__FILE__) + '/template_handler_test_mocks'
require File.dirname(__FILE__) + '/../lib/prawnto'
#require File.dirname(__FILE__) + '/../init'
#TODO: ruby1.9: pull same testing scheme from Raw once we're on 1.9
class BaseTemplateHandlerTest < Test::Unit::TestCase
include TemplateHandlerTestMocks
def setup
@view = ActionView.new
@handler = Prawnto::TemplateHandlers::Base.new(@view)
@controller = @view.controller
end
def test_headers_disposition_inline_and_filename
@controller.prawnto :filename=>'xxx.pdf', :inline=>true
@handler.pull_prawnto_options
@handler.set_disposition
assert_equal 'inline;filename=xxx.pdf', @view.headers['Content-Disposition']
end
def test_headers_disposition_attachment_and_filename
@controller.prawnto :filename=>'xxx.pdf', :inline=>false
@handler.pull_prawnto_options
@handler.set_disposition
assert_equal 'attachment;filename=xxx.pdf', @view.headers['Content-Disposition']
end
def test_headers_disposition_default
@handler.pull_prawnto_options
@handler.set_disposition
assert_equal 'inline', @view.headers['Content-Disposition']
end
end

View file

@ -1,40 +0,0 @@
require 'rubygems'
require 'test/unit'
require File.dirname(__FILE__) + '/template_handler_test_mocks'
require File.dirname(__FILE__) + '/../lib/prawnto'
#require File.dirname(__FILE__) + '/../init'
#TODO: ruby1.9: pull same testing scheme from Raw once we're on 1.9
class DslTemplateHandlerTest < Test::Unit::TestCase
include TemplateHandlerTestMocks
def setup
@view = ActionView.new
@handler = Prawnto::TemplateHandlers::Dsl.new(@view)
@controller = @view.controller
end
def test_prawnto_options_dsl_hash
@y = 3231; @x = 5322
@controller.prawnto :dsl=> {'x'=>:@x, :y=>'@y'}
@handler.pull_prawnto_options
source = @handler.build_source_to_establish_locals(Template.new(""))
assert_equal @x, eval(source + "\nx")
assert_equal @y, eval(source + "\ny")
end
def test_prawnto_options_dsl_array
@y = 3231; @x = 5322
@controller.prawnto :dsl=> ['x', :@y]
@handler.pull_prawnto_options
source = @handler.build_source_to_establish_locals(Template.new(""))
assert_equal @x, eval(source + "\nx")
assert_equal @y, eval(source + "\ny")
end
end

View file

@ -1,163 +0,0 @@
# uncomment and edit below if you want to get off gem version
#$LOAD_PATH.unshift '~/cracklabs/vendor/gems/prawn-0.0.0.1/lib/' #to force picup of latest prawn (instead of stable gem)
require 'rubygems'
require 'action_controller'
require 'action_view'
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/prawnto'
require File.dirname(__FILE__) + '/template_handler_test_mocks'
class RawTemplateHandlerTest < Test::Unit::TestCase
include TemplateHandlerTestMocks
class ::ApplicationHelper
end
def setup
@view = ActionView.new
@handler = Prawnto::TemplateHandlers::Raw.new(@view)
end
def test_massage_template_source_header_comments
expected_commented_lines = [0,2,3]
source = <<EOS
require 'prawn'
require 'hello'
require "rubygems"
$LOAD_PATH.unshift blah blah
LOAD_PATH.unshift blah blah
EOS
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.each_with_index do |line, i|
method = expected_commented_lines.include?(i) ? :assert_match : :assert_no_match
self.send method, /^\s*\#/, line
end
end
def test_massage_template_source_generate
@handler.pull_prawnto_options
changed_lines = [0,2,3]
source = <<EOS
Prawn::Document.generate('hello.pdf') do |pdf|
end
EOS
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
assert_match(/^\s*(\S+)\s*\=\s*Prawn\:\:Document\.new\(?\s*\)?\s*do\s*\|pdf\|/, output_lines.first)
variable = $1
assert_match(/^\s*\[(\S+)\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
assert_equal variable, $1
end
def test_massage_template_source_new
@handler.pull_prawnto_options
unchanged_lines = [0,1,2]
source = <<EOS
x = Prawn::Document.new do |pdf|
text.blah blah blah
end
x.render_file('hello.pdf')
EOS
source_lines = source.split("\n")
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.each_with_index do |line, i|
method = unchanged_lines.include?(i) ? :assert_equal : :assert_not_equal
self.send method, source_lines[i], line
end
assert_match(/^\s*\#\s*x\.render\_file\(\'hello.pdf\'\)/, output_lines[3])
assert_match(/^\s*\[\s*x\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
end
def test_massage_template_source_classes_methods
source = <<EOS
class Foo
def initialize
@foo = true
end
end
def bar(*args)
if args[0]==true
z = false
end
end
EOS
@handler.send :setup_run_environment
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.pop
output_lines.each {|l| assert_match(/^\s*$/, l)}
assert @handler.run_environment.methods(false).include?('bar')
assert class <<@handler.run_environment; self; end.constants.include?('Foo')
end
CURRENT_PATH = Pathname('.').realpath
PRAWN_PATH = Pathname(Prawn::BASEDIR).realpath
REFERENCE_PATH = Pathname('reference_pdfs').realpath
INPUT_PATH = PRAWN_PATH + 'examples'
IGNORE_LIST = %w(table_bench ruport_formatter page_geometry)
INPUTS = INPUT_PATH.children.select {|p| p.extname==".rb" && !IGNORE_LIST.include?(p.basename('.rb').to_s)}
def self.ensure_reference_pdfs_are_recent
head_lines = (INPUT_PATH + "../.git/HEAD").read.split("\n")
head_hash = Hash[*head_lines.map {|line| line.split(':').map{|v| v.strip}}.flatten]
head_version = (INPUT_PATH + "../.git" + head_hash['ref'])
REFERENCE_PATH.mkpath
current_version = REFERENCE_PATH + 'HEAD'
if !current_version.exist? || current_version.read!=head_version.read
puts "\n!!!! reference pdfs are determined to be old-- repopulating...\n\n"
require 'fileutils'
FileUtils.instance_eval do
rm REFERENCE_PATH + '*', :force=>true
INPUTS.each do |path|
pre_brood = INPUT_PATH.children
cd INPUT_PATH
system("ruby #{path.basename}")
post_brood = INPUT_PATH.children
new_kids = post_brood - pre_brood
new_kids.each {|p| mv p, REFERENCE_PATH + p.basename}
cd CURRENT_PATH
end
cp head_version, current_version
end
else
puts "\n reference pdfs are current-- continuing...\n"
end
end
#TODO: ruby 1.9: uncomment below line when on 1.9
#ensure_reference_pdfs_are_recent
def assert_renders_correctly(name, path)
input_source = path.read
output_source = @handler.compile(Template.new(input_source))
value = @view.instance_eval output_source
reference = (REFERENCE_PATH + @view.prawnto_options[:filename]).read
message = "template: #{name}\n"
message += ">"*30 + " original template: " + ">"*20 + "\n"
message += input_source + "\n"*2
message += ">"*30 + " manipulated template: " + ">"*20 + "\n"
message += output_source + "\n" + "<"*60 + "\n"
assert_equal reference, value, message
end
#!!! Can't actually verify pdf equality until ruby 1.9
# (cuz hash orders are messed up otherwise and no other way to test equality at the moment)
INPUTS.each do |path|
name = path.basename('.rb')
define_method "test_template_should_render_correctly [template: #{name}] " do
# assert_renders_correctly name, path
assert true
end
end
end

View file

@ -1,77 +0,0 @@
require 'rubygems'
require File.dirname(__FILE__) + '/../lib/prawnto'
module TemplateHandlerTestMocks
class Template
attr_reader :source, :locals, :filename
def initialize(source, locals={})
@source = source
@locals = locals
@filename = "blah.pdf"
end
end
class Response
def initialize
@headers = {}
end
def headers
@headers
end
def content_type=(value)
end
end
class Request
def env
{}
end
end
class ActionController
include Prawnto::ActionController
def response
@response ||= Response.new
end
def request
@request ||= Request.new
end
def headers
response.headers
end
end
class ActionView
def controller
@controller ||= ActionController.new
end
def response
controller.response
end
def request
controller.request
end
def headers
controller.headers
end
def prawnto_options
controller.get_instance_variable(:@prawnto_options)
end
end
end