You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10-patches.rb 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # frozen_string_literal: true
  2. module ActiveRecord
  3. class Base
  4. # Translate attribute names for validation errors display
  5. def self.human_attribute_name(attr, options = {})
  6. prepared_attr = attr.to_s.sub(/_id$/, '').sub(/^.+\./, '')
  7. class_prefix = name.underscore.tr('/', '_')
  8. redmine_default = [
  9. :"field_#{class_prefix}_#{prepared_attr}",
  10. :"field_#{prepared_attr}"
  11. ]
  12. options[:default] = redmine_default + Array(options[:default])
  13. super
  14. end
  15. end
  16. # Undefines private Kernel#open method to allow using `open` scopes in models.
  17. # See Defect #11545 (http://www.redmine.org/issues/11545) for details.
  18. class Base
  19. class << self
  20. undef open
  21. end
  22. end
  23. class Relation ; undef open ; end
  24. end
  25. module ActionView
  26. module Helpers
  27. module DateHelper
  28. # distance_of_time_in_words breaks when difference is greater than 30 years
  29. def distance_of_date_in_words(from_date, to_date = 0, options = {})
  30. from_date = from_date.to_date if from_date.respond_to?(:to_date)
  31. to_date = to_date.to_date if to_date.respond_to?(:to_date)
  32. distance_in_days = (to_date - from_date).abs
  33. I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
  34. case distance_in_days
  35. when 0..60 then locale.t :x_days, :count => distance_in_days.round
  36. when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
  37. else locale.t :over_x_years, :count => (distance_in_days / 365).floor
  38. end
  39. end
  40. end
  41. end
  42. end
  43. end
  44. ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
  45. # HTML5: <option value=""></option> is invalid, use <option value="">&nbsp;</option> instead
  46. module ActionView
  47. module Helpers
  48. module Tags
  49. SelectRenderer.prepend(Module.new do
  50. def add_options(option_tags, options, value = nil)
  51. if options.delete(:include_blank)
  52. options[:prompt] = '&nbsp;'.html_safe
  53. end
  54. super
  55. end
  56. end)
  57. end
  58. module FormHelper
  59. alias :date_field_without_max :date_field
  60. def date_field(object_name, method, options = {})
  61. date_field_without_max(object_name, method, options.reverse_merge(max: '9999-12-31'))
  62. end
  63. end
  64. module FormTagHelper
  65. alias :select_tag_without_non_empty_blank_option :select_tag
  66. def select_tag(name, option_tags = nil, options = {})
  67. if options.delete(:include_blank)
  68. options[:prompt] = '&nbsp;'.html_safe
  69. end
  70. select_tag_without_non_empty_blank_option(name, option_tags, options)
  71. end
  72. alias :date_field_tag_without_max :date_field_tag
  73. def date_field_tag(name, value = nil, options = {})
  74. date_field_tag_without_max(name, value, options.reverse_merge(max: '9999-12-31'))
  75. end
  76. end
  77. module FormOptionsHelper
  78. alias :options_for_select_without_non_empty_blank_option :options_for_select
  79. def options_for_select(container, selected = nil)
  80. if container.is_a?(Array)
  81. container = container.map {|element| element.presence || ["&nbsp;".html_safe, ""]}
  82. end
  83. options_for_select_without_non_empty_blank_option(container, selected)
  84. end
  85. end
  86. end
  87. end
  88. require 'mail'
  89. module DeliveryMethods
  90. class TmpFile
  91. def initialize(*args); end
  92. def deliver!(mail)
  93. dest_dir = File.join(Rails.root, 'tmp', 'emails')
  94. Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
  95. filename = "#{Time.now.to_i}_#{mail.message_id.gsub(/[<>]/, '')}.eml"
  96. File.binwrite(File.join(dest_dir, filename), mail.encoded)
  97. end
  98. end
  99. end
  100. ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
  101. module ActionController
  102. module MimeResponds
  103. class Collector
  104. def api(&block)
  105. any(:xml, :json, &block)
  106. end
  107. end
  108. end
  109. end
  110. module ActionController
  111. class Base
  112. # Displays an explicit message instead of a NoMethodError exception
  113. # when trying to start Redmine with an old session_store.rb
  114. # TODO: remove it in a later version
  115. def self.session=(*args)
  116. $stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" +
  117. "Setting the session secret with ActionController.session= is no longer supported."
  118. exit 1
  119. end
  120. end
  121. end
  122. module ActionView
  123. LookupContext.prepend(Module.new do
  124. def formats=(values)
  125. if (Array(values) & [:xml, :json]).any?
  126. values << :api
  127. end
  128. super values
  129. end
  130. end)
  131. Rendering.prepend(Module.new do
  132. def rendered_format
  133. if lookup_context.formats.first == :api
  134. return request.format
  135. end
  136. super
  137. end
  138. end)
  139. end
  140. Mime::SET << 'api'
  141. # Adds asset_id parameters to assets like Rails 3 to invalidate caches in browser
  142. module ActionView
  143. module Helpers
  144. module AssetUrlHelper
  145. @@cache_asset_timestamps = Rails.env.production?
  146. @@asset_timestamps_cache = {}
  147. @@asset_timestamps_cache_guard = Mutex.new
  148. def asset_path_with_asset_id(source, options = {})
  149. asset_id = rails_asset_id(source, options)
  150. unless asset_id.blank?
  151. source += "?#{asset_id}"
  152. end
  153. asset_path(source, options.merge(skip_pipeline: true))
  154. end
  155. alias :path_to_asset :asset_path_with_asset_id
  156. def rails_asset_id(source, options = {})
  157. if asset_id = ENV["RAILS_ASSET_ID"]
  158. asset_id
  159. else
  160. if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source])
  161. asset_id
  162. else
  163. extname = compute_asset_extname(source, options)
  164. path = File.join(Rails.public_path, "#{source}#{extname}")
  165. exist = false
  166. if File.exist? path
  167. exist = true
  168. else
  169. path = File.join(Rails.public_path, public_compute_asset_path("#{source}#{extname}", options))
  170. if File.exist? path
  171. exist = true
  172. end
  173. end
  174. asset_id = exist ? File.mtime(path).to_i.to_s : ''
  175. if @@cache_asset_timestamps
  176. @@asset_timestamps_cache_guard.synchronize do
  177. @@asset_timestamps_cache[source] = asset_id
  178. end
  179. end
  180. asset_id
  181. end
  182. end
  183. end
  184. end
  185. end
  186. end
  187. # https://github.com/rack/rack/pull/1703
  188. # TODO: remove this when Rack is updated to 3.0.0
  189. require 'rack'
  190. module Rack
  191. class RewindableInput
  192. unless method_defined?(:size)
  193. def size
  194. make_rewindable unless @rewindable_io
  195. @rewindable_io.size
  196. end
  197. end
  198. end
  199. end