mirror of
https://github.com/meineerde/redmine.git
synced 2025-12-19 15:01:14 +00:00
Ability to render multiple partials with view hook (#17763).
git-svn-id: http://svn.redmine.org/redmine/trunk@13449 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
2ab1a9dccd
commit
ff9be52e45
@ -99,20 +99,29 @@ module Redmine
|
|||||||
{:only_path => true }
|
{:only_path => true }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Helper method to directly render a partial using the context:
|
# Helper method to directly render using the context,
|
||||||
|
# render_options must be valid #render options.
|
||||||
#
|
#
|
||||||
# class MyHook < Redmine::Hook::ViewListener
|
# class MyHook < Redmine::Hook::ViewListener
|
||||||
# render_on :view_issues_show_details_bottom, :partial => "show_more_data"
|
# render_on :view_issues_show_details_bottom, :partial => "show_more_data"
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def self.render_on(hook, options={})
|
# class MultipleHook < Redmine::Hook::ViewListener
|
||||||
|
# render_on :view_issues_show_details_bottom,
|
||||||
|
# {:partial => "show_more_data"},
|
||||||
|
# {:partial => "show_even_more_data"}
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
def self.render_on(hook, *render_options)
|
||||||
define_method hook do |context|
|
define_method hook do |context|
|
||||||
if context[:hook_caller].respond_to?(:render)
|
render_options.map do |options|
|
||||||
context[:hook_caller].send(:render, {:locals => context}.merge(options))
|
if context[:hook_caller].respond_to?(:render)
|
||||||
elsif context[:controller].is_a?(ActionController::Base)
|
context[:hook_caller].send(:render, {:locals => context}.merge(options))
|
||||||
context[:controller].send(:render_to_string, {:locals => context}.merge(options))
|
elsif context[:controller].is_a?(ActionController::Base)
|
||||||
else
|
context[:controller].send(:render_to_string, {:locals => context}.merge(options))
|
||||||
raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}"
|
else
|
||||||
|
raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -45,6 +45,14 @@ class HookTest < ActionController::IntegrationTest
|
|||||||
VIEW
|
VIEW
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SingleRenderOn < Redmine::Hook::ViewListener
|
||||||
|
render_on :view_welcome_index_left, :inline => 'SingleRenderOn 1'
|
||||||
|
end
|
||||||
|
|
||||||
|
class MultipleRenderOn < Redmine::Hook::ViewListener
|
||||||
|
render_on :view_welcome_index_left, {:inline => 'MultipleRenderOn 1'}, {:inline => 'MultipleRenderOn 2'}
|
||||||
|
end
|
||||||
|
|
||||||
# Hooks that stores the call context
|
# Hooks that stores the call context
|
||||||
class ContextTestHook < Redmine::Hook::ViewListener
|
class ContextTestHook < Redmine::Hook::ViewListener
|
||||||
cattr_accessor :context
|
cattr_accessor :context
|
||||||
@ -105,4 +113,11 @@ VIEW
|
|||||||
assert_kind_of Hash, context[:request].params
|
assert_kind_of Hash, context[:request].params
|
||||||
assert_kind_of AccountController, context[:hook_caller]
|
assert_kind_of AccountController, context[:hook_caller]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_multiple_hooks
|
||||||
|
Redmine::Hook.add_listener(SingleRenderOn)
|
||||||
|
Redmine::Hook.add_listener(MultipleRenderOn)
|
||||||
|
get '/'
|
||||||
|
assert_equal 1, response.body.scan("SingleRenderOn 1 MultipleRenderOn 1 MultipleRenderOn 2").size
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user