annotate .svn/pristine/78/78a37a0a3569b9f852eefa9ba8497b7e7f0b8f99.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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