有一篇关于ActiveSupport::Concern
钩子的文章。这是 Rails 中的一个有效实现:
module ActionController
class Base < Metal
include AbstractController::Layouts
end
end
module AbstractController
module Layouts
extend ActiveSupport::Concern
include Rendering
included do
class_attribute :_layout, :_layout_conditions, :instance_accessor => false
self._layout = nil
self._layout_conditions = {}
_write_layout_method
end
module ClassMethods
...
end
end
end
module AbstractController
module Rendering
extend ActiveSupport::Concern
included do
class_attribute :protected_instance_variables
self.protected_instance_variables = []
end
module ClassMethods
...
end
end
end
在ruby 钩子之前如何执行extend
调用?必须先执行。毕竟,它的全部意义在于劫持ruby 默认并重新实现它。但是,根据 Ruby 文档,在您将此模块(例如)包含在另一个(例如)中之后立即执行。所以这里对我来说很困惑。如果是这种情况,则永远不会调用覆盖的。Layouts
append_features
extend
append_features
append_features
AbstractController::Layouts
ActionController::Base
append_features
ActiveSupport::Concern