diff config/initializers/10-patches.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children 261b3d9a4903
line wrap: on
line diff
--- a/config/initializers/10-patches.rb	Wed Jun 27 14:54:18 2012 +0100
+++ b/config/initializers/10-patches.rb	Mon Jan 07 12:01:42 2013 +0000
@@ -1,45 +1,24 @@
-
 require 'active_record'
 
 module ActiveRecord
   class Base
     include Redmine::I18n
+    # Translate attribute names for validation errors display
+    def self.human_attribute_name(attr, *args)
+      attr = attr.to_s.sub(/_id$/, '')
 
-    # Translate attribute names for validation errors display
-    def self.human_attribute_name(attr)
-      l("field_#{attr.to_s.gsub(/_id$/, '')}", :default => attr)
+      l("field_#{name.underscore.gsub('/', '_')}_#{attr}", :default => ["field_#{attr}".to_sym, attr])
     end
   end
-end
 
-module ActiveRecord
-  class Errors
-    def full_messages(options = {})
-      full_messages = []
-
-      @errors.each_key do |attr|
-        @errors[attr].each do |message|
-          next unless message
-
-          if attr == "base"
-            full_messages << message
-          elsif attr == "custom_values"
-            # Replace the generic "custom values is invalid"
-            # with the errors on custom values
-            @base.custom_values.each do |value|
-              value.errors.each do |attr, msg|
-                full_messages << value.custom_field.name + ' ' + msg
-              end
-            end
-          else
-            attr_name = @base.class.human_attribute_name(attr)
-            full_messages << attr_name + ' ' + message.to_s
-          end
-        end
-      end
-      full_messages
+  # Undefines private Kernel#open method to allow using `open` scopes in models.
+  # See Defect #11545 (http://www.redmine.org/issues/11545) for details.
+  class Base
+    class << self
+      undef open
     end
   end
+  class Relation ; undef open ; end
 end
 
 module ActionView
@@ -61,54 +40,128 @@
       end
     end
   end
+
+  class Resolver
+    def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
+      cached(key, [name, prefix, partial], details, locals) do
+        if details[:formats] & [:xml, :json]
+          details = details.dup
+          details[:formats] = details[:formats].dup + [:api]
+        end
+        find_templates(name, prefix, partial, details)
+      end
+    end
+  end
 end
 
-ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
+# Do not HTML escape text templates
+module ActionView
+  class Template
+    module Handlers
+      class ERB
+        def call(template)
+          if template.source.encoding_aware?
+            # First, convert to BINARY, so in case the encoding is
+            # wrong, we can still find an encoding tag
+            # (<%# encoding %>) inside the String using a regular
+            # expression
+            template_source = template.source.dup.force_encoding("BINARY")
 
-module AsynchronousMailer
-  # Adds :async_smtp and :async_sendmail delivery methods
-  # to perform email deliveries asynchronously
-  %w(smtp sendmail).each do |type|
-    define_method("perform_delivery_async_#{type}") do |mail|
+            erb = template_source.gsub(ENCODING_TAG, '')
+            encoding = $2
+
+            erb.force_encoding valid_encoding(template.source.dup, encoding)
+
+            # Always make sure we return a String in the default_internal
+            erb.encode!
+          else
+            erb = template.source.dup
+          end
+
+          self.class.erb_implementation.new(
+            erb,
+            :trim => (self.class.erb_trim_mode == "-"),
+            :escape => template.identifier =~ /\.text/ # only escape HTML templates
+          ).src
+        end
+      end
+    end
+  end
+end
+
+ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
+
+require 'mail'
+
+module DeliveryMethods
+  class AsyncSMTP < ::Mail::SMTP
+    def deliver!(*args)
       Thread.start do
-        send "perform_delivery_#{type}", mail
+        super *args
       end
     end
   end
 
-  # Adds a delivery method that writes emails in tmp/emails for testing purpose
-  def perform_delivery_tmp_file(mail)
-    dest_dir = File.join(Rails.root, 'tmp', 'emails')
-    Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
-    File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
+  class AsyncSendmail < ::Mail::Sendmail
+    def deliver!(*args)
+      Thread.start do
+        super *args
+      end
+    end
+  end
+
+  class TmpFile
+    def initialize(*args); end
+
+    def deliver!(mail)
+      dest_dir = File.join(Rails.root, 'tmp', 'emails')
+      Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
+      File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
+    end
   end
 end
 
-ActionMailer::Base.send :include, AsynchronousMailer
+ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP
+ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail
+ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
 
-module TMail
-  # TMail::Unquoter.convert_to_with_fallback_on_iso_8859_1 introduced in TMail 1.2.7
-  # triggers a test failure in test_add_issue_with_japanese_keywords(MailHandlerTest)
-  class Unquoter
-    class << self
-      alias_method :convert_to, :convert_to_without_fallback_on_iso_8859_1
-    end
-  end
-
-  # Patch for TMail 1.2.7. See http://www.redmine.org/issues/8751
-  class Encoder
-    def puts_meta(str)
-      add_text str
+# Changes how sent emails are logged
+# Rails doesn't log cc and bcc which is misleading when using bcc only (#12090)
+module ActionMailer
+  class LogSubscriber < ActiveSupport::LogSubscriber
+    def deliver(event)
+      recipients = [:to, :cc, :bcc].inject("") do |s, header|
+        r = Array.wrap(event.payload[header])
+        if r.any?
+          s << "\n  #{header}: #{r.join(', ')}"
+        end
+        s
+      end
+      info("\nSent email \"#{event.payload[:subject]}\" (%1.fms)#{recipients}" % event.duration)
+      debug(event.payload[:mail])
     end
   end
 end
 
 module ActionController
   module MimeResponds
-    class Responder
+    class Collector
       def api(&block)
         any(:xml, :json, &block)
       end
     end
   end
 end
+
+module ActionController
+  class Base
+    # Displays an explicit message instead of a NoMethodError exception
+    # when trying to start Redmine with an old session_store.rb
+    # TODO: remove it in a later version
+    def self.session=(*args)
+      $stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" +
+        "Setting the session secret with ActionController.session= is no longer supported in Rails 3."
+      exit 1
+    end
+  end
+end