comparison lib/redmine/hook.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children 622f24f53b42
comparison
equal deleted inserted replaced
929:5f33065ddc4b 1115:433d4f72a19b
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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.
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
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 ActionController::UrlWriter 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
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 if context[:hook_caller].respond_to?(:render)
111 context[:hook_caller].send(:render, {:locals => context}.merge(options))
112 elsif context[:controller].is_a?(ActionController::Base)
113 context[:controller].send(:render_to_string, {:locals => context}.merge(options))
114 else
115 raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}"
116 end
114 end 117 end
118 end
119
120 def controller
121 nil
122 end
123
124 def config
125 ActionController::Base.config
115 end 126 end
116 end 127 end
117 128
118 # Helper module included in ApplicationHelper and ActionController so that 129 # Helper module included in ApplicationHelper and ActionController so that
119 # hooks can be called in views like this: 130 # hooks can be called in views like this:
131 # Several objects are automatically added to the call context: 142 # Several objects are automatically added to the call context:
132 # 143 #
133 # * project => current project 144 # * project => current project
134 # * request => Request instance 145 # * request => Request instance
135 # * controller => current Controller instance 146 # * controller => current Controller instance
147 # * hook_caller => object that called the hook
136 # 148 #
137 module Helper 149 module Helper
138 def call_hook(hook, context={}) 150 def call_hook(hook, context={})
139 if is_a?(ActionController::Base) 151 if is_a?(ActionController::Base)
140 default_context = {:controller => self, :project => @project, :request => request} 152 default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self}
141 Redmine::Hook.call_hook(hook, default_context.merge(context)) 153 Redmine::Hook.call_hook(hook, default_context.merge(context))
142 else 154 else
143 default_context = {:controller => controller, :project => @project, :request => request} 155 default_context = { :project => @project, :hook_caller => self }
144 Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ') 156 default_context[:controller] = controller if respond_to?(:controller)
157 default_context[:request] = request if respond_to?(:request)
158 Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe
145 end 159 end
146 end 160 end
147 end 161 end
148 end 162 end
149 end 163 end