You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook.rb 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. module Redmine
  18. module Hook
  19. @@listener_classes = []
  20. @@listeners = nil
  21. @@hook_listeners = {}
  22. class << self
  23. # Adds a listener class.
  24. # Automatically called when a class inherits from Redmine::Hook::Listener.
  25. def add_listener(klass)
  26. raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
  27. @@listener_classes << klass
  28. clear_listeners_instances
  29. end
  30. # Returns all the listener instances.
  31. def listeners
  32. @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
  33. end
  34. # Returns the listener instances for the given hook.
  35. def hook_listeners(hook)
  36. @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
  37. end
  38. # Clears all the listeners.
  39. def clear_listeners
  40. @@listener_classes = []
  41. clear_listeners_instances
  42. end
  43. # Clears all the listeners instances.
  44. def clear_listeners_instances
  45. @@listeners = nil
  46. @@hook_listeners = {}
  47. end
  48. # Calls a hook.
  49. # Returns the listeners response.
  50. def call_hook(hook, context={})
  51. [].tap do |response|
  52. hls = hook_listeners(hook)
  53. if hls.any?
  54. hls.each {|listener| response << listener.send(hook, context)}
  55. end
  56. end
  57. end
  58. end
  59. # Helper module included in ApplicationHelper and ActionController so that
  60. # hooks can be called in views like this:
  61. #
  62. # <%= call_hook(:some_hook) %>
  63. # <%= call_hook(:another_hook, :foo => 'bar') %>
  64. #
  65. # Or in controllers like:
  66. # call_hook(:some_hook)
  67. # call_hook(:another_hook, :foo => 'bar')
  68. #
  69. # Hooks added to views will be concatenated into a string. Hooks added to
  70. # controllers will return an array of results.
  71. #
  72. # Several objects are automatically added to the call context:
  73. #
  74. # * project => current project
  75. # * request => Request instance
  76. # * controller => current Controller instance
  77. # * hook_caller => object that called the hook
  78. #
  79. module Helper
  80. def call_hook(hook, context={})
  81. if is_a?(ActionController::Base)
  82. default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self}
  83. Redmine::Hook.call_hook(hook, default_context.merge(context))
  84. else
  85. default_context = { :project => @project, :hook_caller => self }
  86. default_context[:controller] = controller if respond_to?(:controller)
  87. default_context[:request] = request if respond_to?(:request)
  88. Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe
  89. end
  90. end
  91. end
  92. end
  93. end