@@ -1,162 +1,159 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | #include ActionController::UrlWriter |
|
|||
21 |
|
||||
22 | @@listener_classes = [] |
|
20 | @@listener_classes = [] | |
23 | @@listeners = nil |
|
21 | @@listeners = nil | |
24 | @@hook_listeners = {} |
|
22 | @@hook_listeners = {} | |
25 |
|
23 | |||
26 | class << self |
|
24 | class << self | |
27 | # Adds a listener class. |
|
25 | # Adds a listener class. | |
28 | # Automatically called when a class inherits from Redmine::Hook::Listener. |
|
26 | # Automatically called when a class inherits from Redmine::Hook::Listener. | |
29 | def add_listener(klass) |
|
27 | def add_listener(klass) | |
30 | 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) | |
31 | @@listener_classes << klass |
|
29 | @@listener_classes << klass | |
32 | clear_listeners_instances |
|
30 | clear_listeners_instances | |
33 | end |
|
31 | end | |
34 |
|
32 | |||
35 | # Returns all the listerners instances. |
|
33 | # Returns all the listerners instances. | |
36 | def listeners |
|
34 | def listeners | |
37 | @@listeners ||= @@listener_classes.collect {|listener| listener.instance} |
|
35 | @@listeners ||= @@listener_classes.collect {|listener| listener.instance} | |
38 | end |
|
36 | end | |
39 |
|
37 | |||
40 | # Returns the listeners instances for the given hook. |
|
38 | # Returns the listeners instances for the given hook. | |
41 | def hook_listeners(hook) |
|
39 | def hook_listeners(hook) | |
42 | @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)} |
|
40 | @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)} | |
43 | end |
|
41 | end | |
44 |
|
42 | |||
45 | # Clears all the listeners. |
|
43 | # Clears all the listeners. | |
46 | def clear_listeners |
|
44 | def clear_listeners | |
47 | @@listener_classes = [] |
|
45 | @@listener_classes = [] | |
48 | clear_listeners_instances |
|
46 | clear_listeners_instances | |
49 | end |
|
47 | end | |
50 |
|
48 | |||
51 | # Clears all the listeners instances. |
|
49 | # Clears all the listeners instances. | |
52 | def clear_listeners_instances |
|
50 | def clear_listeners_instances | |
53 | @@listeners = nil |
|
51 | @@listeners = nil | |
54 | @@hook_listeners = {} |
|
52 | @@hook_listeners = {} | |
55 | end |
|
53 | end | |
56 |
|
54 | |||
57 | # Calls a hook. |
|
55 | # Calls a hook. | |
58 | # Returns the listeners response. |
|
56 | # Returns the listeners response. | |
59 | def call_hook(hook, context={}) |
|
57 | def call_hook(hook, context={}) | |
60 | [].tap do |response| |
|
58 | [].tap do |response| | |
61 | hls = hook_listeners(hook) |
|
59 | hls = hook_listeners(hook) | |
62 | if hls.any? |
|
60 | if hls.any? | |
63 | hls.each {|listener| response << listener.send(hook, context)} |
|
61 | hls.each {|listener| response << listener.send(hook, context)} | |
64 | end |
|
62 | end | |
65 | end |
|
63 | end | |
66 | end |
|
64 | end | |
67 | end |
|
65 | end | |
68 |
|
66 | |||
69 | # Base class for hook listeners. |
|
67 | # Base class for hook listeners. | |
70 | class Listener |
|
68 | class Listener | |
71 | include Singleton |
|
69 | include Singleton | |
72 | include Redmine::I18n |
|
70 | include Redmine::I18n | |
73 |
|
71 | |||
74 | # Registers the listener |
|
72 | # Registers the listener | |
75 | def self.inherited(child) |
|
73 | def self.inherited(child) | |
76 | Redmine::Hook.add_listener(child) |
|
74 | Redmine::Hook.add_listener(child) | |
77 | super |
|
75 | super | |
78 | end |
|
76 | end | |
79 |
|
77 | |||
80 | end |
|
78 | end | |
81 |
|
79 | |||
82 | # Listener class used for views hooks. |
|
80 | # Listener class used for views hooks. | |
83 | # Listeners that inherit this class will include various helpers by default. |
|
81 | # Listeners that inherit this class will include various helpers by default. | |
84 | class ViewListener < Listener |
|
82 | class ViewListener < Listener | |
85 | include ERB::Util |
|
83 | include ERB::Util | |
86 | include ActionView::Helpers::TagHelper |
|
84 | include ActionView::Helpers::TagHelper | |
87 | include ActionView::Helpers::FormHelper |
|
85 | include ActionView::Helpers::FormHelper | |
88 | include ActionView::Helpers::FormTagHelper |
|
86 | include ActionView::Helpers::FormTagHelper | |
89 | include ActionView::Helpers::FormOptionsHelper |
|
87 | include ActionView::Helpers::FormOptionsHelper | |
90 | include ActionView::Helpers::JavaScriptHelper |
|
88 | include ActionView::Helpers::JavaScriptHelper | |
91 | #include ActionView::Helpers::PrototypeHelper |
|
|||
92 | include ActionView::Helpers::NumberHelper |
|
89 | include ActionView::Helpers::NumberHelper | |
93 | include ActionView::Helpers::UrlHelper |
|
90 | include ActionView::Helpers::UrlHelper | |
94 | include ActionView::Helpers::AssetTagHelper |
|
91 | include ActionView::Helpers::AssetTagHelper | |
95 | include ActionView::Helpers::TextHelper |
|
92 | include ActionView::Helpers::TextHelper | |
96 | include Rails.application.routes.url_helpers |
|
93 | include Rails.application.routes.url_helpers | |
97 | include ApplicationHelper |
|
94 | include ApplicationHelper | |
98 |
|
95 | |||
99 | # Default to creating links using only the path. Subclasses can |
|
96 | # Default to creating links using only the path. Subclasses can | |
100 | # change this default as needed |
|
97 | # change this default as needed | |
101 | def self.default_url_options |
|
98 | def self.default_url_options | |
102 | {:only_path => true } |
|
99 | {:only_path => true } | |
103 | end |
|
100 | end | |
104 |
|
101 | |||
105 | # Helper method to directly render a partial using the context: |
|
102 | # Helper method to directly render a partial using the context: | |
106 | # |
|
103 | # | |
107 | # class MyHook < Redmine::Hook::ViewListener |
|
104 | # class MyHook < Redmine::Hook::ViewListener | |
108 | # render_on :view_issues_show_details_bottom, :partial => "show_more_data" |
|
105 | # render_on :view_issues_show_details_bottom, :partial => "show_more_data" | |
109 | # end |
|
106 | # end | |
110 | # |
|
107 | # | |
111 | def self.render_on(hook, options={}) |
|
108 | def self.render_on(hook, options={}) | |
112 | define_method hook do |context| |
|
109 | define_method hook do |context| | |
113 | context[:controller].send(:render_to_string, {:locals => context}.merge(options)) |
|
110 | context[:controller].send(:render_to_string, {:locals => context}.merge(options)) | |
114 | end |
|
111 | end | |
115 | end |
|
112 | end | |
116 |
|
113 | |||
117 | def controller |
|
114 | def controller | |
118 | nil |
|
115 | nil | |
119 | end |
|
116 | end | |
120 |
|
117 | |||
121 | def config |
|
118 | def config | |
122 | ActionController::Base.config |
|
119 | ActionController::Base.config | |
123 | end |
|
120 | end | |
124 | end |
|
121 | end | |
125 |
|
122 | |||
126 | # Helper module included in ApplicationHelper and ActionController so that |
|
123 | # Helper module included in ApplicationHelper and ActionController so that | |
127 | # hooks can be called in views like this: |
|
124 | # hooks can be called in views like this: | |
128 | # |
|
125 | # | |
129 | # <%= call_hook(:some_hook) %> |
|
126 | # <%= call_hook(:some_hook) %> | |
130 | # <%= call_hook(:another_hook, :foo => 'bar') %> |
|
127 | # <%= call_hook(:another_hook, :foo => 'bar') %> | |
131 | # |
|
128 | # | |
132 | # Or in controllers like: |
|
129 | # Or in controllers like: | |
133 | # call_hook(:some_hook) |
|
130 | # call_hook(:some_hook) | |
134 | # call_hook(:another_hook, :foo => 'bar') |
|
131 | # call_hook(:another_hook, :foo => 'bar') | |
135 | # |
|
132 | # | |
136 | # Hooks added to views will be concatenated into a string. Hooks added to |
|
133 | # Hooks added to views will be concatenated into a string. Hooks added to | |
137 | # controllers will return an array of results. |
|
134 | # controllers will return an array of results. | |
138 | # |
|
135 | # | |
139 | # Several objects are automatically added to the call context: |
|
136 | # Several objects are automatically added to the call context: | |
140 | # |
|
137 | # | |
141 | # * project => current project |
|
138 | # * project => current project | |
142 | # * request => Request instance |
|
139 | # * request => Request instance | |
143 | # * controller => current Controller instance |
|
140 | # * controller => current Controller instance | |
144 | # |
|
141 | # | |
145 | module Helper |
|
142 | module Helper | |
146 | def call_hook(hook, context={}) |
|
143 | def call_hook(hook, context={}) | |
147 | if is_a?(ActionController::Base) |
|
144 | if is_a?(ActionController::Base) | |
148 | default_context = {:controller => self, :project => @project, :request => request} |
|
145 | default_context = {:controller => self, :project => @project, :request => request} | |
149 | Redmine::Hook.call_hook(hook, default_context.merge(context)) |
|
146 | Redmine::Hook.call_hook(hook, default_context.merge(context)) | |
150 | else |
|
147 | else | |
151 | default_context = { :project => @project } |
|
148 | default_context = { :project => @project } | |
152 | default_context[:controller] = controller if respond_to?(:controller) |
|
149 | default_context[:controller] = controller if respond_to?(:controller) | |
153 | default_context[:request] = request if respond_to?(:request) |
|
150 | default_context[:request] = request if respond_to?(:request) | |
154 | Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe |
|
151 | Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe | |
155 | end |
|
152 | end | |
156 | end |
|
153 | end | |
157 | end |
|
154 | end | |
158 | end |
|
155 | end | |
159 | end |
|
156 | end | |
160 |
|
157 | |||
161 | ApplicationHelper.send(:include, Redmine::Hook::Helper) |
|
158 | ApplicationHelper.send(:include, Redmine::Hook::Helper) | |
162 | ActionController::Base.send(:include, Redmine::Hook::Helper) |
|
159 | ActionController::Base.send(:include, Redmine::Hook::Helper) |
General Comments 0
You need to be logged in to leave comments.
Login now