Commit 973928e0b9d9a7e821cbd4fdef0f6a800eb7915c
1 parent
bd238bbe
Exists in
master
and in
27 other branches
rails3: fix organization_mailing
Showing
3 changed files
with
49 additions
and
51 deletions
Show diff stats
... | ... | @@ -0,0 +1,30 @@ |
1 | +class OrganizationMailing < Mailing | |
2 | + | |
3 | + def generate_from | |
4 | + "#{person.name} <#{source.environment.contact_email}>" | |
5 | + end | |
6 | + | |
7 | + def recipients(offset=0, limit=100) | |
8 | + source.members.all(:order => :id, :offset => offset, :limit => limit, :joins => "LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)", :conditions => { "m.person_id" => nil }) | |
9 | + end | |
10 | + | |
11 | + def each_recipient | |
12 | + offset = 0 | |
13 | + limit = 50 | |
14 | + while !(people = recipients(offset, limit)).empty? | |
15 | + people.each do |person| | |
16 | + yield person | |
17 | + end | |
18 | + offset = offset + limit | |
19 | + end | |
20 | + end | |
21 | + | |
22 | + def signature_message | |
23 | + _('Sent by community %s.') % source.name | |
24 | + end | |
25 | + | |
26 | + include Rails.application.routes.url_helpers | |
27 | + def url | |
28 | + url_for(source.url) | |
29 | + end | |
30 | +end | ... | ... |
app/models/organization_mailing.rb
... | ... | @@ -1,30 +0,0 @@ |
1 | -class OrganizationMailing < Mailing | |
2 | - | |
3 | - def generate_from | |
4 | - "#{person.name} <#{source.environment.contact_email}>" | |
5 | - end | |
6 | - | |
7 | - def recipients(offset=0, limit=100) | |
8 | - source.members.all(:order => :id, :offset => offset, :limit => limit, :joins => "LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)", :conditions => { "m.person_id" => nil }) | |
9 | - end | |
10 | - | |
11 | - def each_recipient | |
12 | - offset = 0 | |
13 | - limit = 50 | |
14 | - while !(people = recipients(offset, limit)).empty? | |
15 | - people.each do |person| | |
16 | - yield person | |
17 | - end | |
18 | - offset = offset + limit | |
19 | - end | |
20 | - end | |
21 | - | |
22 | - def signature_message | |
23 | - _('Sent by community %s.') % source.name | |
24 | - end | |
25 | - | |
26 | - include Rails.application.routes.url_helpers | |
27 | - def url | |
28 | - url_for(source.url) | |
29 | - end | |
30 | -end |
test/unit/organization_mailing_test.rb
... | ... | @@ -26,92 +26,90 @@ class OrganizationMailingTest < ActiveSupport::TestCase |
26 | 26 | end |
27 | 27 | |
28 | 28 | should 'return community name' do |
29 | - mailing = OrganizationMailing.new(:source => community) | |
29 | + mailing = build(OrganizationMailing, :source => community) | |
30 | 30 | assert_equal community.name, mailing.source.name |
31 | 31 | end |
32 | 32 | |
33 | 33 | should 'return community with source_id' do |
34 | - mailing = OrganizationMailing.new(:source => community) | |
34 | + mailing = build(OrganizationMailing, :source => community) | |
35 | 35 | assert_equal community, mailing.source |
36 | 36 | end |
37 | 37 | |
38 | 38 | should 'return person with person_id' do |
39 | - mailing = OrganizationMailing.new(:source => community, :person => person) | |
39 | + mailing = build(OrganizationMailing, :source => community, :person => person) | |
40 | 40 | assert_equal person, mailing.person |
41 | 41 | end |
42 | 42 | |
43 | 43 | should 'display name and email on generate_from' do |
44 | - mailing = OrganizationMailing.new(:source => community, :person => person) | |
44 | + mailing = build(OrganizationMailing, :source => community, :person => person) | |
45 | 45 | assert_equal "#{person.name} <#{community.environment.contact_email}>", mailing.generate_from |
46 | 46 | end |
47 | 47 | |
48 | 48 | should 'generate subject' do |
49 | - mailing = OrganizationMailing.new(:source => community, :subject => 'Hello :)') | |
49 | + mailing = build(OrganizationMailing, :source => community, :subject => 'Hello :)') | |
50 | 50 | assert_equal "[#{community.name}] #{mailing.subject}", mailing.generate_subject |
51 | 51 | end |
52 | 52 | |
53 | 53 | should 'return signature message' do |
54 | - mailing = OrganizationMailing.new(:source => community) | |
54 | + mailing = build(OrganizationMailing, :source => community) | |
55 | 55 | assert_equal "Sent by community #{community.name}.", mailing.signature_message |
56 | 56 | end |
57 | 57 | |
58 | 58 | should 'return url for organization on url' do |
59 | - mailing = OrganizationMailing.new(:source => community) | |
60 | - assert_equal "#{community.environment.top_url}/#{community.name.to_slug}", mailing.url | |
59 | + mailing = build(OrganizationMailing, :source => community) | |
60 | + assert_equal "#{community.environment.top_url}/#{community.name.to_slug}/", mailing.url | |
61 | 61 | end |
62 | 62 | |
63 | 63 | should 'deliver mailing to each member after create' do |
64 | - mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
64 | + create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
65 | 65 | process_delayed_job_queue |
66 | - assert_equal 2, ActionMailer::Base.deliveries.count | |
66 | + assert_equal community.members.compact.count, ActionMailer::Base.deliveries.count | |
67 | 67 | end |
68 | 68 | |
69 | 69 | should 'deliver mailing when there are many mailings created' do |
70 | - 50.times { OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) } | |
70 | + 50.times { create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) } | |
71 | 71 | process_delayed_job_queue |
72 | - assert_equal 50*community.members_count, ActionMailer::Base.deliveries.count | |
72 | + assert_equal 50*community.members.compact.count, ActionMailer::Base.deliveries.count | |
73 | 73 | end |
74 | 74 | |
75 | 75 | should 'create mailing sent to each recipient after delivering mailing' do |
76 | - mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
76 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
77 | 77 | assert_difference MailingSent, :count, 2 do |
78 | 78 | process_delayed_job_queue |
79 | 79 | end |
80 | 80 | end |
81 | 81 | |
82 | 82 | should 'change locale according to the mailing locale' do |
83 | - mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => person) | |
83 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => person) | |
84 | 84 | Noosfero.expects(:with_locale).with('pt') |
85 | 85 | process_delayed_job_queue |
86 | 86 | end |
87 | 87 | |
88 | 88 | should 'have community by source_id' do |
89 | - mailing = community.mailings.build(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
90 | - mailing.save! | |
91 | - | |
89 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
92 | 90 | assert_equal community, Mailing.find(mailing.id).source |
93 | 91 | end |
94 | 92 | |
95 | 93 | should 'return recipient' do |
96 | - mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
94 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
97 | 95 | assert_equal [Person['user_one'], Person['user_two']], mailing.recipients |
98 | 96 | end |
99 | 97 | |
100 | 98 | should 'return recipients according to limit' do |
101 | - mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
99 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
102 | 100 | assert_equal [Person['user_one']], mailing.recipients(0, 1) |
103 | 101 | end |
104 | 102 | |
105 | 103 | should 'return true if already sent mailing to a recipient' do |
106 | 104 | member = Person['user_one'] |
107 | - mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
105 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
108 | 106 | process_delayed_job_queue |
109 | 107 | assert mailing.mailing_sents.find_by_person_id(member.id) |
110 | 108 | end |
111 | 109 | |
112 | 110 | should 'return false if did not sent mailing to a recipient' do |
113 | 111 | recipient = fast_create(Person) |
114 | - mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
112 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
115 | 113 | process_delayed_job_queue |
116 | 114 | |
117 | 115 | assert !mailing.mailing_sents.find_by_person_id(recipient.id) | ... | ... |