annotate .svn/pristine/bf/bfe0563380a11f9cf16cb611af1cccdd37907a3b.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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