Skip to content

Commit

Permalink
All helper methods now proxied through the current view_context
Browse files Browse the repository at this point in the history
  • Loading branch information
blaknite committed May 16, 2016
1 parent 4b3e343 commit a01a72c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/blush.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
require 'blush/configuration'
require 'blush/has_presenter'
require 'blush/helper_proxy'
require 'blush/presenter'
require 'blush/view_context'

module Blush
module_function def config
@config ||= Blush::Configuration.new
end

module_function def helpers=(view_context)
@helpers = view_context
end

module_function def helpers
@helpers ||= ActionView::Base.new
@helpers ||= ApplicationController.helpers
end
end

ActiveRecord::Base.send :extend, Blush::HasPresenter
ActionController::Base.send :include, Blush::ViewContext
7 changes: 7 additions & 0 deletions lib/blush/helper_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Blush
class HelperProxy
def method_missing(method, *args, &block)
Blush.helpers.send(method, *args, &block)
end
end
end
2 changes: 1 addition & 1 deletion lib/blush/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(object)
end

def helpers
Blush.helpers
@helpers ||= Blush::HelperProxy.new
end

alias_method :h, :helpers
Expand Down
9 changes: 9 additions & 0 deletions lib/blush/view_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Blush
module ViewContext
def view_context
super.tap do |context|
Blush.helpers = context
end
end
end
end

0 comments on commit a01a72c

Please sign in to comment.