annotate .svn/pristine/78/78a37a0a3569b9f852eefa9ba8497b7e7f0b8f99.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module Hook
Chris@1295 20 @@listener_classes = []
Chris@1295 21 @@listeners = nil
Chris@1295 22 @@hook_listeners = {}
Chris@1295 23
Chris@1295 24 class << self
Chris@1295 25 # Adds a listener class.
Chris@1295 26 # Automatically called when a class inherits from Redmine::Hook::Listener.
Chris@1295 27 def add_listener(klass)
Chris@1295 28 raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
Chris@1295 29 @@listener_classes << klass
Chris@1295 30 clear_listeners_instances
Chris@1295 31 end
Chris@1295 32
Chris@1295 33 # Returns all the listerners instances.
Chris@1295 34 def listeners
Chris@1295 35 @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
Chris@1295 36 end
Chris@1295 37
Chris@1295 38 # Returns the listeners instances for the given hook.
Chris@1295 39 def hook_listeners(hook)
Chris@1295 40 @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
Chris@1295 41 end
Chris@1295 42
Chris@1295 43 # Clears all the listeners.
Chris@1295 44 def clear_listeners
Chris@1295 45 @@listener_classes = []
Chris@1295 46 clear_listeners_instances
Chris@1295 47 end
Chris@1295 48
Chris@1295 49 # Clears all the listeners instances.
Chris@1295 50 def clear_listeners_instances
Chris@1295 51 @@listeners = nil
Chris@1295 52 @@hook_listeners = {}
Chris@1295 53 end
Chris@1295 54
Chris@1295 55 # Calls a hook.
Chris@1295 56 # Returns the listeners response.
Chris@1295 57 def call_hook(hook, context={})
Chris@1295 58 [].tap do |response|
Chris@1295 59 hls = hook_listeners(hook)
Chris@1295 60 if hls.any?
Chris@1295 61 hls.each {|listener| response << listener.send(hook, context)}
Chris@1295 62 end
Chris@1295 63 end
Chris@1295 64 end
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 # Base class for hook listeners.
Chris@1295 68 class Listener
Chris@1295 69 include Singleton
Chris@1295 70 include Redmine::I18n
Chris@1295 71
Chris@1295 72 # Registers the listener
Chris@1295 73 def self.inherited(child)
Chris@1295 74 Redmine::Hook.add_listener(child)
Chris@1295 75 super
Chris@1295 76 end
Chris@1295 77
Chris@1295 78 end
Chris@1295 79
Chris@1295 80 # Listener class used for views hooks.
Chris@1295 81 # Listeners that inherit this class will include various helpers by default.
Chris@1295 82 class ViewListener < Listener
Chris@1295 83 include ERB::Util
Chris@1295 84 include ActionView::Helpers::TagHelper
Chris@1295 85 include ActionView::Helpers::FormHelper
Chris@1295 86 include ActionView::Helpers::FormTagHelper
Chris@1295 87 include ActionView::Helpers::FormOptionsHelper
Chris@1295 88 include ActionView::Helpers::JavaScriptHelper
Chris@1295 89 include ActionView::Helpers::NumberHelper
Chris@1295 90 include ActionView::Helpers::UrlHelper
Chris@1295 91 include ActionView::Helpers::AssetTagHelper
Chris@1295 92 include ActionView::Helpers::TextHelper
Chris@1295 93 include Rails.application.routes.url_helpers
Chris@1295 94 include ApplicationHelper
Chris@1295 95
Chris@1295 96 # Default to creating links using only the path. Subclasses can
Chris@1295 97 # change this default as needed
Chris@1295 98 def self.default_url_options
Chris@1295 99 {:only_path => true }
Chris@1295 100 end
Chris@1295 101
Chris@1295 102 # Helper method to directly render a partial using the context:
Chris@1295 103 #
Chris@1295 104 # class MyHook < Redmine::Hook::ViewListener
Chris@1295 105 # render_on :view_issues_show_details_bottom, :partial => "show_more_data"
Chris@1295 106 # end
Chris@1295 107 #
Chris@1295 108 def self.render_on(hook, options={})
Chris@1295 109 define_method hook do |context|
Chris@1295 110 if context[:hook_caller].respond_to?(:render)
Chris@1295 111 context[:hook_caller].send(:render, {:locals => context}.merge(options))
Chris@1295 112 elsif context[:controller].is_a?(ActionController::Base)
Chris@1295 113 context[:controller].send(:render_to_string, {:locals => context}.merge(options))
Chris@1295 114 else
Chris@1295 115 raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}"
Chris@1295 116 end
Chris@1295 117 end
Chris@1295 118 end
Chris@1295 119
Chris@1295 120 def controller
Chris@1295 121 nil
Chris@1295 122 end
Chris@1295 123
Chris@1295 124 def config
Chris@1295 125 ActionController::Base.config
Chris@1295 126 end
Chris@1295 127 end
Chris@1295 128
Chris@1295 129 # Helper module included in ApplicationHelper and ActionController so that
Chris@1295 130 # hooks can be called in views like this:
Chris@1295 131 #
Chris@1295 132 # <%= call_hook(:some_hook) %>
Chris@1295 133 # <%= call_hook(:another_hook, :foo => 'bar') %>
Chris@1295 134 #
Chris@1295 135 # Or in controllers like:
Chris@1295 136 # call_hook(:some_hook)
Chris@1295 137 # call_hook(:another_hook, :foo => 'bar')
Chris@1295 138 #
Chris@1295 139 # Hooks added to views will be concatenated into a string. Hooks added to
Chris@1295 140 # controllers will return an array of results.
Chris@1295 141 #
Chris@1295 142 # Several objects are automatically added to the call context:
Chris@1295 143 #
Chris@1295 144 # * project => current project
Chris@1295 145 # * request => Request instance
Chris@1295 146 # * controller => current Controller instance
Chris@1295 147 # * hook_caller => object that called the hook
Chris@1295 148 #
Chris@1295 149 module Helper
Chris@1295 150 def call_hook(hook, context={})
Chris@1295 151 if is_a?(ActionController::Base)
Chris@1295 152 default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self}
Chris@1295 153 Redmine::Hook.call_hook(hook, default_context.merge(context))
Chris@1295 154 else
Chris@1295 155 default_context = { :project => @project, :hook_caller => self }
Chris@1295 156 default_context[:controller] = controller if respond_to?(:controller)
Chris@1295 157 default_context[:request] = request if respond_to?(:request)
Chris@1295 158 Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe
Chris@1295 159 end
Chris@1295 160 end
Chris@1295 161 end
Chris@1295 162 end
Chris@1295 163 end
Chris@1295 164
Chris@1295 165 ApplicationHelper.send(:include, Redmine::Hook::Helper)
Chris@1295 166 ActionController::Base.send(:include, Redmine::Hook::Helper)