From 8e505565f9e7f8da86134e9a22f3dece3b7355a3 Mon Sep 17 00:00:00 2001 From: Istvan Hoka Date: Tue, 14 Aug 2012 14:18:14 +0300 Subject: [PATCH] Fixed view_context priming. Fixes #244, relates to 057ab4e8. Fix in 057ab4e8 resulted in a "NameError: undefined local variable or method `view_context' for Draper::ViewContext:Module". --- lib/draper/view_context.rb | 2 +- spec/draper/view_context_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/draper/view_context.rb b/lib/draper/view_context.rb index 29cf1a5..e9d3a8f 100644 --- a/lib/draper/view_context.rb +++ b/lib/draper/view_context.rb @@ -1,7 +1,7 @@ module Draper module ViewContext def self.current - Thread.current[:current_view_context] || view_context + Thread.current[:current_view_context] || ApplicationController.new.view_context end def self.current=(input) diff --git a/spec/draper/view_context_spec.rb b/spec/draper/view_context_spec.rb index f64535c..25e7ce9 100644 --- a/spec/draper/view_context_spec.rb +++ b/spec/draper/view_context_spec.rb @@ -2,6 +2,10 @@ require 'spec_helper' require 'draper/test/view_context' describe Draper::ViewContext do + before(:each) do + Thread.current[:current_view_context] = nil + end + let(:app_controller) { ApplicationController } let(:app_controller_instance) { app_controller.new } @@ -13,4 +17,14 @@ describe Draper::ViewContext do ctx = app_controller_instance.view_context Draper::ViewContext.current.should == ctx end + + describe "view_context priming" do + let(:app_controller_instance) { double(ApplicationController, :view_context => view_context) } + let(:view_context) { double("ApplicationController#view_context") } + + it "primes the view_context when nil" do + app_controller.should_receive(:new).and_return(app_controller_instance) + Draper::ViewContext.current.should == view_context + end + end end