Rewrote technique for getting view_context/helper access

This commit is contained in:
Jeff Casimir 2011-10-08 01:06:19 -04:00
parent 6c10302cd3
commit 39fc5c9513
8 changed files with 90 additions and 8 deletions

View File

@ -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

View File

@ -101,7 +101,7 @@ module Draper
#
# @return [Object] proxy
def helpers
@helpers ||= ApplicationController::all_helpers
@helpers ||= ApplicationController.current_view_context
end
alias :h :helpers

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
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

View File

@ -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

37
spec/view_context_spec.rb Normal file
View File

@ -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