Project

General

Profile

Feature #2542 » hook.rb.revised.diff

Revised patch - Thomas Löber, 2009-02-02 11:58

View differences:

lib/redmine/hook.rb (Arbeitskopie)
17 17

  
18 18
module Redmine
19 19
  module Hook
20
    include ActionController::UrlWriter
21

  
20 22
    @@listener_classes = []
21 23
    @@listeners = nil
22 24
    @@hook_listeners = {}
......
55 57
      # Calls a hook.
56 58
      # Returns the listeners response.
57 59
      def call_hook(hook, context={})
58
        response = ''
59
        hook_listeners(hook).each do |listener|
60
          response << listener.send(hook, context).to_s
60
        returning [] do |response|
61
          hls = hook_listeners(hook)
62
          if hls.any?
63
            request = context[:request]
64
            if request
65
              default_url_options[:host] ||= request.env["SERVER_NAME"]
66
              default_url_options[:port] ||= request.env["SERVER_PORT"]
67
            end
68
            hls.each {|listener| response << listener.send(hook, context)}
69
          end
61 70
        end
62
        response
63 71
      end
64 72
    end
65 73

  
......
91 99
      include ActionView::Helpers::TextHelper
92 100
      include ActionController::UrlWriter
93 101
      include ApplicationHelper
102

  
103
      def self.render_on(hook, options={})
104
        define_method hook do |context|
105
          context[:controller].send(:render_to_string, {:locals => context}.merge(options))
106
        end
107
      end
94 108
    end
95 109

  
96 110
    # Helper module included in ApplicationHelper so that hooks can be called
......
101 115
    # Current project is automatically added to the call context.
102 116
    module Helper
103 117
      def call_hook(hook, context={})
104
        Redmine::Hook.call_hook(hook, {:project => @project}.merge(context))
118
        if is_a?(ActionController::Base)
119
          ctx = {:controller => self, :project => @project, :request => request}
120
          Redmine::Hook.call_hook(hook, ctx.merge(context))
121
        else
122
          ctx = {:controller => controller, :project => @project, :request => request}
123
          Redmine::Hook.call_hook(hook, ctx.merge(context)).join
124
        end        
105 125
      end
106 126
    end
107 127
  end
test/unit/lib/redmine/hook_test.rb (Arbeitskopie)
67 67
  
68 68
  def test_call_hook
69 69
    @hook_module.add_listener(TestHook1)
70
    assert_equal 'Test hook 1 listener.', @hook_module.call_hook(:view_layouts_base_html_head)
70
    assert_equal ['Test hook 1 listener.'], @hook_module.call_hook(:view_layouts_base_html_head)
71 71
  end
72 72
  
73 73
  def test_call_hook_with_context
74 74
    @hook_module.add_listener(TestHook3)
75
    assert_equal 'Context keys: bar, foo.', @hook_module.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
75
    assert_equal ['Context keys: bar, foo.'], @hook_module.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
76 76
  end
77 77
  
78 78
  def test_call_hook_with_multiple_listeners
79 79
    @hook_module.add_listener(TestHook1)
80 80
    @hook_module.add_listener(TestHook2)
81
    assert_equal 'Test hook 1 listener.Test hook 2 listener.', @hook_module.call_hook(:view_layouts_base_html_head)
81
    assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], @hook_module.call_hook(:view_layouts_base_html_head)
82 82
  end
83 83
end
(2-2/3)