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