1
0
mirror of https://github.com/meineerde/redmine.git synced 2026-03-11 19:53:07 +00:00

Hooks initial commit.

git-svn-id: http://redmine.rubyforge.org/svn/branches/work@1717 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2008-08-07 18:24:35 +00:00
parent 7c56968018
commit f892fcf716
4 changed files with 225 additions and 2 deletions

View File

@ -14,8 +14,9 @@
body {behavior: url(<%= stylesheet_path "csshover.htc" %>);}
</style>
<![endif]-->
<!-- page specific tags --><%= yield :header_tags %>
<%= call_hook :view_layouts_base_html_head, :project => @project %>
<!-- page specific tags -->
<%= yield :header_tags -%>
</head>
<body>
<div id="wrapper">

View File

@ -4,6 +4,7 @@ require 'redmine/activity'
require 'redmine/mime_type'
require 'redmine/core_ext'
require 'redmine/themes'
require 'redmine/hook'
require 'redmine/plugin'
begin

116
hooks/lib/redmine/hook.rb Normal file
View File

@ -0,0 +1,116 @@
# Redmine - project management software
# Copyright (C) 2006-2008 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module Redmine
module Hook
HOOKS = %w(view_layouts_base_html_head).collect {|h| h.to_sym}
@@listener_classes = []
@@listeners = nil
@@hook_listeners = {}
class << self
# Adds a listener class.
# Automatically called when a class inherits from Redmine::Hook::Listener.
def add_listener(klass)
raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
@@listener_classes << klass
clear_listeners_instances
end
# Returns all the listerners instances.
def listeners
@@listeners ||= @@listener_classes.collect {|listener| listener.instance}
end
# Returns the listeners instances for the given hook.
def hook_listeners(hook)
@@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
end
# Clears all the listeners.
def clear_listeners
@@listener_classes = []
clear_listeners_instances
end
# Clears all the listeners instances.
def clear_listeners_instances
@@listeners = nil
@@hook_listeners = {}
end
# 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
end
response
end
def hooks
HOOKS.dup
end
def valid_hook?(hook)
HOOKS.include?(hook)
end
end
# Base class for hook listeners.
class Listener
include Singleton
# Registers the listener
def self.inherited(child)
Redmine::Hook.add_listener(child)
super
end
end
# Listener class used for views hooks.
# Listeners that inherit this class will include various helpers by default.
class ViewListener < Listener
include ERB::Util
include ActionView::Helpers::TagHelper
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::JavaScriptHelper
include ActionView::Helpers::PrototypeHelper
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter
include ApplicationHelper
end
# Helper module included in ApplicationHelper so that hooks can be called
# in views like this:
# <%= call_hook(:some_hook) %>
# <%= call_hook(:another_hook, :foo => 'bar' %>
module Helper
def call_hook(hook, context={})
Redmine::Hook.call_hook(hook, context)
end
end
end
end
ApplicationHelper.send(:include, Redmine::Hook::Helper)

View File

@ -0,0 +1,105 @@
# redMine - project management software
# Copyright (C) 2006-2008 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.dirname(__FILE__) + '/../../../test_helper'
class Redmine::Hook::ManagerTest < Test::Unit::TestCase
# Some fake hooks that are manually registered in these tests
class FakeHook
include Singleton
end
class TestHook1 < FakeHook
def view_layouts_base_html_head(context)
'Test hook 1 listener.'
end
end
class TestHook2 < FakeHook
def view_layouts_base_html_head(context)
'Test hook 2 listener.'
end
end
class TestHook3 < FakeHook
def view_layouts_base_html_head(context)
"Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}."
end
end
def setup
@hook_module = Redmine::Hook
end
def teardown
@hook_module.clear_listeners
end
def test_sanity
assert true
end
def test_hooks_format
assert_kind_of Array, @hook_module.hooks
@hook_module.hooks.each do |hook|
assert_kind_of Symbol, hook
assert_kind_of Array, @hook_module.hook_listeners(hook)
assert_equal 0, @hook_module.hook_listeners(hook).length
end
end
def test_valid_hook
assert @hook_module.valid_hook?(:view_layouts_base_html_head)
end
def test_invalid_hook
assert !@hook_module.valid_hook?(:an_invalid_hook_name)
end
def test_clear_listeners
assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
@hook_module.add_listener(TestHook1)
@hook_module.add_listener(TestHook2)
assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size
@hook_module.clear_listeners
assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
end
def test_add_listener
assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
@hook_module.add_listener(TestHook1)
assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size
end
def test_call_hook
@hook_module.add_listener(TestHook1)
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')
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)
end
end