Chris@0: Chris@0: require 'active_record' Chris@0: Chris@0: module ActiveRecord Chris@0: class Base Chris@0: include Redmine::I18n Chris@0: Chris@0: # Translate attribute names for validation errors display Chris@0: def self.human_attribute_name(attr) Chris@0: l("field_#{attr.to_s.gsub(/_id$/, '')}") Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: module ActiveRecord Chris@0: class Errors Chris@0: def full_messages(options = {}) Chris@0: full_messages = [] Chris@0: Chris@0: @errors.each_key do |attr| Chris@0: @errors[attr].each do |message| Chris@0: next unless message Chris@0: Chris@0: if attr == "base" Chris@0: full_messages << message Chris@0: elsif attr == "custom_values" Chris@0: # Replace the generic "custom values is invalid" Chris@0: # with the errors on custom values Chris@0: @base.custom_values.each do |value| Chris@0: value.errors.each do |attr, msg| Chris@0: full_messages << value.custom_field.name + ' ' + msg Chris@0: end Chris@0: end Chris@0: else Chris@0: attr_name = @base.class.human_attribute_name(attr) Chris@0: full_messages << attr_name + ' ' + message.to_s Chris@0: end Chris@0: end Chris@0: end Chris@0: full_messages Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: module ActionView Chris@0: module Helpers Chris@0: module DateHelper Chris@0: # distance_of_time_in_words breaks when difference is greater than 30 years Chris@0: def distance_of_date_in_words(from_date, to_date = 0, options = {}) Chris@0: from_date = from_date.to_date if from_date.respond_to?(:to_date) Chris@0: to_date = to_date.to_date if to_date.respond_to?(:to_date) Chris@0: distance_in_days = (to_date - from_date).abs Chris@0: Chris@0: I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale| Chris@0: case distance_in_days Chris@0: when 0..60 then locale.t :x_days, :count => distance_in_days.round Chris@0: when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round Chris@0: else locale.t :over_x_years, :count => (distance_in_days / 365).floor Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" } Chris@0: Chris@0: # Adds :async_smtp and :async_sendmail delivery methods Chris@0: # to perform email deliveries asynchronously Chris@0: module AsynchronousMailer Chris@0: %w(smtp sendmail).each do |type| Chris@0: define_method("perform_delivery_async_#{type}") do |mail| Chris@0: Thread.start do Chris@0: send "perform_delivery_#{type}", mail Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: ActionMailer::Base.send :include, AsynchronousMailer chris@37: chris@37: # TODO: Hack to support i18n 4.x on Rails 2.3.5. Remove post 2.3.6. chris@37: # See http://www.redmine.org/issues/6428 and http://www.redmine.org/issues/5608 chris@37: module I18n chris@37: module Backend chris@37: module Base chris@37: def warn_syntax_deprecation!(*args) chris@37: return if @skip_syntax_deprecation Chris@117: warn "The {{key}} interpolation syntax in I18n messages is deprecated and will be removed in Redmine 1.2. Please use %{key} instead, see http://www.redmine.org/issues/7013 for more information." chris@37: @skip_syntax_deprecation = true chris@37: end chris@37: end chris@37: end chris@37: end Chris@117: Chris@117: module ActionController Chris@117: module MimeResponds Chris@117: class Responder Chris@117: def api(&block) Chris@117: any(:xml, :json, &block) Chris@117: end Chris@117: end Chris@117: end Chris@117: end