##// END OF EJS Templates
Included Redmine::Hook::Helper to ActionController::Base so call_hook...
Eric Davis -
r1977:8b3a8ac1b478
parent child
Show More
@@ -1,109 +1,110
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module Hook
20 20 @@listener_classes = []
21 21 @@listeners = nil
22 22 @@hook_listeners = {}
23 23
24 24 class << self
25 25 # Adds a listener class.
26 26 # Automatically called when a class inherits from Redmine::Hook::Listener.
27 27 def add_listener(klass)
28 28 raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
29 29 @@listener_classes << klass
30 30 clear_listeners_instances
31 31 end
32 32
33 33 # Returns all the listerners instances.
34 34 def listeners
35 35 @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
36 36 end
37 37
38 38 # Returns the listeners instances for the given hook.
39 39 def hook_listeners(hook)
40 40 @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
41 41 end
42 42
43 43 # Clears all the listeners.
44 44 def clear_listeners
45 45 @@listener_classes = []
46 46 clear_listeners_instances
47 47 end
48 48
49 49 # Clears all the listeners instances.
50 50 def clear_listeners_instances
51 51 @@listeners = nil
52 52 @@hook_listeners = {}
53 53 end
54 54
55 55 # Calls a hook.
56 56 # Returns the listeners response.
57 57 def call_hook(hook, context={})
58 58 response = ''
59 59 hook_listeners(hook).each do |listener|
60 60 response << listener.send(hook, context).to_s
61 61 end
62 62 response
63 63 end
64 64 end
65 65
66 66 # Base class for hook listeners.
67 67 class Listener
68 68 include Singleton
69 69
70 70 # Registers the listener
71 71 def self.inherited(child)
72 72 Redmine::Hook.add_listener(child)
73 73 super
74 74 end
75 75 end
76 76
77 77 # Listener class used for views hooks.
78 78 # Listeners that inherit this class will include various helpers by default.
79 79 class ViewListener < Listener
80 80 include ERB::Util
81 81 include ActionView::Helpers::TagHelper
82 82 include ActionView::Helpers::FormHelper
83 83 include ActionView::Helpers::FormTagHelper
84 84 include ActionView::Helpers::FormOptionsHelper
85 85 include ActionView::Helpers::JavaScriptHelper
86 86 include ActionView::Helpers::PrototypeHelper
87 87 include ActionView::Helpers::NumberHelper
88 88 include ActionView::Helpers::UrlHelper
89 89 include ActionView::Helpers::AssetTagHelper
90 90 include ActionView::Helpers::TextHelper
91 91 include ActionController::UrlWriter
92 92 include ApplicationHelper
93 93 end
94 94
95 95 # Helper module included in ApplicationHelper so that hooks can be called
96 96 # in views like this:
97 97 # <%= call_hook(:some_hook) %>
98 98 # <%= call_hook(:another_hook, :foo => 'bar' %>
99 99 #
100 100 # Current project is automatically added to the call context.
101 101 module Helper
102 102 def call_hook(hook, context={})
103 103 Redmine::Hook.call_hook(hook, {:project => @project}.merge(context))
104 104 end
105 105 end
106 106 end
107 107 end
108 108
109 109 ApplicationHelper.send(:include, Redmine::Hook::Helper)
110 ActionController::Base.send(:include, Redmine::Hook::Helper)
@@ -1,40 +1,44
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19 require 'application'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class ApplicationController; def rescue_action(e) raise e end; end
23 23
24 24 class ApplicationControllerTest < Test::Unit::TestCase
25 25 def setup
26 26 @controller = ApplicationController.new
27 27 @request = ActionController::TestRequest.new
28 28 @response = ActionController::TestResponse.new
29 29 end
30 30
31 31 # check that all language files are valid
32 32 def test_localization
33 33 lang_files_count = Dir["#{RAILS_ROOT}/lang/*.yml"].size
34 34 assert_equal lang_files_count, GLoc.valid_languages.size
35 35 GLoc.valid_languages.each do |lang|
36 36 assert set_language_if_valid(lang)
37 37 end
38 38 set_language_if_valid('en')
39 39 end
40
41 def test_call_hook_mixed_in
42 assert @controller.respond_to?(:call_hook)
43 end
40 44 end
General Comments 0
You need to be logged in to leave comments. Login now