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