##// END OF EJS Templates
Port async delivery methods to Rails 3....
Jean-Philippe Lang -
r9350:26868d8b1404
parent child
Show More
@@ -1,81 +1,94
1 1 require 'active_record'
2 2
3 3 module ActiveRecord
4 4 class Base
5 5 include Redmine::I18n
6 6 def self.named_scope(*args)
7 7 scope(*args)
8 8 end
9 9
10 10 # Translate attribute names for validation errors display
11 11 def self.human_attribute_name(attr, *args)
12 12 l("field_#{attr.to_s.gsub(/_id$/, '')}", :default => attr)
13 13 end
14 14 end
15 15 end
16 16
17 17 module ActionView
18 18 module Helpers
19 19 module DateHelper
20 20 # distance_of_time_in_words breaks when difference is greater than 30 years
21 21 def distance_of_date_in_words(from_date, to_date = 0, options = {})
22 22 from_date = from_date.to_date if from_date.respond_to?(:to_date)
23 23 to_date = to_date.to_date if to_date.respond_to?(:to_date)
24 24 distance_in_days = (to_date - from_date).abs
25 25
26 26 I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
27 27 case distance_in_days
28 28 when 0..60 then locale.t :x_days, :count => distance_in_days.round
29 29 when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
30 30 else locale.t :over_x_years, :count => (distance_in_days / 365).floor
31 31 end
32 32 end
33 33 end
34 34 end
35 35 end
36 36
37 37 class Resolver
38 38 def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
39 39 cached(key, [name, prefix, partial], details, locals) do
40 40 if details[:formats] & [:xml, :json]
41 41 details = details.dup
42 42 details[:formats] = details[:formats].dup + [:api]
43 43 end
44 44 find_templates(name, prefix, partial, details)
45 45 end
46 46 end
47 47 end
48 48 end
49 49
50 50 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
51 51
52 module AsynchronousMailer
53 # Adds :async_smtp and :async_sendmail delivery methods
54 # to perform email deliveries asynchronously
55 %w(smtp sendmail).each do |type|
56 define_method("perform_delivery_async_#{type}") do |mail|
52 require 'mail'
53
54 module DeliveryMethods
55 class AsyncSMTP < ::Mail::SMTP
56 def deliver!(*args)
57 Thread.start do
58 super *args
59 end
60 end
61 end
62
63 class AsyncSendmail < ::Mail::Sendmail
64 def deliver!(*args)
57 65 Thread.start do
58 send "perform_delivery_#{type}", mail
66 super *args
59 67 end
60 68 end
61 69 end
62 70
63 # Adds a delivery method that writes emails in tmp/emails for testing purpose
64 def perform_delivery_tmp_file(mail)
65 dest_dir = File.join(Rails.root, 'tmp', 'emails')
66 Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
67 File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
71 class TmpFile
72 def initialize(*args); end
73
74 def deliver!(mail)
75 dest_dir = File.join(Rails.root, 'tmp', 'emails')
76 Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
77 File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
78 end
68 79 end
69 80 end
70 81
71 ActionMailer::Base.send :include, AsynchronousMailer
82 ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP
83 ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail
84 ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
72 85
73 86 module ActionController
74 87 module MimeResponds
75 88 class Collector
76 89 def api(&block)
77 90 any(:xml, :json, &block)
78 91 end
79 92 end
80 93 end
81 94 end
General Comments 0
You need to be logged in to leave comments. Login now