annotate config/initializers/10-patches.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 94944d00e43c
rev   line source
Chris@0 1
Chris@0 2 require 'active_record'
Chris@0 3
Chris@0 4 module ActiveRecord
Chris@0 5 class Base
Chris@0 6 include Redmine::I18n
Chris@0 7
Chris@0 8 # Translate attribute names for validation errors display
Chris@0 9 def self.human_attribute_name(attr)
Chris@0 10 l("field_#{attr.to_s.gsub(/_id$/, '')}")
Chris@0 11 end
Chris@0 12 end
Chris@0 13 end
Chris@0 14
Chris@0 15 module ActiveRecord
Chris@0 16 class Errors
Chris@0 17 def full_messages(options = {})
Chris@0 18 full_messages = []
Chris@0 19
Chris@0 20 @errors.each_key do |attr|
Chris@0 21 @errors[attr].each do |message|
Chris@0 22 next unless message
Chris@0 23
Chris@0 24 if attr == "base"
Chris@0 25 full_messages << message
Chris@0 26 elsif attr == "custom_values"
Chris@0 27 # Replace the generic "custom values is invalid"
Chris@0 28 # with the errors on custom values
Chris@0 29 @base.custom_values.each do |value|
Chris@0 30 value.errors.each do |attr, msg|
Chris@0 31 full_messages << value.custom_field.name + ' ' + msg
Chris@0 32 end
Chris@0 33 end
Chris@0 34 else
Chris@0 35 attr_name = @base.class.human_attribute_name(attr)
Chris@0 36 full_messages << attr_name + ' ' + message.to_s
Chris@0 37 end
Chris@0 38 end
Chris@0 39 end
Chris@0 40 full_messages
Chris@0 41 end
Chris@0 42 end
Chris@0 43 end
Chris@0 44
Chris@0 45 module ActionView
Chris@0 46 module Helpers
Chris@0 47 module DateHelper
Chris@0 48 # distance_of_time_in_words breaks when difference is greater than 30 years
Chris@0 49 def distance_of_date_in_words(from_date, to_date = 0, options = {})
Chris@0 50 from_date = from_date.to_date if from_date.respond_to?(:to_date)
Chris@0 51 to_date = to_date.to_date if to_date.respond_to?(:to_date)
Chris@0 52 distance_in_days = (to_date - from_date).abs
Chris@0 53
Chris@0 54 I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
Chris@0 55 case distance_in_days
Chris@0 56 when 0..60 then locale.t :x_days, :count => distance_in_days.round
Chris@0 57 when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
Chris@0 58 else locale.t :over_x_years, :count => (distance_in_days / 365).floor
Chris@0 59 end
Chris@0 60 end
Chris@0 61 end
Chris@0 62 end
Chris@0 63 end
Chris@0 64 end
Chris@0 65
Chris@0 66 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
Chris@0 67
Chris@0 68 # Adds :async_smtp and :async_sendmail delivery methods
Chris@0 69 # to perform email deliveries asynchronously
Chris@0 70 module AsynchronousMailer
Chris@0 71 %w(smtp sendmail).each do |type|
Chris@0 72 define_method("perform_delivery_async_#{type}") do |mail|
Chris@0 73 Thread.start do
Chris@0 74 send "perform_delivery_#{type}", mail
Chris@0 75 end
Chris@0 76 end
Chris@0 77 end
Chris@0 78 end
Chris@0 79
Chris@0 80 ActionMailer::Base.send :include, AsynchronousMailer