@@ -107,7 +107,13 module Redmine | |||||
107 | # |
|
107 | # | |
108 | def self.render_on(hook, options={}) |
|
108 | def self.render_on(hook, options={}) | |
109 | define_method hook do |context| |
|
109 | define_method hook do |context| | |
|
110 | if context[:hook_caller].respond_to?(:render) | |||
|
111 | context[:hook_caller].send(:render, {:locals => context}.merge(options)) | |||
|
112 | elsif context[:controller].is_a?(ActionController::Base) | |||
110 | context[:controller].send(:render_to_string, {:locals => context}.merge(options)) |
|
113 | context[:controller].send(:render_to_string, {:locals => context}.merge(options)) | |
|
114 | else | |||
|
115 | raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}" | |||
|
116 | end | |||
111 | end |
|
117 | end | |
112 | end |
|
118 | end | |
113 |
|
119 | |||
@@ -138,14 +144,15 module Redmine | |||||
138 | # * project => current project |
|
144 | # * project => current project | |
139 | # * request => Request instance |
|
145 | # * request => Request instance | |
140 | # * controller => current Controller instance |
|
146 | # * controller => current Controller instance | |
|
147 | # * hook_caller => object that called the hook | |||
141 | # |
|
148 | # | |
142 | module Helper |
|
149 | module Helper | |
143 | def call_hook(hook, context={}) |
|
150 | def call_hook(hook, context={}) | |
144 | if is_a?(ActionController::Base) |
|
151 | if is_a?(ActionController::Base) | |
145 | default_context = {:controller => self, :project => @project, :request => request} |
|
152 | default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self} | |
146 | Redmine::Hook.call_hook(hook, default_context.merge(context)) |
|
153 | Redmine::Hook.call_hook(hook, default_context.merge(context)) | |
147 | else |
|
154 | else | |
148 | default_context = { :project => @project } |
|
155 | default_context = { :project => @project, :hook_caller => self } | |
149 | default_context[:controller] = controller if respond_to?(:controller) |
|
156 | default_context[:controller] = controller if respond_to?(:controller) | |
150 | default_context[:request] = request if respond_to?(:request) |
|
157 | default_context[:request] = request if respond_to?(:request) | |
151 | Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe |
|
158 | Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe |
@@ -35,6 +35,17 class MenuManagerTest < ActionController::IntegrationTest | |||||
35 | end |
|
35 | end | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
|
38 | class ContentForInsideHook < Redmine::Hook::ViewListener | |||
|
39 | render_on :view_welcome_index_left, :inline => <<-VIEW | |||
|
40 | <% content_for :header_tags do %> | |||
|
41 | <%= javascript_include_tag 'test_plugin.js', :plugin => 'test_plugin' %> | |||
|
42 | <%= stylesheet_link_tag 'test_plugin.css', :plugin => 'test_plugin' %> | |||
|
43 | <% end %> | |||
|
44 | ||||
|
45 | <p>ContentForInsideHook content</p> | |||
|
46 | VIEW | |||
|
47 | end | |||
|
48 | ||||
38 | def setup |
|
49 | def setup | |
39 | Redmine::Hook.clear_listeners |
|
50 | Redmine::Hook.clear_listeners | |
40 | end |
|
51 | end | |
@@ -64,4 +75,16 class MenuManagerTest < ActionController::IntegrationTest | |||||
64 | assert_select 'div#main' |
|
75 | assert_select 'div#main' | |
65 | assert_select 'div#main.nosidebar', 0 |
|
76 | assert_select 'div#main.nosidebar', 0 | |
66 | end |
|
77 | end | |
|
78 | ||||
|
79 | def test_hook_with_content_for_should_append_content | |||
|
80 | Redmine::Hook.add_listener(ContentForInsideHook) | |||
|
81 | ||||
|
82 | get '/' | |||
|
83 | assert_response :success | |||
|
84 | assert_select 'p', :text => 'ContentForInsideHook content' | |||
|
85 | assert_select 'head' do | |||
|
86 | assert_select 'script[src=/plugin_assets/test_plugin/javascripts/test_plugin.js]' | |||
|
87 | assert_select 'link[href=/plugin_assets/test_plugin/stylesheets/test_plugin.css]' | |||
|
88 | end | |||
|
89 | end | |||
67 | end |
|
90 | end |
@@ -89,7 +89,7 class Redmine::Hook::ManagerTest < ActionView::TestCase | |||||
89 |
|
89 | |||
90 | def test_call_hook_with_context |
|
90 | def test_call_hook_with_context | |
91 | @hook_module.add_listener(TestHook3) |
|
91 | @hook_module.add_listener(TestHook3) | |
92 | assert_equal ['Context keys: bar, controller, foo, project, request.'], |
|
92 | assert_equal ['Context keys: bar, controller, foo, hook_caller, project, request.'], | |
93 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') |
|
93 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') | |
94 | end |
|
94 | end | |
95 |
|
95 |
General Comments 0
You need to be logged in to leave comments.
Login now