Feature #2542 » hook.rb.revised.diff
| lib/redmine/hook.rb (Arbeitskopie) | ||
|---|---|---|
|
module Redmine
|
||
|
module Hook
|
||
|
include ActionController::UrlWriter
|
||
|
@@listener_classes = []
|
||
|
@@listeners = nil
|
||
|
@@hook_listeners = {}
|
||
| ... | ... | |
|
# Calls a hook.
|
||
|
# Returns the listeners response.
|
||
|
def call_hook(hook, context={})
|
||
|
response = ''
|
||
|
hook_listeners(hook).each do |listener|
|
||
|
response << listener.send(hook, context).to_s
|
||
|
returning [] do |response|
|
||
|
hls = hook_listeners(hook)
|
||
|
if hls.any?
|
||
|
request = context[:request]
|
||
|
if request
|
||
|
default_url_options[:host] ||= request.env["SERVER_NAME"]
|
||
|
default_url_options[:port] ||= request.env["SERVER_PORT"]
|
||
|
end
|
||
|
hls.each {|listener| response << listener.send(hook, context)}
|
||
|
end
|
||
|
end
|
||
|
response
|
||
|
end
|
||
|
end
|
||
| ... | ... | |
|
include ActionView::Helpers::TextHelper
|
||
|
include ActionController::UrlWriter
|
||
|
include ApplicationHelper
|
||
|
def self.render_on(hook, options={})
|
||
|
define_method hook do |context|
|
||
|
context[:controller].send(:render_to_string, {:locals => context}.merge(options))
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
# Helper module included in ApplicationHelper so that hooks can be called
|
||
| ... | ... | |
|
# Current project is automatically added to the call context.
|
||
|
module Helper
|
||
|
def call_hook(hook, context={})
|
||
|
Redmine::Hook.call_hook(hook, {:project => @project}.merge(context))
|
||
|
if is_a?(ActionController::Base)
|
||
|
ctx = {:controller => self, :project => @project, :request => request}
|
||
|
Redmine::Hook.call_hook(hook, ctx.merge(context))
|
||
|
else
|
||
|
ctx = {:controller => controller, :project => @project, :request => request}
|
||
|
Redmine::Hook.call_hook(hook, ctx.merge(context)).join
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| test/unit/lib/redmine/hook_test.rb (Arbeitskopie) | ||
|---|---|---|
|
|
||
|
def test_call_hook
|
||
|
@hook_module.add_listener(TestHook1)
|
||
|
assert_equal 'Test hook 1 listener.', @hook_module.call_hook(:view_layouts_base_html_head)
|
||
|
assert_equal ['Test hook 1 listener.'], @hook_module.call_hook(:view_layouts_base_html_head)
|
||
|
end
|
||
|
|
||
|
def test_call_hook_with_context
|
||
|
@hook_module.add_listener(TestHook3)
|
||
|
assert_equal 'Context keys: bar, foo.', @hook_module.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
|
||
|
assert_equal ['Context keys: bar, foo.'], @hook_module.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
|
||
|
end
|
||
|
|
||
|
def test_call_hook_with_multiple_listeners
|
||
|
@hook_module.add_listener(TestHook1)
|
||
|
@hook_module.add_listener(TestHook2)
|
||
|
assert_equal 'Test hook 1 listener.Test hook 2 listener.', @hook_module.call_hook(:view_layouts_base_html_head)
|
||
|
assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], @hook_module.call_hook(:view_layouts_base_html_head)
|
||
|
end
|
||
|
end
|
||