1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

Add callback so v4 extensions can see when other extensions are activated

This commit is contained in:
Thomas Reynolds 2013-06-04 15:51:03 -07:00
parent f3e2e8fdf5
commit 265f90e240
6 changed files with 63 additions and 1 deletions

View file

@ -0,0 +1,8 @@
Feature: v4 Extensions should have after_activated hooks
Scenario: Hello Helper
Given the Server is running at "v4-extension-callbacks"
Then going to "/index.html" should not raise an exception
And I should see "Extension One: true"
And I should see "Extension Two: true"

View file

@ -0,0 +1,26 @@
class ExtensionOne < ::Middleman::Extension
def initialize(app, options_hash={})
super
after_extension_activated :extension_two do
app.set :extension_two_was_activated, true
end
end
end
ExtensionOne.register
class ExtensionTwo < ::Middleman::Extension
def initialize(app, options_hash={})
super
after_extension_activated :extension_one do
app.set :extension_one_was_activated, true
end
end
end
ExtensionTwo.register
activate :extension_one
activate :extension_two

View file

@ -0,0 +1,2 @@
Extension One: <%= extension_one_was_activated.inspect %>
Extension Two: <%= extension_two_was_activated.inspect %>

View file

@ -142,6 +142,7 @@ module Middleman
# Search the root of the project for required files
$LOAD_PATH.unshift(root)
::Middleman::Extension.clear_after_extension_callbacks
run_hook :initialized
if config[:autoload_sprockets]
@ -178,7 +179,7 @@ module Middleman
run_hook :after_configuration
logger.debug "Loaded extensions:"
self.extensions.each do |ext,_|
self.extensions.each do |ext, klass|
if ext.is_a?(Hash)
ext.each do |k,_|
logger.debug "== Extension: #{k}"
@ -186,6 +187,10 @@ module Middleman
else
logger.debug "== Extension: #{ext}"
end
if klass.is_a?(::Middleman::Extension)
::Middleman::Extension.activated_extension(klass)
end
end
end
end

View file

@ -1,4 +1,5 @@
require "active_support/core_ext/class/attribute"
require "active_support/core_ext/module/delegation"
module Middleman
@ -131,11 +132,31 @@ module Middleman
def activate
new(::Middleman::Application)
end
def clear_after_extension_callbacks
@_extension_activation_callbacks = {}
end
def after_extension_activated(name, &block)
@_extension_activation_callbacks ||= {}
@_extension_activation_callbacks[name] ||= []
@_extension_activation_callbacks[name] << block if block_given?
end
def activated_extension(instance)
name = instance.class.extension_name
return unless @_extension_activation_callbacks && @_extension_activation_callbacks[name]
@_extension_activation_callbacks[name].each do |block|
block.arity == 1 ? block.call(instance) : block.call()
end
end
end
attr_accessor :options
attr_reader :app
delegate :after_extension_activated, :to => :"::Middleman::Extension"
def initialize(klass, options_hash={}, &block)
@_helpers = []
@klass = klass