Project

General

Profile

Patch #1677 » hook.rb

Jean-Philippe Lang, 2008-08-05 18:29

 
1
module Redmine
2
  module Hook
3
    HOOKS = %w(issue_show project_member_list_header project_member_list_row).collect {|h| h.to_sym}
4
    
5
    @@listener_classes = []
6
    @@listeners = nil
7
    @@hook_listeners = {}
8
    
9
    class << self
10
      def add_listener(klass)
11
        @@listener_classes << klass
12
      end
13
      
14
      def listeners
15
        @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
16
      end
17
 
18
      def hook_listeners(hook)
19
        @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
20
      end
21
      
22
      def call_hook(hook, context={})
23
        response = ''
24
        hook_listeners(hook).each do |listener|
25
          response << listener.send(hook, context).to_s
26
        end
27
        response
28
      end
29
    end
30

    
31
    class Listener
32
      include Singleton
33

    
34
      def self.inherited(child)
35
        Redmine::Hook.add_listener(child)
36
        super
37
      end
38
    end
39
    
40
    class ViewExtensionListener < Listener
41
      include ERB::Util
42
      include ActionView::Helpers::TagHelper
43
      include ActionView::Helpers::FormHelper
44
      include ActionView::Helpers::FormTagHelper
45
      include ActionView::Helpers::FormOptionsHelper
46
      include ActionView::Helpers::JavaScriptHelper
47
      include ActionView::Helpers::PrototypeHelper
48
      include ActionView::Helpers::NumberHelper
49
      include ActionView::Helpers::UrlHelper
50
      include ActionController::UrlWriter
51
      include ApplicationHelper
52
    end
53

    
54
    module Helper
55
      def call_hook(hook, context={})
56
        Redmine::Hook.call_hook(hook, context)
57
      end
58
    end
59
  end
60
end
61

    
62
ApplicationHelper.send(:include, Redmine::Hook::Helper)
63

    
(3-3/3)