annotate lib/redmine/hook.rb @ 1621:3a510bf6a9bc

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