robincurry-dl-fughley 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.markdown +71 -0
- data/Rakefile +56 -0
- data/VERSION.yml +4 -0
- data/lib/dl_fughley.rb +81 -0
- data/test/database.yml +3 -0
- data/test/dl_fughley_test.rb +460 -0
- data/test/factories.rb +7 -0
- data/test/schema.rb +8 -0
- data/test/test_helper.rb +52 -0
- metadata +66 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Robin Curry
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# DL-Fughley: Distribution List Fu - turn your models into email distribution lists.
|
2
|
+
|
3
|
+
Note - this project is still in its infacy and probably not quite ready for production use. Check back soon...
|
4
|
+
|
5
|
+
## Use named scopes, finders, or other methods as a distribution list
|
6
|
+
|
7
|
+
class Person < ActiveRecord::Base
|
8
|
+
is_dl :all # results in a dl for all@yourdomain.com
|
9
|
+
is_dl :guys # results in a dl for guys@yourdomain.com
|
10
|
+
is_dl :girls # results in a dl for girls@yourdomain.com
|
11
|
+
is_dl :peeps # results in a dl for peeps@yourdomain.com
|
12
|
+
|
13
|
+
named_scope :guys, :conditions => {:gender => 'M'}
|
14
|
+
named_scope :girls, :conditions => {:gender => 'F'}
|
15
|
+
|
16
|
+
def peeps
|
17
|
+
# return an enumerable containing your peeps
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
## Or, use dl_missing to handle more complex scenarios
|
24
|
+
|
25
|
+
class Person < ActiveRecord::Base
|
26
|
+
is_dl
|
27
|
+
|
28
|
+
def dl_missing(name)
|
29
|
+
Group.find_by_name(name).people
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
## Control who can send to the distribution list
|
36
|
+
|
37
|
+
class Person < ActiveRecord::Base
|
38
|
+
is_dl :all, :allow => [:members] # allow emails from anyone included in the distribution list.
|
39
|
+
is_dl :peeps, :allow => ['me@yourdomain.com', 'you@yourdomain.com', 'butnobodyelse@yourdomain.com']
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
## Munge replies to send them back to the distribution list
|
45
|
+
|
46
|
+
class Person < ActiveRecord::Base
|
47
|
+
is_dl :all, :reply_to => :list # replies will be sent to all@yourdomain.com
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
## Use a different from address for emails sent to distribution list
|
53
|
+
|
54
|
+
class Person < ActiveRecord::Base
|
55
|
+
is_dl :all, :from => 'info@yourdomain.com' # emails to group will come from info@yourdomain.com rather than original sender.
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
## Add a subject prefix to all new emails to distribution list
|
61
|
+
|
62
|
+
class Person < ActiveRecord::Base
|
63
|
+
is_dl :all, :subject_prefix => 'info' # Will prefix subjects with "[info]"
|
64
|
+
is_dl :peeps, :subject_prefix => :list_name # Will prefix subjects with "[peeps]"
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
## Copyright
|
70
|
+
|
71
|
+
Copyright (c) 2009 Robin Curry. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "dl-fughley"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "robin.curry@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/robincurry/dl-fughley"
|
11
|
+
gem.authors = ["robincurry"]
|
12
|
+
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
if File.exist?('VERSION.yml')
|
45
|
+
config = YAML.load(File.read('VERSION.yml'))
|
46
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
47
|
+
else
|
48
|
+
version = ""
|
49
|
+
end
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "dl-fughley #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
56
|
+
|
data/VERSION.yml
ADDED
data/lib/dl_fughley.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'activerecord'
|
3
|
+
require 'action_mailer'
|
4
|
+
|
5
|
+
module DL
|
6
|
+
module Fughley
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(IsDL)
|
9
|
+
end
|
10
|
+
|
11
|
+
module IsDL
|
12
|
+
def is_dl(*args)
|
13
|
+
options = args.extract_options!
|
14
|
+
(class << self; self; end).instance_eval do
|
15
|
+
dl_name = options[:as] || args.first
|
16
|
+
method_name = ("dl_" + dl_name.to_s).to_sym
|
17
|
+
send :define_method, method_name do
|
18
|
+
[self.send(dl_name), options || {}]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
extend ClassMethods unless (class << self; included_modules; end).include?(ClassMethods)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def dl(name)
|
28
|
+
method_name = ("dl_" + name.to_s).to_sym
|
29
|
+
if self.respond_to?(method_name)
|
30
|
+
self.send(method_name)
|
31
|
+
elsif self.respond_to?(:dl_missing)
|
32
|
+
self.send(:dl_missing, name)
|
33
|
+
else
|
34
|
+
[]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def distribute(email)
|
39
|
+
to = email.to.first
|
40
|
+
list = to.gsub(/@.*$/, '')
|
41
|
+
|
42
|
+
|
43
|
+
people = dl(list)
|
44
|
+
options = {
|
45
|
+
:allow => [],
|
46
|
+
:email_field => :email,
|
47
|
+
:reply_to => :sender
|
48
|
+
}.merge!(people.extract_options!)
|
49
|
+
|
50
|
+
people.flatten!
|
51
|
+
|
52
|
+
if (options[:allow].nil? || options[:allow].empty?) ||
|
53
|
+
options[:allow].include?(email.from.first) ||
|
54
|
+
(options[:allow].include?(:members) && people.detect{|p| p.email == email.from.first})
|
55
|
+
|
56
|
+
people.each do |person|
|
57
|
+
email.to = person.send(options[:email_field])
|
58
|
+
email.reply_to = email.from if options[:reply_to] == :sender
|
59
|
+
email.reply_to = to if options[:reply_to] == :list
|
60
|
+
email.from = options[:from] if options[:from]
|
61
|
+
|
62
|
+
case options[:subject_prefix]
|
63
|
+
when :list_name
|
64
|
+
email.subject = "[#{list.downcase}] #{email.subject}" unless email.subject =~ /\[#{list}\]/i
|
65
|
+
when false, nil
|
66
|
+
# do nothing
|
67
|
+
else
|
68
|
+
email.subject = "[#{options[:subject_prefix]}] #{email.subject}" unless email.subject =~ /\[#{options[:subject_prefix]}\]/i
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
ActionMailer::Base.deliver(email)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
ActiveRecord::Base.send(:include, DL::Fughley)
|
data/test/database.yml
ADDED
@@ -0,0 +1,460 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DlFughleyTest < ActiveSupport::TestCase
|
4
|
+
context "receive email" do
|
5
|
+
setup do
|
6
|
+
@people = []
|
7
|
+
1.upto(5) do |i|
|
8
|
+
@people[i - 1] = Factory(:person)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Establish that the person class is a dl
|
12
|
+
Person.is_dl
|
13
|
+
|
14
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
15
|
+
ActionMailer::Base.deliveries = []
|
16
|
+
end
|
17
|
+
|
18
|
+
context "addressed to yall@mydomain.com (a non-existent group)" do
|
19
|
+
setup do
|
20
|
+
email = to_email(
|
21
|
+
:from => 'user@example.com',
|
22
|
+
:to => 'yall@mydomain.com',
|
23
|
+
:subject => 'test to the whole group',
|
24
|
+
:body => 'Hello Entire Group!')
|
25
|
+
PostOffice.receive(email.to_s)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "not trigger delivery to anybody" do
|
29
|
+
assert_deliveries 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "addressed to [some group name]@mydomain.com" do
|
34
|
+
setup do
|
35
|
+
# add a leaders distribution list to the Person class that only returns 2 people.
|
36
|
+
Person.instance_eval do
|
37
|
+
def dl_leaders
|
38
|
+
[Factory(:person), Factory(:person)]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
email = to_email(
|
43
|
+
:from => 'user@example.com',
|
44
|
+
:to => 'leaders@example.com',
|
45
|
+
:subject => 'test to a group',
|
46
|
+
:body => 'Hello Leaders!')
|
47
|
+
PostOffice.receive(email.to_s)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "trigger delivery to each person in the group" do
|
51
|
+
assert_deliveries 2
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "addressed to [some other group]@mydomain.com" do
|
56
|
+
setup do
|
57
|
+
# add a holla distribution list to the Person class that only returns 3 people.
|
58
|
+
Person.instance_eval do
|
59
|
+
def dl_holla
|
60
|
+
[Factory(:person), Factory(:person), Factory(:person)]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
email = to_email(
|
65
|
+
:from => 'user@example.com',
|
66
|
+
:to => 'holla@example.com',
|
67
|
+
:subject => 'test to the group',
|
68
|
+
:body => 'Holla!')
|
69
|
+
PostOffice.receive(email.to_s)
|
70
|
+
end
|
71
|
+
|
72
|
+
should "trigger delivery to each person in the group" do
|
73
|
+
assert_deliveries 3
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "addressed to all@mydomain.com (a named scope)" do
|
78
|
+
setup do
|
79
|
+
# Tell the person class that Person.all is a dl.
|
80
|
+
Person.is_dl(:all)
|
81
|
+
|
82
|
+
email = to_email(
|
83
|
+
:from => 'user@example.com',
|
84
|
+
:to => 'all@example.com',
|
85
|
+
:subject => 'test to the whole group',
|
86
|
+
:body => 'Hello everybody!')
|
87
|
+
PostOffice.receive(email.to_s)
|
88
|
+
end
|
89
|
+
|
90
|
+
should "trigger delivery to each person" do
|
91
|
+
assert_deliveries Person.count
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "addressed to yousguys@mydomain.com (a group handled by dl_missing)" do
|
96
|
+
setup do
|
97
|
+
Person.instance_eval do
|
98
|
+
def dl_missing(name, *args)
|
99
|
+
if name.to_s =~ /^yousguys$/ then
|
100
|
+
[Factory(:person), Factory(:person), Factory(:person)]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
email = to_email(
|
106
|
+
:from => 'user@example.com',
|
107
|
+
:to => 'yousguys@example.com',
|
108
|
+
:subject => 'test to a missing group',
|
109
|
+
:body => 'Hello everybody!')
|
110
|
+
PostOffice.receive(email.to_s)
|
111
|
+
end
|
112
|
+
|
113
|
+
should "trigger delivery to each person in the group" do
|
114
|
+
assert_deliveries 3
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
context "dl setup" do
|
121
|
+
context "duplicate lists" do
|
122
|
+
setup do
|
123
|
+
Person.is_dl(:all)
|
124
|
+
Person.is_dl(:all)
|
125
|
+
end
|
126
|
+
|
127
|
+
should "not fail. it should only create one dl_all method" do
|
128
|
+
assert true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context ":as option" do
|
133
|
+
setup do
|
134
|
+
Person.is_dl(:all, :as => :everyone)
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
should "result in a dl that uses the named_scope but with the provided :as name" do
|
139
|
+
assert Person.respond_to?(:dl_everyone)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context ":email_field option" do
|
144
|
+
setup do
|
145
|
+
Person.class_eval do
|
146
|
+
def email_addy
|
147
|
+
"alternate_email@yourdomain.com"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
1.upto(2) do |i|
|
152
|
+
Factory(:person)
|
153
|
+
end
|
154
|
+
|
155
|
+
Person.is_dl(:all, :email_field => :email_addy)
|
156
|
+
|
157
|
+
email = to_email(
|
158
|
+
:from => 'user@example.com',
|
159
|
+
:to => 'all@example.com',
|
160
|
+
:subject => 'test to the whole group',
|
161
|
+
:body => 'Hello everybody!')
|
162
|
+
PostOffice.receive(email.to_s)
|
163
|
+
end
|
164
|
+
|
165
|
+
should "use the provided field for email address" do
|
166
|
+
assert_sent_email do |email|
|
167
|
+
email.to.include?('alternate_email@yourdomain.com')
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context ":allow option with whitelist (:allow => ['allowed_user_1@example.com', 'allowed_user_2@example.com'])" do
|
173
|
+
setup do
|
174
|
+
1.upto(2) do |i|
|
175
|
+
Factory(:person)
|
176
|
+
end
|
177
|
+
|
178
|
+
Person.is_dl(:all, :allow => ['allowed_user_1@example.com', 'allowed_user_2@example.com'])
|
179
|
+
|
180
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
181
|
+
ActionMailer::Base.deliveries = []
|
182
|
+
end
|
183
|
+
|
184
|
+
should "not allow a user not in whitelist to send email" do
|
185
|
+
email = to_email(
|
186
|
+
:from => 'spammer@example.com',
|
187
|
+
:to => 'all@example.com',
|
188
|
+
:subject => 'test to the whole group',
|
189
|
+
:body => 'Hello everybody!')
|
190
|
+
PostOffice.receive(email.to_s)
|
191
|
+
|
192
|
+
assert_deliveries 0
|
193
|
+
end
|
194
|
+
|
195
|
+
should "allow a user in the whitelist to send email" do
|
196
|
+
email = to_email(
|
197
|
+
:from => 'allowed_user_2@example.com',
|
198
|
+
:to => 'all@example.com',
|
199
|
+
:subject => 'test to the whole group',
|
200
|
+
:body => 'Hello everybody!')
|
201
|
+
PostOffice.receive(email.to_s)
|
202
|
+
|
203
|
+
assert_deliveries Person.count
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context ":allow option with :members option (:allow => [:members])" do
|
208
|
+
setup do
|
209
|
+
Factory(:person, :email => "allowed_user_1@example.com")
|
210
|
+
Factory(:person, :email => "allowed_user_2@example.com")
|
211
|
+
|
212
|
+
Person.is_dl(:all, :allow => [:members])
|
213
|
+
|
214
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
215
|
+
ActionMailer::Base.deliveries = []
|
216
|
+
end
|
217
|
+
|
218
|
+
should "not allow a user not a member of group to send email" do
|
219
|
+
email = to_email(
|
220
|
+
:from => 'spammer@example.com',
|
221
|
+
:to => 'all@example.com',
|
222
|
+
:subject => 'test to the whole group',
|
223
|
+
:body => 'Hello everybody!')
|
224
|
+
PostOffice.receive(email.to_s)
|
225
|
+
|
226
|
+
assert_deliveries 0
|
227
|
+
end
|
228
|
+
|
229
|
+
should "allow a user in the group to send email" do
|
230
|
+
email = to_email(
|
231
|
+
:from => 'allowed_user_1@example.com',
|
232
|
+
:to => 'all@example.com',
|
233
|
+
:subject => 'test to the whole group',
|
234
|
+
:body => 'Hello everybody!')
|
235
|
+
PostOffice.receive(email.to_s)
|
236
|
+
|
237
|
+
assert_deliveries Person.count
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
context ":from option" do
|
242
|
+
setup do
|
243
|
+
Factory(:person)
|
244
|
+
Person.is_dl(:all, :from => "info@example.com")
|
245
|
+
|
246
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
247
|
+
ActionMailer::Base.deliveries = []
|
248
|
+
|
249
|
+
email = to_email(
|
250
|
+
:from => 'user@example.com',
|
251
|
+
:to => 'all@example.com',
|
252
|
+
:subject => 'test to the whole group',
|
253
|
+
:body => 'Hello everybody!')
|
254
|
+
PostOffice.receive(email.to_s)
|
255
|
+
end
|
256
|
+
|
257
|
+
should "use provided :from address in place of the origin email from address" do
|
258
|
+
assert_sent_email do |email|
|
259
|
+
email.from.include?('info@example.com')
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
context ":reply_to option is :sender (:reply_to => :sender)" do
|
265
|
+
setup do
|
266
|
+
Factory(:person)
|
267
|
+
Person.is_dl(:all, :reply_to => :sender)
|
268
|
+
|
269
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
270
|
+
ActionMailer::Base.deliveries = []
|
271
|
+
|
272
|
+
@email = to_email(
|
273
|
+
:from => 'user@example.com',
|
274
|
+
:to => 'all@example.com',
|
275
|
+
:subject => 'test to the whole group',
|
276
|
+
:body => 'Hello everybody!')
|
277
|
+
PostOffice.receive(@email.to_s)
|
278
|
+
end
|
279
|
+
|
280
|
+
should "send emails with reply-to header set to sender's address" do
|
281
|
+
assert_sent_email do |email|
|
282
|
+
email.reply_to == @email.from
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
context ":reply_to option is :list (:reply_to => :list)" do
|
288
|
+
setup do
|
289
|
+
Factory(:person)
|
290
|
+
Person.is_dl(:all, :reply_to => :list)
|
291
|
+
|
292
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
293
|
+
ActionMailer::Base.deliveries = []
|
294
|
+
|
295
|
+
@email = to_email(
|
296
|
+
:from => 'user@example.com',
|
297
|
+
:to => 'all@example.com',
|
298
|
+
:subject => 'test to the whole group',
|
299
|
+
:body => 'Hello everybody!')
|
300
|
+
PostOffice.receive(@email.to_s)
|
301
|
+
end
|
302
|
+
|
303
|
+
should "send emails with reply-to header set to list address" do
|
304
|
+
assert_sent_email do |email|
|
305
|
+
email.reply_to == @email.to
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
context ":subject_prefix => :list_name" do
|
311
|
+
setup do
|
312
|
+
Factory(:person)
|
313
|
+
Person.is_dl(:all, :subject_prefix => :list_name)
|
314
|
+
|
315
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
316
|
+
ActionMailer::Base.deliveries = []
|
317
|
+
|
318
|
+
@email = to_email(
|
319
|
+
:from => 'user@example.com',
|
320
|
+
:to => 'all@example.com',
|
321
|
+
:subject => 'test to the whole group',
|
322
|
+
:body => 'Hello everybody!')
|
323
|
+
PostOffice.receive(@email.to_s)
|
324
|
+
end
|
325
|
+
|
326
|
+
should "send emails with a subject prefix matching the downcased list name" do
|
327
|
+
assert_sent_email do |email|
|
328
|
+
email.subject =~ /\[all\]/
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
context ":subject_prefix => 'all-my-peeps'" do
|
334
|
+
setup do
|
335
|
+
Factory(:person)
|
336
|
+
Person.is_dl(:all, :subject_prefix => "all-my-peeps")
|
337
|
+
|
338
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
339
|
+
ActionMailer::Base.deliveries = []
|
340
|
+
|
341
|
+
@email = to_email(
|
342
|
+
:from => 'user@example.com',
|
343
|
+
:to => 'all@example.com',
|
344
|
+
:subject => 'test to the whole group',
|
345
|
+
:body => 'Hello everybody!')
|
346
|
+
PostOffice.receive(@email.to_s)
|
347
|
+
end
|
348
|
+
|
349
|
+
should "send emails with a subject prefix matching the downcased list name" do
|
350
|
+
assert_sent_email do |email|
|
351
|
+
email.subject =~ /\[all-my-peeps\]/
|
352
|
+
end
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
context ":subject_prefix => false" do
|
357
|
+
setup do
|
358
|
+
Factory(:person)
|
359
|
+
Person.is_dl(:all, :subject_prefix => false)
|
360
|
+
|
361
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
362
|
+
ActionMailer::Base.deliveries = []
|
363
|
+
|
364
|
+
@email = to_email(
|
365
|
+
:from => 'user@example.com',
|
366
|
+
:to => 'all@example.com',
|
367
|
+
:subject => 'test to the whole group',
|
368
|
+
:body => 'Hello everybody!')
|
369
|
+
PostOffice.receive(@email.to_s)
|
370
|
+
end
|
371
|
+
|
372
|
+
should "send emails without a subject prefix" do
|
373
|
+
assert_sent_email do |email|
|
374
|
+
!(email.subject =~ /\[all\]/)
|
375
|
+
end
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
context ":subject_prefix => nil" do
|
380
|
+
setup do
|
381
|
+
Factory(:person)
|
382
|
+
Person.is_dl(:all, :subject_prefix => nil)
|
383
|
+
|
384
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
385
|
+
ActionMailer::Base.deliveries = []
|
386
|
+
|
387
|
+
@email = to_email(
|
388
|
+
:from => 'user@example.com',
|
389
|
+
:to => 'all@example.com',
|
390
|
+
:subject => 'test to the whole group',
|
391
|
+
:body => 'Hello everybody!')
|
392
|
+
PostOffice.receive(@email.to_s)
|
393
|
+
end
|
394
|
+
|
395
|
+
should "send emails without a subject prefix" do
|
396
|
+
assert_sent_email do |email|
|
397
|
+
!(email.subject =~ /\[all\]/)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
context ":subject_prefix => :list_name, but prefix is already included (in a reply, for instance)" do
|
403
|
+
setup do
|
404
|
+
Factory(:person)
|
405
|
+
Person.is_dl(:all, :subject_prefix => :list_name)
|
406
|
+
|
407
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
408
|
+
ActionMailer::Base.deliveries = []
|
409
|
+
|
410
|
+
@email = to_email(
|
411
|
+
:from => 'user@example.com',
|
412
|
+
:to => 'all@example.com',
|
413
|
+
:subject => 'RE: [all] test to the whole group',
|
414
|
+
:body => 'Hello everybody!')
|
415
|
+
PostOffice.receive(@email.to_s)
|
416
|
+
end
|
417
|
+
|
418
|
+
should "not have multiple prefixes in the subject" do
|
419
|
+
assert_sent_email do |email|
|
420
|
+
!(email.subject =~ /\[all\].*\[all\]/)
|
421
|
+
end
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
context ":subject_prefix => 'all-my-peeps', but prefix is already included (in a reply, for instance)" do
|
426
|
+
setup do
|
427
|
+
Factory(:person)
|
428
|
+
Person.is_dl(:all, :subject_prefix => "all-my-peeps")
|
429
|
+
|
430
|
+
# Reset deliveries during setup. Otherwise they will accumulate after each test.
|
431
|
+
ActionMailer::Base.deliveries = []
|
432
|
+
|
433
|
+
@email = to_email(
|
434
|
+
:from => 'user@example.com',
|
435
|
+
:to => 'all@example.com',
|
436
|
+
:subject => 're: [all-my-peeps] test to the whole group',
|
437
|
+
:body => 'Hello everybody!')
|
438
|
+
PostOffice.receive(@email.to_s)
|
439
|
+
end
|
440
|
+
|
441
|
+
should "send emails with a subject prefix matching the downcased list name" do
|
442
|
+
assert_sent_email do |email|
|
443
|
+
!(email.subject =~ /\[all-my-peeps\].*\[all-my-peeps\]/)
|
444
|
+
end
|
445
|
+
end
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
|
450
|
+
private
|
451
|
+
def to_email(values)
|
452
|
+
values.symbolize_keys!
|
453
|
+
email = TMail::Mail.new
|
454
|
+
email.to = values[:to]
|
455
|
+
email.from = values[:from]
|
456
|
+
email.subject = values[:subject]
|
457
|
+
email.body = values[:body]
|
458
|
+
email
|
459
|
+
end
|
460
|
+
end
|
data/test/factories.rb
ADDED
data/test/schema.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/test_case'
|
6
|
+
|
7
|
+
require 'shoulda'
|
8
|
+
require 'shoulda/action_mailer'
|
9
|
+
require 'factory_girl'
|
10
|
+
|
11
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
12
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
+
require 'dl_fughley'
|
14
|
+
|
15
|
+
|
16
|
+
class Person < ActiveRecord::Base
|
17
|
+
validates_presence_of :email, :message => "can't be blank"
|
18
|
+
end
|
19
|
+
|
20
|
+
class PostOffice < ActionMailer::Base
|
21
|
+
|
22
|
+
def receive(email)
|
23
|
+
Person.distribute(email)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def connect(environment)
|
29
|
+
conf = YAML::load(File.open(File.dirname(__FILE__) + '/database.yml'))
|
30
|
+
ActiveRecord::Base.establish_connection(conf[environment])
|
31
|
+
end
|
32
|
+
connect('test')
|
33
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
34
|
+
|
35
|
+
|
36
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
37
|
+
# The :test delivery method accumulates sent emails in the
|
38
|
+
# ActionMailer::Base.deliveries array.
|
39
|
+
ActionMailer::Base.delivery_method = :test
|
40
|
+
|
41
|
+
class ActiveSupport::TestCase
|
42
|
+
# fixtures :all
|
43
|
+
|
44
|
+
def assert_deliveries(count)
|
45
|
+
assert_equal count, ActionMailer::Base.deliveries.length
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: robincurry-dl-fughley
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- robincurry
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: robin.curry@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.markdown
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.markdown
|
28
|
+
- Rakefile
|
29
|
+
- VERSION.yml
|
30
|
+
- lib/dl_fughley.rb
|
31
|
+
- test/database.yml
|
32
|
+
- test/dl_fughley_test.rb
|
33
|
+
- test/factories.rb
|
34
|
+
- test/schema.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/robincurry/dl-fughley
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: TODO
|
62
|
+
test_files:
|
63
|
+
- test/dl_fughley_test.rb
|
64
|
+
- test/factories.rb
|
65
|
+
- test/schema.rb
|
66
|
+
- test/test_helper.rb
|