annotate .svn/pristine/ce/ce353587e94a2173fc11172d92c51303bcbfa3ef.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 module Redmine
Chris@1494 19 module Hook
Chris@1494 20 @@listener_classes = []
Chris@1494 21 @@listeners = nil
Chris@1494 22 @@hook_listeners = {}
Chris@1494 23
Chris@1494 24 class << self
Chris@1494 25 # Adds a listener class.
Chris@1494 26 # Automatically called when a class inherits from Redmine::Hook::Listener.
Chris@1494 27 def add_listener(klass)
Chris@1494 28 raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
Chris@1494 29 @@listener_classes << klass
Chris@1494 30 clear_listeners_instances
Chris@1494 31 end
Chris@1494 32
Chris@1494 33 # Returns all the listerners instances.
Chris@1494 34 def listeners
Chris@1494 35 @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
Chris@1494 36 end
Chris@1494 37
Chris@1494 38 # Returns the listeners instances for the given hook.
Chris@1494 39 def hook_listeners(hook)
Chris@1494 40 @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
Chris@1494 41 end
Chris@1494 42
Chris@1494 43 # Clears all the listeners.
Chris@1494 44 def clear_listeners
Chris@1494 45 @@listener_classes = []
Chris@1494 46 clear_listeners_instances
Chris@1494 47 end
Chris@1494 48
Chris@1494 49 # Clears all the listeners instances.
Chris@1494 50 def clear_listeners_instances
Chris@1494 51 @@listeners = nil
Chris@1494 52 @@hook_listeners = {}
Chris@1494 53 end
Chris@1494 54
Chris@1494 55 # Calls a hook.
Chris@1494 56 # Returns the listeners response.
Chris@1494 57 def call_hook(hook, context={})
Chris@1494 58 [].tap do |response|
Chris@1494 59 hls = hook_listeners(hook)
Chris@1494 60 if hls.any?
Chris@1494 61 hls.each {|listener| response << listener.send(hook, context)}
Chris@1494 62 end
Chris@1494 63 end
Chris@1494 64 end
Chris@1494 65 end
Chris@1494 66
Chris@1494 67 # Base class for hook listeners.
Chris@1494 68 class Listener
Chris@1494 69 include Singleton
Chris@1494 70 include Redmine::I18n
Chris@1494 71
Chris@1494 72 # Registers the listener
Chris@1494 73 def self.inherited(child)
Chris@1494 74 Redmine::Hook.add_listener(child)
Chris@1494 75 super
Chris@1494 76 end
Chris@1494 77
Chris@1494 78 end
Chris@1494 79
Chris@1494 80 # Listener class used for views hooks.
Chris@1494 81 # Listeners that inherit this class will include various helpers by default.
Chris@1494 82 class ViewListener < Listener
Chris@1494 83 include ERB::Util
Chris@1494 84 include ActionView::Helpers::TagHelper
Chris@1494 85 include ActionView::Helpers::FormHelper
Chris@1494 86 include ActionView::Helpers::FormTagHelper
Chris@1494 87 include ActionView::Helpers::FormOptionsHelper
Chris@1494 88 include ActionView::Helpers::JavaScriptHelper
Chris@1494 89 include ActionView::Helpers::NumberHelper
Chris@1494 90 include ActionView::Helpers::UrlHelper
Chris@1494 91 include ActionView::Helpers::AssetTagHelper
Chris@1494 92 include ActionView::Helpers::TextHelper
Chris@1494 93 include Rails.application.routes.url_helpers
Chris@1494 94 include ApplicationHelper
Chris@1494 95
Chris@1494 96 # Default to creating links using only the path. Subclasses can
Chris@1494 97 # change this default as needed
Chris@1494 98 def self.default_url_options
Chris@1494 99 {:only_path => true }
Chris@1494 100 end
Chris@1494 101
Chris@1494 102 # Helper method to directly render a partial using the context:
Chris@1494 103 #
Chris@1494 104 # class MyHook < Redmine::Hook::ViewListener
Chris@1494 105 # render_on :view_issues_show_details_bottom, :partial => "show_more_data"
Chris@1494 106 # end
Chris@1494 107 #
Chris@1494 108 def self.render_on(hook, options={})
Chris@1494 109 define_method hook do |context|
Chris@1494 110 if context[:hook_caller].respond_to?(:render)
Chris@1494 111 context[:hook_caller].send(:render, {:locals => context}.merge(options))
Chris@1494 112 elsif context[:controller].is_a?(ActionController::Base)
Chris@1494 113 context[:controller].send(:render_to_string, {:locals => context}.merge(options))
Chris@1494 114 else
Chris@1494 115 raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}"
Chris@1494 116 end
Chris@1494 117 end
Chris@1494 118 end
Chris@1494 119
Chris@1494 120 def controller
Chris@1494 121 nil
Chris@1494 122 end
Chris@1494 123
Chris@1494 124 def config
Chris@1494 125 ActionController::Base.config
Chris@1494 126 end
Chris@1494 127 end
Chris@1494 128
Chris@1494 129 # Helper module included in ApplicationHelper and ActionController so that
Chris@1494 130 # hooks can be called in views like this:
Chris@1494 131 #
Chris@1494 132 # <%= call_hook(:some_hook) %>
Chris@1494 133 # <%= call_hook(:another_hook, :foo => 'bar') %>
Chris@1494 134 #
Chris@1494 135 # Or in controllers like:
Chris@1494 136 # call_hook(:some_hook)
Chris@1494 137 # call_hook(:another_hook, :foo => 'bar')
Chris@1494 138 #
Chris@1494 139 # Hooks added to views will be concatenated into a string. Hooks added to
Chris@1494 140 # controllers will return an array of results.
Chris@1494 141 #
Chris@1494 142 # Several objects are automatically added to the call context:
Chris@1494 143 #
Chris@1494 144 # * project => current project
Chris@1494 145 # * request => Request instance
Chris@1494 146 # * controller => current Controller instance
Chris@1494 147 # * hook_caller => object that called the hook
Chris@1494 148 #
Chris@1494 149 module Helper
Chris@1494 150 def call_hook(hook, context={})
Chris@1494 151 if is_a?(ActionController::Base)
Chris@1494 152 default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self}
Chris@1494 153 Redmine::Hook.call_hook(hook, default_context.merge(context))
Chris@1494 154 else
Chris@1494 155 default_context = { :project => @project, :hook_caller => self }
Chris@1494 156 default_context[:controller] = controller if respond_to?(:controller)
Chris@1494 157 default_context[:request] = request if respond_to?(:request)
Chris@1494 158 Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe
Chris@1494 159 end
Chris@1494 160 end
Chris@1494 161 end
Chris@1494 162 end
Chris@1494 163 end
Chris@1494 164
Chris@1494 165 ApplicationHelper.send(:include, Redmine::Hook::Helper)
Chris@1494 166 ActionController::Base.send(:include, Redmine::Hook::Helper)