Commit 5b7a5c39 authored by Eric Davis's avatar Eric Davis

Added request and controller objects to the hooks by default.

The request and controller objects are now added to all hook contexts by
default.  This will also make url_for work better in hooks by setting up
the default_url_options :host, :port, and :protocol.

Finally a new helper method @render_or@ has been added to ViewListener.  This
will let a hook easily render a partial without a full method definition.

Thanks to Thomas Löber for the original patch.  #2542

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2429 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent a3fa56d9
......@@ -17,6 +17,8 @@
module Redmine
module Hook
include ActionController::UrlWriter
@@listener_classes = []
@@listeners = nil
@@hook_listeners = {}
......@@ -55,11 +57,22 @@ module Redmine
# 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"]
# Only set port if it's requested and isn't port 80. Otherwise a url
# like: +http://example.com:/url+ may be generated
if request.env["SERVER_PORT"] && request.env["SERVER_PORT"] != 80
default_url_options[:port] ||= request.env["SERVER_PORT"]
end
default_url_options[:protocol] ||= request.protocol
end
hls.each {|listener| response << listener.send(hook, context)}
end
end
response
end
end
......@@ -91,17 +104,48 @@ module Redmine
include ActionView::Helpers::TextHelper
include ActionController::UrlWriter
include ApplicationHelper
# Helper method to directly render a partial using the context:
#
# class MyHook < Redmine::Hook::ViewListener
# render_on :view_issues_show_details_bottom, :partial => "show_more_data"
# end
#
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
# in views like this:
# Helper module included in ApplicationHelper and ActionControllerso that
# hooks can be called in views like this:
#
# <%= call_hook(:some_hook) %>
# <%= call_hook(:another_hook, :foo => 'bar' %>
#
# Current project is automatically added to the call context.
# Or in controllers like:
# call_hook(:some_hook)
# call_hook(:another_hook, :foo => 'bar'
#
# Hooks added to views will be concatenated into a string. Hooks added to
# controllers will return an array of results.
#
# Several objects are automatically added to the call context:
#
# * project => current project
# * request => Request instance
# * controller => current Controller instance
#
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
......
......@@ -20,7 +20,7 @@ require File.dirname(__FILE__) + '/../../../test_helper'
class Redmine::Hook::ManagerTest < Test::Unit::TestCase
# Some hooks that are manually registered in these tests
class TestHook < Redmine::Hook::Listener; end
class TestHook < Redmine::Hook::ViewListener; end
class TestHook1 < TestHook
def view_layouts_base_html_head(context)
......@@ -39,6 +39,13 @@ class Redmine::Hook::ManagerTest < Test::Unit::TestCase
"Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}."
end
end
class TestLinkToHook < TestHook
def view_layouts_base_html_head(context)
link_to('Issues', :controller => 'issues')
end
end
Redmine::Hook.clear_listeners
def setup
......@@ -47,6 +54,7 @@ class Redmine::Hook::ManagerTest < Test::Unit::TestCase
def teardown
@hook_module.clear_listeners
@hook_module.default_url_options = { }
end
def test_clear_listeners
......@@ -67,17 +75,84 @@ class Redmine::Hook::ManagerTest < Test::Unit::TestCase
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
# Context: Redmine::Hook::call_hook
def test_call_hook_default_url_options_set
request = ActionController::TestRequest.new
request.env = { "SERVER_NAME" => 'example.com'}
@hook_module.add_listener(TestLinkToHook)
assert_equal ['<a href="http://example.com/issues">Issues</a>'],
@hook_module.call_hook(:view_layouts_base_html_head, :request => request)
end
def test_call_hook_default_url_options_set_with_no_standard_request_port
request = ActionController::TestRequest.new
request.env = { "SERVER_NAME" => 'example.com', "SERVER_PORT" => 3000}
@hook_module.add_listener(TestLinkToHook)
assert_equal ['<a href="http://example.com:3000/issues">Issues</a>'],
@hook_module.call_hook(:view_layouts_base_html_head, :request => request)
end
def test_call_hook_default_url_options_set_with_ssl
request = ActionController::TestRequest.new
request.env = { "SERVER_NAME" => 'example.com', "HTTPS" => 'on'}
@hook_module.add_listener(TestLinkToHook)
assert_equal ['<a href="https://example.com/issues">Issues</a>'],
@hook_module.call_hook(:view_layouts_base_html_head, :request => request)
end
def test_call_hook_default_url_options_set_with_forwarded_ssl
request = ActionController::TestRequest.new
request.env = { "SERVER_NAME" => 'example.com', "HTTP_X_FORWARDED_PROTO" => "https"}
@hook_module.add_listener(TestLinkToHook)
assert_equal ['<a href="https://example.com/issues">Issues</a>'],
@hook_module.call_hook(:view_layouts_base_html_head, :request => request)
end
# Context: Redmine::Hook::Helper.call_hook
def test_call_hook_with_project_added_to_context
# TODO: Implement test
end
def test_call_hook_from_controller_with_controller_added_to_context
# TODO: Implement test
end
def test_call_hook_from_controller_with_request_added_to_context
# TODO: Implement test
end
def test_call_hook_from_view_with_project_added_to_context
# TODO: Implement test
end
def test_call_hook_from_view_with_controller_added_to_context
# TODO: Implement test
end
def test_call_hook_from_view_with_request_added_to_context
# TODO: Implement test
end
def test_call_hook_from_view_should_join_responses_with_a_space
# TODO: Implement test
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment