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