Skip to content

Commit

Permalink
ensure the correct view_context is present by adding a before_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
blaknite committed May 18, 2016
1 parent b0cbdec commit 11d9527
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/blush.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ module Blush

##
# Cache the view context globally
module_function def view_context=(view_context)
@view_context = view_context
module_function def helpers=(helper_proxy)
@helpers = helper_proxy
end

##
# Get the cached view context
module_function def view_context
@view_context ||= ApplicationController.helpers
module_function def helpers
@helpers ||= Blush::HelperProxy.new(ApplicationController.new.view_context)
end

end
6 changes: 5 additions & 1 deletion lib/blush/helper_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ module Blush
##
# Proxy helper methods to the view context
class HelperProxy
def initialize(view_context)
@view_context = view_context
end

def method_missing(method, *args, &block)
Blush.view_context.send(method, *args, &block)
@view_context.send(method, *args, &block)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/blush/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(object)
##
# Access helpers from the view context through HelperProxy
def helpers
@helpers ||= Blush::HelperProxy.new
@helpers ||= Blush.helpers
end

##
Expand Down
9 changes: 8 additions & 1 deletion lib/blush/view_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ module ViewContext
# Taps into the call to view_context to caches it
def view_context
super.tap do |context|
Blush.view_context = context
set_blush_helpers(context)
end
end

##
# Sets the cached helper proxy based on the given view_context
def set_blush_helpers(context = nil)
context ||= view_context
Blush.helpers = Blush::HelperProxy.new(context)
end
end

end

0 comments on commit 11d9527

Please sign in to comment.