annotate .svn/pristine/fd/fd17adbe8ddb5fa8d4ce1ceca6975396fcf0109b.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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