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