annotate lib/redmine/hook.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 94944d00e43c
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2008 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@0 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@0 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 include ActionController::UrlWriter
Chris@0 21
Chris@0 22 @@listener_classes = []
Chris@0 23 @@listeners = nil
Chris@0 24 @@hook_listeners = {}
Chris@0 25
Chris@0 26 class << self
Chris@0 27 # Adds a listener class.
Chris@0 28 # Automatically called when a class inherits from Redmine::Hook::Listener.
Chris@0 29 def add_listener(klass)
Chris@0 30 raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
Chris@0 31 @@listener_classes << klass
Chris@0 32 clear_listeners_instances
Chris@0 33 end
Chris@0 34
Chris@0 35 # Returns all the listerners instances.
Chris@0 36 def listeners
Chris@0 37 @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
Chris@0 38 end
Chris@0 39
Chris@0 40 # Returns the listeners instances for the given hook.
Chris@0 41 def hook_listeners(hook)
Chris@0 42 @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
Chris@0 43 end
Chris@0 44
Chris@0 45 # Clears all the listeners.
Chris@0 46 def clear_listeners
Chris@0 47 @@listener_classes = []
Chris@0 48 clear_listeners_instances
Chris@0 49 end
Chris@0 50
Chris@0 51 # Clears all the listeners instances.
Chris@0 52 def clear_listeners_instances
Chris@0 53 @@listeners = nil
Chris@0 54 @@hook_listeners = {}
Chris@0 55 end
Chris@0 56
Chris@0 57 # Calls a hook.
Chris@0 58 # Returns the listeners response.
Chris@0 59 def call_hook(hook, context={})
Chris@0 60 returning [] do |response|
Chris@0 61 hls = hook_listeners(hook)
Chris@0 62 if hls.any?
Chris@0 63 hls.each {|listener| response << listener.send(hook, context)}
Chris@0 64 end
Chris@0 65 end
Chris@0 66 end
Chris@0 67 end
Chris@0 68
Chris@0 69 # Base class for hook listeners.
Chris@0 70 class Listener
Chris@0 71 include Singleton
Chris@0 72 include Redmine::I18n
Chris@0 73
Chris@0 74 # Registers the listener
Chris@0 75 def self.inherited(child)
Chris@0 76 Redmine::Hook.add_listener(child)
Chris@0 77 super
Chris@0 78 end
Chris@0 79
Chris@0 80 end
Chris@0 81
Chris@0 82 # Listener class used for views hooks.
Chris@0 83 # Listeners that inherit this class will include various helpers by default.
Chris@0 84 class ViewListener < Listener
Chris@0 85 include ERB::Util
Chris@0 86 include ActionView::Helpers::TagHelper
Chris@0 87 include ActionView::Helpers::FormHelper
Chris@0 88 include ActionView::Helpers::FormTagHelper
Chris@0 89 include ActionView::Helpers::FormOptionsHelper
Chris@0 90 include ActionView::Helpers::JavaScriptHelper
Chris@0 91 include ActionView::Helpers::PrototypeHelper
Chris@0 92 include ActionView::Helpers::NumberHelper
Chris@0 93 include ActionView::Helpers::UrlHelper
Chris@0 94 include ActionView::Helpers::AssetTagHelper
Chris@0 95 include ActionView::Helpers::TextHelper
Chris@0 96 include ActionController::UrlWriter
Chris@0 97 include ApplicationHelper
Chris@0 98
Chris@0 99 # Default to creating links using only the path. Subclasses can
Chris@0 100 # change this default as needed
Chris@0 101 def self.default_url_options
Chris@0 102 {:only_path => true }
Chris@0 103 end
Chris@0 104
Chris@0 105 # Helper method to directly render a partial using the context:
Chris@0 106 #
Chris@0 107 # class MyHook < Redmine::Hook::ViewListener
Chris@0 108 # render_on :view_issues_show_details_bottom, :partial => "show_more_data"
Chris@0 109 # end
Chris@0 110 #
Chris@0 111 def self.render_on(hook, options={})
Chris@0 112 define_method hook do |context|
Chris@0 113 context[:controller].send(:render_to_string, {:locals => context}.merge(options))
Chris@0 114 end
Chris@0 115 end
Chris@0 116 end
Chris@0 117
Chris@0 118 # Helper module included in ApplicationHelper and ActionControllerso that
Chris@0 119 # hooks can be called in views like this:
Chris@0 120 #
Chris@0 121 # <%= call_hook(:some_hook) %>
Chris@0 122 # <%= call_hook(:another_hook, :foo => 'bar' %>
Chris@0 123 #
Chris@0 124 # Or in controllers like:
Chris@0 125 # call_hook(:some_hook)
Chris@0 126 # call_hook(:another_hook, :foo => 'bar'
Chris@0 127 #
Chris@0 128 # Hooks added to views will be concatenated into a string. Hooks added to
Chris@0 129 # controllers will return an array of results.
Chris@0 130 #
Chris@0 131 # Several objects are automatically added to the call context:
Chris@0 132 #
Chris@0 133 # * project => current project
Chris@0 134 # * request => Request instance
Chris@0 135 # * controller => current Controller instance
Chris@0 136 #
Chris@0 137 module Helper
Chris@0 138 def call_hook(hook, context={})
Chris@0 139 if is_a?(ActionController::Base)
Chris@0 140 default_context = {:controller => self, :project => @project, :request => request}
Chris@0 141 Redmine::Hook.call_hook(hook, default_context.merge(context))
Chris@0 142 else
Chris@0 143 default_context = {:controller => controller, :project => @project, :request => request}
Chris@0 144 Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ')
Chris@0 145 end
Chris@0 146 end
Chris@0 147 end
Chris@0 148 end
Chris@0 149 end
Chris@0 150
Chris@0 151 ApplicationHelper.send(:include, Redmine::Hook::Helper)
Chris@0 152 ActionController::Base.send(:include, Redmine::Hook::Helper)