From e350a9a85d2cd06822004b1fa9232302c562c2ba Mon Sep 17 00:00:00 2001 From: Jeff Casimir Date: Sat, 8 Oct 2011 01:37:33 -0400 Subject: [PATCH] Simplified structure after testing in an actual app --- lib/draper/base.rb | 2 +- lib/draper/system.rb | 2 +- lib/draper/view_context.rb | 15 ++----------- spec/base_spec.rb | 6 ++++-- spec/samples/application_controller.rb | 30 +++++++++++++++----------- spec/view_context_spec.rb | 12 ----------- 6 files changed, 25 insertions(+), 42 deletions(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 054e965..69b70f9 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -101,7 +101,7 @@ module Draper # # @return [Object] proxy def helpers - @helpers ||= ApplicationController.current_view_context + Thread.current[:current_view_context] end alias :h :helpers diff --git a/lib/draper/system.rb b/lib/draper/system.rb index cbbfe40..26ced3a 100644 --- a/lib/draper/system.rb +++ b/lib/draper/system.rb @@ -1,7 +1,7 @@ module Draper class System def self.setup - ApplicationController.send(:include, Draper::ViewContext) if defined?(ApplicationController) + ActionController::Base.send(:include, Draper::ViewContext) if defined?(ActionController::Base) end end end \ No newline at end of file diff --git a/lib/draper/view_context.rb b/lib/draper/view_context.rb index fb88903..83807f7 100644 --- a/lib/draper/view_context.rb +++ b/lib/draper/view_context.rb @@ -1,21 +1,10 @@ module Draper module ViewContext - module ClassMethods - def current_view_context - Thread.current[:current_view_context] || - raise("set_current_view_context must be called from a before_filter") - end - end - - module InstanceMethods - def set_current_view_context - Thread.current[:current_view_context] ||= self.class.view_context - end + def set_current_view_context + Thread.current[:current_view_context] ||= self.view_context end def self.included(source) - source.send(:include, InstanceMethods) - source.send(:extend, ClassMethods) source.send(:before_filter, :set_current_view_context) end end diff --git a/spec/base_spec.rb b/spec/base_spec.rb index 6a16947..16ca67e 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -6,8 +6,10 @@ describe Draper::Base do subject{ Draper::Base.new(source) } let(:source){ Product.new } - it "should get a valid view_context" do - ApplicationController.current_view_context.should be + context(".helpers") do + it "should have a valid view_context" do + subject.helpers.should be + end end context(".lazy_helpers") do diff --git a/spec/samples/application_controller.rb b/spec/samples/application_controller.rb index 576991b..006ca35 100644 --- a/spec/samples/application_controller.rb +++ b/spec/samples/application_controller.rb @@ -1,28 +1,32 @@ -class ApplicationController +module ActionController + class Base + @@before_filters = [] + def self.before_filters + @@before_filters + end + def self.before_filter(name) + @@before_filters << name + end + end +end + +class ApplicationController < ActionController::Base extend ActionView::Helpers extend ActionView::Helpers::TagHelper extend ActionView::Helpers::UrlHelper extend ApplicationHelper - def self.view_context - @@view_context ||= ApplicationController + def view_context + @view_context ||= ApplicationController end - def self.view_context=(input) - @@view_context = input + def view_context=(input) + @view_context = input end def self.hello "Hello!" end - - @@before_filters = [] - def self.before_filters - @@before_filters - end - def self.before_filter(name) - @@before_filters << name - end end Draper::System.setup \ No newline at end of file diff --git a/spec/view_context_spec.rb b/spec/view_context_spec.rb index 000995f..cbc6ff1 100644 --- a/spec/view_context_spec.rb +++ b/spec/view_context_spec.rb @@ -10,22 +10,10 @@ describe Draper::ViewContext do app_controller.new end - it "implements .current_view_context" do - app_controller.should respond_to(:current_view_context) - end - it "implements #set_current_view_context" do app_controller_instance.should respond_to(:set_current_view_context) end - it "sets and returns the view context" do - fake_context = Object.new - Thread.current[:current_view_context] = nil - app_controller_instance.class.send(:view_context=, fake_context) - app_controller_instance.set_current_view_context - app_controller.current_view_context.should === fake_context - end - it "calls #before_filter with #set_current_view_context" do app_controller.before_filters.should include(:set_current_view_context) end