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