annotate .svn/pristine/45/4537efe545b75bf585f6f1b2f82d7cbf9d1195c2.svn-base @ 1494:e248c7af89ec redmine-2.4

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