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