1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Simplified structure after testing in an actual app

This commit is contained in:
Jeff Casimir 2011-10-08 01:37:33 -04:00
parent 42823e517d
commit e350a9a85d
6 changed files with 25 additions and 42 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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