From 39fc5c9513f69d14dbece70afc347ae7134debc5 Mon Sep 17 00:00:00 2001 From: Jeff Casimir Date: Sat, 8 Oct 2011 01:06:19 -0400 Subject: [PATCH] Rewrote technique for getting view_context/helper access --- lib/draper.rb | 1 + lib/draper/base.rb | 2 +- lib/draper/system.rb | 2 +- lib/draper/view_context.rb | 22 +++++++++++++++ spec/base_spec.rb | 5 ++++ spec/samples/application_controller.rb | 28 +++++++++++++++---- spec/spec_helper.rb | 1 - spec/view_context_spec.rb | 37 ++++++++++++++++++++++++++ 8 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 lib/draper/view_context.rb create mode 100644 spec/view_context_spec.rb diff --git a/lib/draper.rb b/lib/draper.rb index 8e87eab..6320405 100644 --- a/lib/draper.rb +++ b/lib/draper.rb @@ -4,5 +4,6 @@ require 'draper/all_helpers' require 'draper/base' require 'draper/lazy_helpers' require 'draper/model_support' +require 'draper/view_context' Draper::System.setup \ No newline at end of file diff --git a/lib/draper/base.rb b/lib/draper/base.rb index a79b4a2..054e965 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -101,7 +101,7 @@ module Draper # # @return [Object] proxy def helpers - @helpers ||= ApplicationController::all_helpers + @helpers ||= ApplicationController.current_view_context end alias :h :helpers diff --git a/lib/draper/system.rb b/lib/draper/system.rb index 6c84611..cbbfe40 100644 --- a/lib/draper/system.rb +++ b/lib/draper/system.rb @@ -1,7 +1,7 @@ module Draper class System def self.setup - ActionController::Base.send(:extend, Draper::AllHelpers) if defined?(ActionController::Base) + ApplicationController.send(:include, Draper::ViewContext) if defined?(ApplicationController) end end end \ No newline at end of file diff --git a/lib/draper/view_context.rb b/lib/draper/view_context.rb new file mode 100644 index 0000000..fb88903 --- /dev/null +++ b/lib/draper/view_context.rb @@ -0,0 +1,22 @@ +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 + end + + def self.included(source) + source.send(:include, InstanceMethods) + source.send(:extend, ClassMethods) + source.send(:before_filter, :set_current_view_context) + end + end +end \ No newline at end of file diff --git a/spec/base_spec.rb b/spec/base_spec.rb index 32840b9..6a16947 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -2,9 +2,14 @@ require 'spec_helper' require 'draper' describe Draper::Base do + before(:each){ ApplicationController.new.set_current_view_context } subject{ Draper::Base.new(source) } let(:source){ Product.new } + it "should get a valid view_context" do + ApplicationController.current_view_context.should be + end + context(".lazy_helpers") do it "makes Rails helpers available without using the h. proxy" do Draper::Base.lazy_helpers diff --git a/spec/samples/application_controller.rb b/spec/samples/application_controller.rb index 94d33d3..576991b 100644 --- a/spec/samples/application_controller.rb +++ b/spec/samples/application_controller.rb @@ -2,9 +2,27 @@ class ApplicationController extend ActionView::Helpers extend ActionView::Helpers::TagHelper extend ActionView::Helpers::UrlHelper - extend ApplicationHelper - - def self.all_helpers - self + extend ApplicationHelper + + def self.view_context + @@view_context ||= ApplicationController end -end \ No newline at end of file + + def self.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/spec_helper.rb b/spec/spec_helper.rb index 6817924..28d7235 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,6 @@ require 'rubygems' require 'bundler/setup' Bundler.require -require 'draper' require './spec/samples/application_helper.rb' Dir.glob(['./spec/samples/*.rb', './spec/support/*.rb']) do |file| require file diff --git a/spec/view_context_spec.rb b/spec/view_context_spec.rb new file mode 100644 index 0000000..000995f --- /dev/null +++ b/spec/view_context_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' +require 'draper' + +describe Draper::ViewContext do + let (:app_controller) do + ApplicationController + end + + let (:app_controller_instance) 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 + + it "raises an exception if the view_context is fetched without being set" do + Thread.current[:current_view_context] = nil + expect {app_controller.current_view_context}.should raise_exception(Exception) + end +end \ No newline at end of file