Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: module Redmine Chris@909: module Hook Chris@909: include ActionController::UrlWriter Chris@909: Chris@909: @@listener_classes = [] Chris@909: @@listeners = nil Chris@909: @@hook_listeners = {} Chris@909: Chris@909: class << self Chris@909: # Adds a listener class. Chris@909: # Automatically called when a class inherits from Redmine::Hook::Listener. Chris@909: def add_listener(klass) Chris@909: raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton) Chris@909: @@listener_classes << klass Chris@909: clear_listeners_instances Chris@909: end Chris@909: Chris@909: # Returns all the listerners instances. Chris@909: def listeners Chris@909: @@listeners ||= @@listener_classes.collect {|listener| listener.instance} Chris@909: end Chris@909: Chris@909: # Returns the listeners instances for the given hook. Chris@909: def hook_listeners(hook) Chris@909: @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)} Chris@909: end Chris@909: Chris@909: # Clears all the listeners. Chris@909: def clear_listeners Chris@909: @@listener_classes = [] Chris@909: clear_listeners_instances Chris@909: end Chris@909: Chris@909: # Clears all the listeners instances. Chris@909: def clear_listeners_instances Chris@909: @@listeners = nil Chris@909: @@hook_listeners = {} Chris@909: end Chris@909: Chris@909: # Calls a hook. Chris@909: # Returns the listeners response. Chris@909: def call_hook(hook, context={}) Chris@909: [].tap do |response| Chris@909: hls = hook_listeners(hook) Chris@909: if hls.any? Chris@909: hls.each {|listener| response << listener.send(hook, context)} Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Base class for hook listeners. Chris@909: class Listener Chris@909: include Singleton Chris@909: include Redmine::I18n Chris@909: Chris@909: # Registers the listener Chris@909: def self.inherited(child) Chris@909: Redmine::Hook.add_listener(child) Chris@909: super Chris@909: end Chris@909: Chris@909: end Chris@909: Chris@909: # Listener class used for views hooks. Chris@909: # Listeners that inherit this class will include various helpers by default. Chris@909: class ViewListener < Listener Chris@909: include ERB::Util Chris@909: include ActionView::Helpers::TagHelper Chris@909: include ActionView::Helpers::FormHelper Chris@909: include ActionView::Helpers::FormTagHelper Chris@909: include ActionView::Helpers::FormOptionsHelper Chris@909: include ActionView::Helpers::JavaScriptHelper Chris@909: include ActionView::Helpers::PrototypeHelper Chris@909: include ActionView::Helpers::NumberHelper Chris@909: include ActionView::Helpers::UrlHelper Chris@909: include ActionView::Helpers::AssetTagHelper Chris@909: include ActionView::Helpers::TextHelper Chris@909: include ActionController::UrlWriter Chris@909: include ApplicationHelper Chris@909: Chris@909: # Default to creating links using only the path. Subclasses can Chris@909: # change this default as needed Chris@909: def self.default_url_options Chris@909: {:only_path => true } Chris@909: end Chris@909: Chris@909: # Helper method to directly render a partial using the context: Chris@909: # Chris@909: # class MyHook < Redmine::Hook::ViewListener Chris@909: # render_on :view_issues_show_details_bottom, :partial => "show_more_data" Chris@909: # end Chris@909: # Chris@909: def self.render_on(hook, options={}) Chris@909: define_method hook do |context| Chris@909: context[:controller].send(:render_to_string, {:locals => context}.merge(options)) Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Helper module included in ApplicationHelper and ActionController so that Chris@909: # hooks can be called in views like this: Chris@909: # Chris@909: # <%= call_hook(:some_hook) %> Chris@909: # <%= call_hook(:another_hook, :foo => 'bar') %> Chris@909: # Chris@909: # Or in controllers like: Chris@909: # call_hook(:some_hook) Chris@909: # call_hook(:another_hook, :foo => 'bar') Chris@909: # Chris@909: # Hooks added to views will be concatenated into a string. Hooks added to Chris@909: # controllers will return an array of results. Chris@909: # Chris@909: # Several objects are automatically added to the call context: Chris@909: # Chris@909: # * project => current project Chris@909: # * request => Request instance Chris@909: # * controller => current Controller instance Chris@909: # Chris@909: module Helper Chris@909: def call_hook(hook, context={}) Chris@909: if is_a?(ActionController::Base) Chris@909: default_context = {:controller => self, :project => @project, :request => request} Chris@909: Redmine::Hook.call_hook(hook, default_context.merge(context)) Chris@909: else Chris@909: default_context = {:controller => controller, :project => @project, :request => request} Chris@909: Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ') Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: ApplicationHelper.send(:include, Redmine::Hook::Helper) Chris@909: ActionController::Base.send(:include, Redmine::Hook::Helper)