Permalink
Browse files

Basic Partial Working.

  • Loading branch information...
forrest committed Feb 28, 2012
1 parent f4ed43e commit 201ea8fd5600788752fd2ec525439b5924cc7de9
View
@@ -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
@@ -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
Binary file not shown.
@@ -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
@@ -0,0 +1 @@
+text "inside partial"
@@ -0,0 +1,3 @@
+text "before partial"
+partial! "test/simple_partial"
+text "after partial"
@@ -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
@@ -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
@@ -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.