Skip to content

Commit

Permalink
Only instantiate HelperProxy once
Browse files Browse the repository at this point in the history
  • Loading branch information
blaknite committed May 18, 2016
1 parent 55d1882 commit 104ae07
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
18 changes: 15 additions & 3 deletions lib/blush.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ module Blush
end

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

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

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

##
# Get the cached view context
module_function def view_context
@view_context ||= ApplicationController.new.view_context
end

end
6 changes: 1 addition & 5 deletions lib/blush/helper_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ 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)
@view_context.send(method, *args, &block)
Blush.view_context.send(method, *args, &block)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/blush/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Railtie < Rails::Railtie
self.class_eval do
include Blush::ViewContext

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

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

Expand Down

0 comments on commit 104ae07

Please sign in to comment.