Created
July 10, 2014 21:31
-
-
Save spalenza/d21b6532b74dfe07d0de to your computer and use it in GitHub Desktop.
Access controller's methods in anywhere.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module StoreController | |
@@controllers = Hash.new | |
Finalizer = lambda { |id| | |
@@controllers.delete id | |
} | |
class << self | |
def set_controller(controller) | |
unless @@controllers.has_key?(Thread.current.object_id) | |
ObjectSpace.define_finalizer Thread.current, Finalizer | |
end if RUBY_VERSION != "1.9.3" | |
@@controllers[Thread.current.object_id] = controller | |
end | |
def get_controller | |
@@controllers[Thread.current.object_id] | |
end | |
def unset_controller | |
Finalizer.call(Thread.current.object_id) | |
end | |
end | |
module Model | |
# ADICIONAR AO MODEL | |
# include StoreController::Model | |
# store_controller_method :current_customer do |controller| | |
# controller && controller.current_customer | |
# end | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def store_controller_method(method_name, &block) | |
define_method(method_name) { block.call(StoreController.get_controller) } | |
end | |
end | |
end | |
module Controller | |
# ADICIONAR AO CONTROLLER | |
# include StoreController::Controller | |
extend ActiveSupport::Concern | |
included do | |
before_filter :set_store_controller | |
after_filter :unset_store_controller | |
end | |
private | |
def set_store_controller | |
StoreController.set_controller(self) | |
end | |
def unset_store_controller | |
StoreController.unset_controller | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment