PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/config/initializers/10-patches.rb

https://bitbucket.org/prdixit/redmine
Ruby | 149 lines | 116 code | 20 blank | 13 comment | 7 complexity | 5b3ac56e293e4f2106a9ad58090c8a12 MD5 | raw file
Possible License(s): GPL-2.0
  1. require 'active_record'
  2. module ActiveRecord
  3. class Base
  4. include Redmine::I18n
  5. # Translate attribute names for validation errors display
  6. def self.human_attribute_name(attr, *args)
  7. attr = attr.to_s.sub(/_id$/, '')
  8. l("field_#{name.underscore.gsub('/', '_')}_#{attr}", :default => ["field_#{attr}".to_sym, attr])
  9. end
  10. end
  11. # Undefines private Kernel#open method to allow using `open` scopes in models.
  12. # See Defect #11545 (http://www.redmine.org/issues/11545) for details.
  13. class Base
  14. class << self
  15. undef open
  16. end
  17. end
  18. class Relation ; undef open ; end
  19. end
  20. module ActionView
  21. module Helpers
  22. module DateHelper
  23. # distance_of_time_in_words breaks when difference is greater than 30 years
  24. def distance_of_date_in_words(from_date, to_date = 0, options = {})
  25. from_date = from_date.to_date if from_date.respond_to?(:to_date)
  26. to_date = to_date.to_date if to_date.respond_to?(:to_date)
  27. distance_in_days = (to_date - from_date).abs
  28. I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
  29. case distance_in_days
  30. when 0..60 then locale.t :x_days, :count => distance_in_days.round
  31. when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
  32. else locale.t :over_x_years, :count => (distance_in_days / 365).floor
  33. end
  34. end
  35. end
  36. end
  37. end
  38. class Resolver
  39. def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
  40. cached(key, [name, prefix, partial], details, locals) do
  41. if details[:formats] & [:xml, :json]
  42. details = details.dup
  43. details[:formats] = details[:formats].dup + [:api]
  44. end
  45. find_templates(name, prefix, partial, details)
  46. end
  47. end
  48. end
  49. end
  50. # Do not HTML escape text templates
  51. module ActionView
  52. class Template
  53. module Handlers
  54. class ERB
  55. def call(template)
  56. if template.source.encoding_aware?
  57. # First, convert to BINARY, so in case the encoding is
  58. # wrong, we can still find an encoding tag
  59. # (<%# encoding %>) inside the String using a regular
  60. # expression
  61. template_source = template.source.dup.force_encoding("BINARY")
  62. erb = template_source.gsub(ENCODING_TAG, '')
  63. encoding = $2
  64. erb.force_encoding valid_encoding(template.source.dup, encoding)
  65. # Always make sure we return a String in the default_internal
  66. erb.encode!
  67. else
  68. erb = template.source.dup
  69. end
  70. self.class.erb_implementation.new(
  71. erb,
  72. :trim => (self.class.erb_trim_mode == "-"),
  73. :escape => template.identifier =~ /\.text/ # only escape HTML templates
  74. ).src
  75. end
  76. end
  77. end
  78. end
  79. end
  80. ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
  81. require 'mail'
  82. module DeliveryMethods
  83. class AsyncSMTP < ::Mail::SMTP
  84. def deliver!(*args)
  85. Thread.start do
  86. super *args
  87. end
  88. end
  89. end
  90. class AsyncSendmail < ::Mail::Sendmail
  91. def deliver!(*args)
  92. Thread.start do
  93. super *args
  94. end
  95. end
  96. end
  97. class TmpFile
  98. def initialize(*args); end
  99. def deliver!(mail)
  100. dest_dir = File.join(Rails.root, 'tmp', 'emails')
  101. Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
  102. File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
  103. end
  104. end
  105. end
  106. ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP
  107. ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail
  108. ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
  109. module ActionController
  110. module MimeResponds
  111. class Collector
  112. def api(&block)
  113. any(:xml, :json, &block)
  114. end
  115. end
  116. end
  117. end
  118. module ActionController
  119. class Base
  120. # Displays an explicit message instead of a NoMethodError exception
  121. # when trying to start Redmine with an old session_store.rb
  122. # TODO: remove it in a later version
  123. def self.session=(*args)
  124. $stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" +
  125. "Setting the session secret with ActionController.session= is no longer supported in Rails 3."
  126. exit 1
  127. end
  128. end
  129. end