Skip to content

Commit

Permalink
Fixing instance variable problem
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest committed Jan 23, 2012
1 parent 4e7d6b0 commit f1a2bac
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ source "http://rubygems.org"
# Specify your gem's dependencies in my_project.gemspec
gemspec

gem "spork", "~> 0.9.0.rc9"
gem "spork", ">= 0.9.0"
gem "rspec-rails"
gem "mocha"
2 changes: 1 addition & 1 deletion lib/prawnto/model_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.to_string(template, calling_object = nil)
instance.prawnto :inline => true, :instance_variables_from => calling_object
end

return instance.render_to_string(:action => template, :template => false, :format => :pdf).html_safe
return instance.render_to_string(:action => template, :template => false, :formats => [:pdf]).html_safe
end

end
Expand Down
8 changes: 8 additions & 0 deletions lib/prawnto/template_handlers/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,24 @@ def copy_instance_variables_from(object, exclude = [])
vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
end

def push_instance_variables_to(object, exclude = [])
vars = instance_variables.map(&:to_s) - exclude.map(&:to_s)
vars.each { |name| object.instance_variable_set(name, instance_variable_get(name)) }
end

# This method is a little hacky with pushing the instance variables back. I would prefer to use bindings, but wasn't having much luck.
def method_missing(m, *args)
begin
super
rescue
if pdf.respond_to?(m.to_s)
pdf.send(m, *args)
elsif @calling_object.respond_to?(m.to_s)
push_instance_variables_to @calling_object
@calling_object.send(m, *args)
elsif @calling_object != @view_context and @view_context.respond_to?(m.to_s)
push_instance_variables_to @calling_object
@view_context.send(m, *args)
else
raise
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ def dsl_render
@x = 1
render :action => "dsl_render"
end

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

end
4 changes: 4 additions & 0 deletions spec/dummy/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module ApplicationHelper
def some_helper
"this is from a helper"
end

def x_output_helper
@x.to_s
end
end
2 changes: 1 addition & 1 deletion spec/dummy/app/models/super_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class SuperModel

def to_pdf
@x = 1
Prawnto::ModelRenderer.to_string "test/default_render.pdf", self
Prawnto::ModelRenderer.to_string "test/default_render", self
end

end
8 changes: 8 additions & 0 deletions spec/dummy/app/views/test/complex.pdf.prawn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
text "Complex Example"
text "@x = #{@x}"
@x = 2
text x_output_helper

if @x.to_s != x_output_helper
raise "Variable did not carry over"
end
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Rails.application.routes.draw do
get "/default_render" => "test#default_render"
get "/dsl_render" => "test#dsl_render"
get "/complex" => "test#complex"

root :to => "test#default_render"
end
8 changes: 6 additions & 2 deletions spec/integrations/test_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe TestController do

describe "GET default_render" do
describe "simple" do
it "returns correct PDF" do
get "/default_render.pdf"
response.should be_success
Expand All @@ -14,7 +14,7 @@
end


describe "GET dsl_render" do
describe "dsl" do
it "returns correct PDF" do
get "/dsl_render.pdf"
response.should be_success
Expand All @@ -25,4 +25,8 @@
end
end

it "uses changed instance values in helpers" do
expect { get "/complex.pdf" }.should_not raise_error
end

end

0 comments on commit f1a2bac

Please sign in to comment.