Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

148 lines
4.8 KiB
Ruby
Raw Permalink Normal View History

#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2011-05-30 20:52:25 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
2011-05-30 20:52:25 +02:00
#
2013-09-16 17:59:31 +02:00
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2013-09-16 17:59:31 +02:00
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# 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.
#
# 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.
#
# See COPYRIGHT and LICENSE files for more details.
#++
2021-05-03 20:58:15 +02:00
module OpenProject
2008-09-05 10:31:06 +00:00
module Hook
class << self
# Adds a listener class.
2021-05-03 20:58:15 +02:00
# Automatically called when a class inherits from OpenProject::Hook::Listener.
2008-09-05 10:31:06 +00:00
def add_listener(klass)
2021-05-03 20:58:15 +02:00
raise ArgumentError, "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
2021-02-11 16:02:18 +01:00
2021-05-03 20:58:15 +02:00
listener_classes << klass
2008-09-05 10:31:06 +00:00
clear_listeners_instances
end
2011-05-30 20:52:25 +02:00
2014-04-04 23:19:39 +02:00
# Returns all the listener instances.
2008-09-05 10:31:06 +00:00
def listeners
2021-05-03 20:58:15 +02:00
@listeners ||= listener_classes.map(&:instance)
end
def listener_classes
@listener_classes ||= []
2008-09-05 10:31:06 +00:00
end
2011-05-30 20:52:25 +02:00
2014-04-04 23:19:39 +02:00
# Returns the listener instances for the given hook.
2008-09-05 10:31:06 +00:00
def hook_listeners(hook)
2021-05-03 20:58:15 +02:00
@hook_listeners ||= {}
@hook_listeners[hook] ||= listeners.select { |listener| listener.respond_to?(hook) }
2008-09-05 10:31:06 +00:00
end
2011-05-30 20:52:25 +02:00
2008-09-05 10:31:06 +00:00
# Clears all the listeners.
def clear_listeners
2021-05-03 20:58:15 +02:00
@listener_classes = []
2008-09-05 10:31:06 +00:00
clear_listeners_instances
end
2011-05-30 20:52:25 +02:00
2008-09-05 10:31:06 +00:00
# Clears all the listeners instances.
def clear_listeners_instances
2021-05-03 20:58:15 +02:00
@listeners = nil
@hook_listeners = {}
2008-09-05 10:31:06 +00:00
end
2011-05-30 20:52:25 +02:00
2008-09-05 10:31:06 +00:00
# Calls a hook.
# Returns the listeners response.
2014-11-03 21:53:03 +01:00
def call_hook(hook, context = {})
[].tap do |response|
2019-11-06 08:22:47 +01:00
hook_listeners(hook).each do |listener|
response << listener.send(hook, context)
rescue StandardError => e
msg = "Failed to collect hook response for #{hook} from #{listener.inspect}"
::OpenProject.logger.error(msg, exception: e, extra: { hook_name: hook })
end
2008-09-05 10:31:06 +00:00
end
end
end
# Base class for hook listeners.
class Listener
include Singleton
include Redmine::I18n
2008-09-05 10:31:06 +00:00
# Registers the listener
def self.inherited(child)
2021-05-03 20:58:15 +02:00
OpenProject::Hook.add_listener(child)
2008-09-05 10:31:06 +00:00
super
end
end
2008-09-05 10:31:06 +00: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
2014-05-19 11:30:08 +02:00
include Sprockets::Rails::Helper
2008-09-05 10:31:06 +00:00
include ActionView::Helpers::TextHelper
2012-08-07 14:54:55 +02:00
include Rails.application.routes.url_helpers
2008-09-05 10:31:06 +00:00
include ApplicationHelper
# Default to creating links using only the path. Subclasses can
# change this default as needed
def self.default_url_options
{
2014-11-03 21:11:16 +01:00
host: OpenProject::StaticRouting::UrlHelpers.host,
only_path: true,
script_name: OpenProject::Configuration.rails_relative_url_root
}
end
2011-05-30 20:52:25 +02:00
# Helper method to directly render a partial using the context:
2011-05-30 20:52:25 +02:00
#
2021-05-03 20:58:15 +02:00
# class MyHook < OpenProject::Hook::ViewListener
2015-10-29 19:12:02 +01:00
# render_on :view_issues_show_details_bottom, partial: "show_more_data"
# end
#
2014-11-03 21:53:03 +01:00
def self.render_on(hook, options = {})
define_method hook do |context|
2012-08-07 14:54:55 +02:00
if context[:hook_caller].respond_to?(:render)
2014-11-03 21:53:03 +01:00
context[:hook_caller].send(:render, { locals: context }.merge(options))
2012-08-07 14:54:55 +02:00
elsif context[:controller].is_a?(ActionController::Base)
2014-11-03 21:53:03 +01:00
context[:controller].send(:render_to_string, { locals: context }.merge(options))
2012-08-07 14:54:55 +02:00
else
2014-11-03 21:53:03 +01:00
raise "Cannot render #{name} hook from #{context[:hook_caller].class.name}"
2012-08-07 14:54:55 +02:00
end
end
end
2012-08-07 14:54:55 +02:00
def controller
nil
end
2012-08-07 14:54:55 +02:00
def config
ActionController::Base.config
end
2008-09-05 10:31:06 +00:00
end
end
end