Skip to content

Commit

Permalink
Basic Partial Working.
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest committed Feb 28, 2012
1 parent f4ed43e commit 201ea8f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ Sometimes you need to be able to render a PDF from anywhere (Background process

== Tips & Tricks

While layouts and partials aren't yet supported, helper methods can be called from within the PDF views. To help keep PDF's DRY, I would recommend creating a <code>PdfHelper</code> class for complex functionality. Don't forget to add this helper to your emailer if you generate PDF's as attachments.
Helper methods can be called from within the PDF views. To help keep PDF's DRY, I would recommend creating a <code>PdfHelper</code> class for complex functionality. Don't forget to add this helper to your emailer if you generate PDF's as attachments.

Limited partial support is available. Check out the wiki for details.

== Testing

Expand Down
28 changes: 24 additions & 4 deletions lib/prawnto/template_handlers/partials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@ module Prawnto
module TemplateHandlers
module Partials

def render_partial(path, prawn_object = pdf)
# get the path
# get file contents (should include a cache somehow?)
# prawn_object.instance_eval partial_contents
def partial!(partial_name, prawn_object = pdf)
prawn_object.instance_eval partial_source(partial_name)
end

private

def partial_source(partial_name)
#todo: implement some caching
File.open( get_file_path(partial_name)).read
end

def get_file_path(partial_name)
root_path = Rails.root
view_path = File.join(root_path, "app/views/")
partial_path = cleaned_path(partial_name)

file_path = Dir[File.join(view_path, partial_path + ".{*.}prawn")].first
file_path
end

def cleaned_path(provided_partial_path)
*provided_path, file_name = provided_partial_path.split("/")
file_name = "_"+file_name unless file_name[0] == "_"
File.join(provided_path, file_name)
end

end
Expand Down
Binary file added spec/assets/partial_render-0.12.0.pdf
Binary file not shown.
13 changes: 9 additions & 4 deletions spec/dummy/app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ def default_render
@x = 1
render :action => "default_render"
end

def dsl_render
@x = 1
render :action => "dsl_render"
end

def instance_var_test
@x = 1
render :action => "instance_var_test"
end

def yield_block_in_helper_test
render :action => "yield_block_in_helper_test"
end


def partial_render
@x = 1
render :action => "partial_render"
end

end
1 change: 1 addition & 0 deletions spec/dummy/app/views/test/_simple_partial.pdf.prawn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text "inside partial"
3 changes: 3 additions & 0 deletions spec/dummy/app/views/test/partial_render.pdf.prawn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
text "before partial"
partial! "test/simple_partial"
text "after partial"
3 changes: 2 additions & 1 deletion spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Rails.application.routes.draw do
get "/default_render" => "test#default_render"
get "/partial_render" => "test#partial_render"
get "/dsl_render" => "test#dsl_render"
get "/instance_var_test" => "test#instance_var_test"
get "/yield_block_in_helper_test" => "test#yield_block_in_helper_test"

root :to => "test#default_render"
end
27 changes: 19 additions & 8 deletions spec/integrations/test_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
require File.expand_path("../spec_helper.rb", File.dirname(__FILE__))

describe TestController do

describe "simple" do
it "returns correct PDF" do
get "/default_render.pdf"
response.should be_success

asset_binary = File.open(asset_path("default_render")).read.bytes.to_a
body_binary = response.body.bytes.to_a
body_binary.should == asset_binary
end


it "shares values/changes of instance vars between view and helpers" do
expect { get "/instance_var_test.pdf" }.should_not raise_error
end

it "should render items in a block passed to a helper" do
get "/yield_block_in_helper_test.pdf"
asset_binary = File.open(asset_path("yield_block_in_helper_test")).read.bytes.to_a
body_binary = response.body.bytes.to_a
body_binary.should == asset_binary
end
end


describe "dsl" do
it "returns correct PDF" do
get "/dsl_render.pdf"
response.should be_success

asset_binary = File.open(asset_path("dsl_render")).read.bytes.to_a
body_binary = response.body.bytes.to_a
body_binary.should == asset_binary
end
end

describe "partials" do
it "renders partials" do
get "/partial_render.pdf"
response.should be_success

asset_binary = File.open(asset_path("partial_render")).read.bytes.to_a
body_binary = response.body.bytes.to_a
body_binary.should == asset_binary
end
end

end
33 changes: 33 additions & 0 deletions spec/units/template_handlers/partials_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "spec_helper"
require "prawnto/template_handlers/partials.rb"

class PartialTester
include Prawnto::TemplateHandlers::Partials
end
describe "Prawnto::TemplateHandlers::Partials" do
subject do
PartialTester.new
end

describe "#get_file_path" do
before { Rails.stubs(:root).returns("") }

it "should find partial in the same folder" do
Dir.expects("[]").with("/app/views/test/_p1.{*.}prawn").returns("some/path")
path = subject.send :get_file_path, "test/p1"
end

it "won't insert underscore if provided" do
Dir.expects("[]").with("/app/views/test/_p1.{*.}prawn").returns("some/path")
path = subject.send :get_file_path, "test/_p1"
end

it "should look in the current folder first"
end

describe "#partial_source" do
end

describe "render_partial" do
end
end

0 comments on commit 201ea8f

Please sign in to comment.