annotate config/initializers/10-patches.rb @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents 261b3d9a4903
children dffacf8a6908
rev   line source
Chris@0 1 require 'active_record'
Chris@0 2
Chris@0 3 module ActiveRecord
Chris@0 4 class Base
Chris@0 5 include Redmine::I18n
Chris@1115 6 # Translate attribute names for validation errors display
Chris@1115 7 def self.human_attribute_name(attr, *args)
Chris@1115 8 attr = attr.to_s.sub(/_id$/, '')
Chris@909 9
Chris@1115 10 l("field_#{name.underscore.gsub('/', '_')}_#{attr}", :default => ["field_#{attr}".to_sym, attr])
Chris@0 11 end
Chris@0 12 end
Chris@0 13
Chris@1115 14 # Undefines private Kernel#open method to allow using `open` scopes in models.
Chris@1115 15 # See Defect #11545 (http://www.redmine.org/issues/11545) for details.
Chris@1115 16 class Base
Chris@1115 17 class << self
Chris@1115 18 undef open
Chris@0 19 end
Chris@0 20 end
Chris@1115 21 class Relation ; undef open ; end
Chris@0 22 end
Chris@0 23
Chris@0 24 module ActionView
Chris@0 25 module Helpers
Chris@0 26 module DateHelper
Chris@0 27 # distance_of_time_in_words breaks when difference is greater than 30 years
Chris@0 28 def distance_of_date_in_words(from_date, to_date = 0, options = {})
Chris@0 29 from_date = from_date.to_date if from_date.respond_to?(:to_date)
Chris@0 30 to_date = to_date.to_date if to_date.respond_to?(:to_date)
Chris@0 31 distance_in_days = (to_date - from_date).abs
Chris@0 32
Chris@0 33 I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
Chris@0 34 case distance_in_days
Chris@0 35 when 0..60 then locale.t :x_days, :count => distance_in_days.round
Chris@0 36 when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
Chris@0 37 else locale.t :over_x_years, :count => (distance_in_days / 365).floor
Chris@0 38 end
Chris@0 39 end
Chris@0 40 end
Chris@0 41 end
Chris@0 42 end
Chris@1115 43
Chris@1115 44 class Resolver
Chris@1115 45 def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
Chris@1115 46 cached(key, [name, prefix, partial], details, locals) do
Chris@1115 47 if details[:formats] & [:xml, :json]
Chris@1115 48 details = details.dup
Chris@1115 49 details[:formats] = details[:formats].dup + [:api]
Chris@1115 50 end
Chris@1115 51 find_templates(name, prefix, partial, details)
Chris@1115 52 end
Chris@1115 53 end
Chris@1115 54 end
Chris@0 55 end
Chris@0 56
Chris@1115 57 # Do not HTML escape text templates
Chris@1115 58 module ActionView
Chris@1115 59 class Template
Chris@1115 60 module Handlers
Chris@1115 61 class ERB
Chris@1115 62 def call(template)
Chris@1115 63 if template.source.encoding_aware?
Chris@1115 64 # First, convert to BINARY, so in case the encoding is
Chris@1115 65 # wrong, we can still find an encoding tag
Chris@1115 66 # (<%# encoding %>) inside the String using a regular
Chris@1115 67 # expression
Chris@1115 68 template_source = template.source.dup.force_encoding("BINARY")
Chris@0 69
Chris@1115 70 erb = template_source.gsub(ENCODING_TAG, '')
Chris@1115 71 encoding = $2
Chris@1115 72
Chris@1115 73 erb.force_encoding valid_encoding(template.source.dup, encoding)
Chris@1115 74
Chris@1115 75 # Always make sure we return a String in the default_internal
Chris@1115 76 erb.encode!
Chris@1115 77 else
Chris@1115 78 erb = template.source.dup
Chris@1115 79 end
Chris@1115 80
Chris@1115 81 self.class.erb_implementation.new(
Chris@1115 82 erb,
Chris@1115 83 :trim => (self.class.erb_trim_mode == "-"),
Chris@1115 84 :escape => template.identifier =~ /\.text/ # only escape HTML templates
Chris@1115 85 ).src
Chris@1115 86 end
Chris@1115 87 end
Chris@1115 88 end
Chris@1115 89 end
Chris@1115 90 end
Chris@1115 91
Chris@1115 92 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
Chris@1115 93
Chris@1464 94 # HTML5: <option value=""></option> is invalid, use <option value="">&nbsp;</option> instead
Chris@1464 95 module ActionView
Chris@1464 96 module Helpers
Chris@1464 97 class InstanceTag
Chris@1464 98 private
Chris@1464 99 def add_options_with_non_empty_blank_option(option_tags, options, value = nil)
Chris@1464 100 if options[:include_blank] == true
Chris@1464 101 options = options.dup
Chris@1464 102 options[:include_blank] = '&nbsp;'.html_safe
Chris@1464 103 end
Chris@1464 104 add_options_without_non_empty_blank_option(option_tags, options, value)
Chris@1464 105 end
Chris@1464 106 alias_method_chain :add_options, :non_empty_blank_option
Chris@1464 107 end
Chris@1464 108
Chris@1464 109 module FormTagHelper
Chris@1464 110 def select_tag_with_non_empty_blank_option(name, option_tags = nil, options = {})
Chris@1464 111 if options.delete(:include_blank)
Chris@1464 112 options[:prompt] = '&nbsp;'.html_safe
Chris@1464 113 end
Chris@1464 114 select_tag_without_non_empty_blank_option(name, option_tags, options)
Chris@1464 115 end
Chris@1464 116 alias_method_chain :select_tag, :non_empty_blank_option
Chris@1464 117 end
Chris@1464 118
Chris@1464 119 module FormOptionsHelper
Chris@1464 120 def options_for_select_with_non_empty_blank_option(container, selected = nil)
Chris@1464 121 if container.is_a?(Array)
Chris@1464 122 container = container.map {|element| element.blank? ? ["&nbsp;".html_safe, ""] : element}
Chris@1464 123 end
Chris@1464 124 options_for_select_without_non_empty_blank_option(container, selected)
Chris@1464 125 end
Chris@1464 126 alias_method_chain :options_for_select, :non_empty_blank_option
Chris@1464 127 end
Chris@1464 128 end
Chris@1464 129 end
Chris@1464 130
Chris@1115 131 require 'mail'
Chris@1115 132
Chris@1115 133 module DeliveryMethods
Chris@1115 134 class AsyncSMTP < ::Mail::SMTP
Chris@1115 135 def deliver!(*args)
Chris@0 136 Thread.start do
Chris@1115 137 super *args
Chris@909 138 end
Chris@0 139 end
Chris@0 140 end
Chris@909 141
Chris@1115 142 class AsyncSendmail < ::Mail::Sendmail
Chris@1115 143 def deliver!(*args)
Chris@1115 144 Thread.start do
Chris@1115 145 super *args
Chris@1115 146 end
Chris@1115 147 end
Chris@1115 148 end
Chris@1115 149
Chris@1115 150 class TmpFile
Chris@1115 151 def initialize(*args); end
Chris@1115 152
Chris@1115 153 def deliver!(mail)
Chris@1115 154 dest_dir = File.join(Rails.root, 'tmp', 'emails')
Chris@1115 155 Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
Chris@1115 156 File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
Chris@1115 157 end
Chris@909 158 end
Chris@0 159 end
Chris@0 160
Chris@1115 161 ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP
Chris@1115 162 ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail
Chris@1115 163 ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
chris@37 164
Chris@1115 165 # Changes how sent emails are logged
Chris@1115 166 # Rails doesn't log cc and bcc which is misleading when using bcc only (#12090)
Chris@1115 167 module ActionMailer
Chris@1115 168 class LogSubscriber < ActiveSupport::LogSubscriber
Chris@1115 169 def deliver(event)
Chris@1115 170 recipients = [:to, :cc, :bcc].inject("") do |s, header|
Chris@1115 171 r = Array.wrap(event.payload[header])
Chris@1115 172 if r.any?
Chris@1115 173 s << "\n #{header}: #{r.join(', ')}"
Chris@1115 174 end
Chris@1115 175 s
Chris@1115 176 end
Chris@1115 177 info("\nSent email \"#{event.payload[:subject]}\" (%1.fms)#{recipients}" % event.duration)
Chris@1115 178 debug(event.payload[:mail])
Chris@909 179 end
Chris@909 180 end
chris@37 181 end
Chris@119 182
Chris@119 183 module ActionController
Chris@119 184 module MimeResponds
Chris@1115 185 class Collector
Chris@119 186 def api(&block)
Chris@119 187 any(:xml, :json, &block)
Chris@119 188 end
Chris@119 189 end
Chris@119 190 end
Chris@119 191 end
Chris@1115 192
Chris@1115 193 module ActionController
Chris@1115 194 class Base
Chris@1115 195 # Displays an explicit message instead of a NoMethodError exception
Chris@1115 196 # when trying to start Redmine with an old session_store.rb
Chris@1115 197 # TODO: remove it in a later version
Chris@1115 198 def self.session=(*args)
Chris@1115 199 $stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" +
Chris@1115 200 "Setting the session secret with ActionController.session= is no longer supported in Rails 3."
Chris@1115 201 exit 1
Chris@1115 202 end
Chris@1115 203 end
Chris@1115 204 end