Skip to content

Commit

Permalink
support all helpers in view context
Browse files Browse the repository at this point in the history
  • Loading branch information
ursm committed Feb 24, 2012
1 parent a1ebf6e commit a44f8f5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
1 change: 0 additions & 1 deletion lib/active_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'active_decorator/version'
require 'active_decorator/decorator'
require 'active_decorator/railtie'

9 changes: 1 addition & 8 deletions lib/active_decorator/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
require 'action_view'

module ActiveDecorator
module Helpers
def method_missing(method, *args, &block)
super
#TODO need to make sure who raised the error?
rescue NoMethodError => no_method_error
begin
@@_decorator_view_proxy ||= DecoratorViewProxy.new
@@_decorator_view_proxy.send method, *args, &block
ActiveDecorator::ViewContext.current.send method, *args, &block
rescue NoMethodError
raise no_method_error
end
end

class DecoratorViewProxy
include ::ActionView::Helpers
end
end
end
2 changes: 2 additions & 0 deletions lib/active_decorator/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_decorator/view_context'
require 'rails'

module ActiveDecorator
Expand All @@ -8,6 +9,7 @@ class Railtie < ::Rails::Railtie
end
ActiveSupport.on_load(:action_controller) do
require 'active_decorator/monkey/abstract_controller/rendering'
ActionController::Base.send :include, ActiveDecorator::ViewContext::Filter
end
ActiveSupport.on_load(:action_mailer) do
require 'active_decorator/monkey/abstract_controller/rendering'
Expand Down
23 changes: 23 additions & 0 deletions lib/active_decorator/view_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ActiveDecorator
module ViewContext
class << self
def current
Thread.current[:view_context]
end

def current=(view_context)
Thread.current[:view_context] = view_context
end
end

module Filter
extend ActiveSupport::Concern

included do
before_filter do |controller|
ActiveDecorator::ViewContext.current = controller.view_context
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/fake_app/books/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<%= @book.link %>
<%= @book.cover_image %>
4 changes: 4 additions & 0 deletions spec/fake_app/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def upcased_title
def link
link_to title, 'http://example.com'
end

def cover_image
image_tag 'cover.png'
end
end

# controllers
Expand Down
1 change: 1 addition & 0 deletions spec/requests/action_view_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
within 'a' do
page.should have_content 'RHG'
end
page.should have_css('img')
end
end

0 comments on commit a44f8f5

Please sign in to comment.