Skip to content

Commit 37df263

Browse files
committed
adding tests and little refactoring
1 parent c4a9d2e commit 37df263

File tree

3 files changed

+188
-168
lines changed

3 files changed

+188
-168
lines changed

Diff for: .ruby-gemset

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ifme

Diff for: app/mailers/notification_mailer.rb

+146-151
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,161 @@
11
class NotificationMailer < ApplicationMailer
22
default from: ENV["DEFAULT_FROM_ADDRESS"]
33

4+
ALLY_NOTIFY_TYPES = ["new_ally_request", "accepted_ally_request"]
5+
46
def take_medication(reminder)
57
@medication = reminder.medication
68
@user = @medication.user
7-
mail(
8-
to: @user.email,
9-
subject: "Don't forget to take #{@medication.name}!"
10-
)
9+
mail(to: @user.email,
10+
subject: "Don't forget to take #{@medication.name}!")
1111
end
1212

1313
def refill_medication(reminder)
1414
@medication = reminder.medication
1515
@user = @medication.user
16-
mail(
17-
to: @user.email,
18-
subject: "Your refill for #{@medication.name} is coming up soon!"
19-
)
16+
mail(to: @user.email,
17+
subject: "Your refill for #{@medication.name} is coming up soon!")
2018
end
2119

22-
2320
def notification_email(recipientid, data)
24-
@data = JSON.parse(data)
25-
@recipient = User.where(id: recipientid).first
26-
subject = 'if me | '
27-
28-
if (can_notify(@recipient, 'comment_notify') &&
29-
(@data['type'] == 'comment_on_moment' ||
30-
@data['type'] == 'comment_on_moment_private' ||
31-
@data['type'] == 'comment_on_strategy' ||
32-
@data['type'] == 'comment_on_strategy_private' ||
33-
@data['type'] == 'comment_on_meeting'))
34-
35-
if (@data['type'] == 'comment_on_moment' || @data['type'] == 'comment_on_moment_private')
36-
subject += @data['user'].to_s + ' commented on your moment "' + @data['moment'].to_s + '"'
37-
elsif @data['type'] == 'comment_on_strategy' || @data['type'] == 'comment_on_strategy_private'
38-
subject += @data['user'].to_s + ' commented on your strategy "' + @data['strategy'].to_s + '"'
39-
else
40-
groupid = Meeting.where(id: @data['meetingid']).first.groupid
41-
group = Group.where(id: groupid).first.name
42-
subject += @data['user'].to_s + ' commented on the meeting "' + @data['meeting'].to_s + '" in the group "' + group.to_s + '"'
43-
end
44-
45-
if @data['type'] == 'comment_on_moment_private' || @data['type'] == 'comment_on_strategy_private'
46-
@message = '<p>Your ally <strong>' + @data['user'].to_s + '</strong> commented privately:</p>';
47-
else
48-
@message = '<p>Your ally <strong>' + @data['user'].to_s + '</strong> commented:</p>';
49-
end
50-
51-
if @data['cutoff']
52-
@message += '<p><i>' + @data['comment'].to_s + ' [...]</i></p>'
53-
else
54-
@message += '<p><i>' + @data['comment'].to_s + '</i></p>'
55-
end
56-
57-
if (@data['type'] == 'comment_on_moment' || @data['type'] == 'comment_on_moment_private')
58-
link = link_to("here", moment_url(@data['momentid']))
59-
@message += '<p>You can read it all ' + link + '!</p>'
60-
elsif @data['type'] == 'comment_on_strategy' || @data['type'] == 'comment_on_strategy_private'
61-
link = link_to("here", strategy_url(@data['strategyid']))
62-
@message += '<p>You can read it all ' + link + '!</p>'
63-
else
64-
link = link_to("here", meeting_url(@data['meetingid']))
65-
@message += '<p>You can read it all ' + link + '!</p>'
66-
end
67-
68-
mail(to: @recipient.email, subject: subject)
69-
elsif (can_notify(@recipient, 'ally_notify') &&
70-
(@data['type'] == 'accepted_ally_request' ||
71-
@data['type'] == 'new_ally_request'))
72-
73-
if @data['type'] == 'accepted_ally_request'
74-
subject += @data['user'].to_s + ' accepted your ally request!'
75-
@message = '<p>Congrats! You can now share Moments, Strategies, and more with ' + @data['user'].to_s + '.</p>'
76-
else
77-
subject += @data['user'].to_s + ' sent an ally request!'
78-
link = link_to("sign in", allies_url)
79-
@message = '<p>Please ' + link + ' to accept or reject the request!</p>'
80-
end
81-
82-
mail(to: @recipient.email, subject: subject)
83-
elsif ((can_notify(@recipient, 'group_notify') &&
84-
(@data['type'] == 'new_group' ||
85-
@data['type'] == 'new_group_member' ||
86-
@data['type'] == 'add_group_leader' ||
87-
@data['type'] == 'remove_group_leader')) ||
88-
(can_notify(@recipient, 'meeting_notify') &&
89-
(@data['type'] == 'new_meeting' ||
90-
@data['type'] == 'remove_meeting' ||
91-
@data['type'] == 'update_meeting' ||
92-
@data['type'] == 'join_meeting')))
93-
94-
if @data['type'] == 'new_group'
95-
what = @data['user'].to_s + ' created a group "' + @data['group'].to_s + '"'
96-
subject += what
97-
group_description = Group.where(id: @data['groupid']).first.description
98-
link = link_to("click here", group_url(@data['groupid']))
99-
@message = '<p>' + what + ':</p>'
100-
@message += '<p><i>' + group_description.to_s + '</i></p>'
101-
@message += '<p>To learn more and join, ' + link + '!</p>'
102-
103-
elsif @data['type'] == 'new_group_member'
104-
subject += @data['user'].to_s + ' joined your group "' + @data['group'].to_s + '"'
105-
link = link_to("click here", group_url(@data['groupid']))
106-
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '!</p>'
107-
108-
elsif @data['type'] == 'add_group_leader'
109-
if @recipient.name == @data['user']
110-
subject += 'You became a leader of "' + @data['group'].to_s + '"'
111-
else
112-
subject += @data['user'].to_s + ' became a leader of "' + @data['group'].to_s + '"'
113-
end
114-
link = link_to("click here", group_url(@data['groupid']))
115-
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '! </p>'
116-
117-
elsif @data['type'] == 'remove_group_leader'
118-
if @recipient.name == @data['user']
119-
subject += 'You are no longer a leader of "' + @data['group'].to_s + '"'
120-
else
121-
subject += @data['user'].to_s + ' is no longer a leader of "' + @data['group'].to_s + '"'
122-
end
123-
link = link_to("click here", group_url(@data['groupid']))
124-
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '!</p>'
125-
126-
elsif @data['type'] == 'new_meeting' || @data['type'] == 'update_meeting'
127-
if @data['type'] == 'update_meeting'
128-
what = @data['user'].to_s + ' has updated the meeting "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
129-
else
130-
what = @data['user'].to_s + ' created a new meeting "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
131-
end
132-
subject += what
133-
134-
meeting_description = Meeting.where(id: @data['meetingid']).first.description
135-
meeting_location = Meeting.where(id: @data['meetingid']).first.location
136-
meeting_date = Meeting.where(id: @data['meetingid']).first.date
137-
meeting_time = Meeting.where(id: @data['meetingid']).first.time
138-
link = link_to("click here", meeting_url(@data['meetingid']))
139-
@message = '<p>' + what + ':</p>'
140-
@message += '<p><i>' + meeting_description.to_s + '</i></p>'
141-
@message += '<p><strong>Location:</strong> ' + meeting_location.to_s + '</p>'
142-
@message += '<p><strong>Date:</strong> ' + meeting_date.to_s + '</p>'
143-
@message += '<p><strong>Time:</strong> ' + meeting_time.to_s + '</p>'
144-
145-
if @data['type'] == 'new_meeting'
146-
@message += '<p>To learn more and attend, ' + link + '!</p>'
147-
else
148-
@message += '<p>To learn more, ' + link + '!</p>'
149-
end
150-
151-
elsif @data['type'] == 'remove_meeting'
152-
subject += @data['user'].to_s + ' has cancelled "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
153-
link = link_to("click here", group_url(@data['groupid']))
154-
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '!</p>'
155-
156-
elsif @data['type'] == 'join_meeting'
157-
subject += @data['user'].to_s + ' has joined "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
158-
link = link_to("click here", meeting_url(@data['meetingid']))
159-
@message = '<p>To see ' + @data['meeting'].to_s + ', ' + link + '!</p>'
160-
161-
end
162-
163-
mail(to: @recipient.email, subject: subject)
164-
end
165-
end
21+
@data = JSON.parse(data)
22+
@recipient = User.where(id: recipientid).first
23+
subject = 'if me | '
24+
25+
if (can_notify(@recipient, 'comment_notify') &&
26+
(@data['type'] == 'comment_on_moment' ||
27+
@data['type'] == 'comment_on_moment_private' ||
28+
@data['type'] == 'comment_on_strategy' ||
29+
@data['type'] == 'comment_on_strategy_private' ||
30+
@data['type'] == 'comment_on_meeting'))
31+
32+
if (@data['type'] == 'comment_on_moment' || @data['type'] == 'comment_on_moment_private')
33+
subject += @data['user'].to_s + ' commented on your moment "' + @data['moment'].to_s + '"'
34+
elsif @data['type'] == 'comment_on_strategy' || @data['type'] == 'comment_on_strategy_private'
35+
subject += @data['user'].to_s + ' commented on your strategy "' + @data['strategy'].to_s + '"'
36+
else
37+
groupid = Meeting.where(id: @data['meetingid']).first.groupid
38+
group = Group.where(id: groupid).first.name
39+
subject += @data['user'].to_s + ' commented on the meeting "' + @data['meeting'].to_s + '" in the group "' + group.to_s + '"'
40+
end
41+
42+
if @data['type'] == 'comment_on_moment_private' || @data['type'] == 'comment_on_strategy_private'
43+
@message = '<p>Your ally <strong>' + @data['user'].to_s + '</strong> commented privately:</p>';
44+
else
45+
@message = '<p>Your ally <strong>' + @data['user'].to_s + '</strong> commented:</p>';
46+
end
47+
48+
if @data['cutoff']
49+
@message += '<p><i>' + @data['comment'].to_s + ' [...]</i></p>'
50+
else
51+
@message += '<p><i>' + @data['comment'].to_s + '</i></p>'
52+
end
53+
54+
if (@data['type'] == 'comment_on_moment' || @data['type'] == 'comment_on_moment_private')
55+
link = link_to("here", moment_url(@data['momentid']))
56+
@message += '<p>You can read it all ' + link + '!</p>'
57+
elsif @data['type'] == 'comment_on_strategy' || @data['type'] == 'comment_on_strategy_private'
58+
link = link_to("here", strategy_url(@data['strategyid']))
59+
@message += '<p>You can read it all ' + link + '!</p>'
60+
else
61+
link = link_to("here", meeting_url(@data['meetingid']))
62+
@message += '<p>You can read it all ' + link + '!</p>'
63+
end
64+
65+
mail(to: @recipient.email, subject: subject)
66+
elsif (can_notify(@recipient, 'ally_notify') && ALLY_NOTIFY_TYPES.include?(@data['type']))
67+
68+
if @data['type'] == 'accepted_ally_request'
69+
subject += @data['user'].to_s + ' accepted your ally request!'
70+
@message = '<p>Congrats! You can now share Moments, Strategies, and more with ' + @data['user'].to_s + '.</p>'
71+
else
72+
subject += @data['user'].to_s + ' sent an ally request!'
73+
link = link_to("sign in", allies_url)
74+
@message = '<p>Please ' + link + ' to accept or reject the request!</p>'
75+
end
76+
77+
mail(to: @recipient.email, subject: subject)
78+
elsif ((can_notify(@recipient, 'group_notify') &&
79+
(@data['type'] == 'new_group' ||
80+
@data['type'] == 'new_group_member' ||
81+
@data['type'] == 'add_group_leader' ||
82+
@data['type'] == 'remove_group_leader')) ||
83+
(can_notify(@recipient, 'meeting_notify') &&
84+
(@data['type'] == 'new_meeting' ||
85+
@data['type'] == 'remove_meeting' ||
86+
@data['type'] == 'update_meeting' ||
87+
@data['type'] == 'join_meeting')))
88+
89+
if @data['type'] == 'new_group'
90+
what = @data['user'].to_s + ' created a group "' + @data['group'].to_s + '"'
91+
subject += what
92+
group_description = Group.where(id: @data['groupid']).first.description
93+
link = link_to("click here", group_url(@data['groupid']))
94+
@message = '<p>' + what + ':</p>'
95+
@message += '<p><i>' + group_description.to_s + '</i></p>'
96+
@message += '<p>To learn more and join, ' + link + '!</p>'
97+
98+
elsif @data['type'] == 'new_group_member'
99+
subject += @data['user'].to_s + ' joined your group "' + @data['group'].to_s + '"'
100+
link = link_to("click here", group_url(@data['groupid']))
101+
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '!</p>'
102+
103+
elsif @data['type'] == 'add_group_leader'
104+
if @recipient.name == @data['user']
105+
subject += 'You became a leader of "' + @data['group'].to_s + '"'
106+
else
107+
subject += @data['user'].to_s + ' became a leader of "' + @data['group'].to_s + '"'
108+
end
109+
link = link_to("click here", group_url(@data['groupid']))
110+
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '! </p>'
111+
112+
elsif @data['type'] == 'remove_group_leader'
113+
if @recipient.name == @data['user']
114+
subject += 'You are no longer a leader of "' + @data['group'].to_s + '"'
115+
else
116+
subject += @data['user'].to_s + ' is no longer a leader of "' + @data['group'].to_s + '"'
117+
end
118+
link = link_to("click here", group_url(@data['groupid']))
119+
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '!</p>'
120+
121+
elsif @data['type'] == 'new_meeting' || @data['type'] == 'update_meeting'
122+
if @data['type'] == 'update_meeting'
123+
what = @data['user'].to_s + ' has updated the meeting "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
124+
else
125+
what = @data['user'].to_s + ' created a new meeting "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
126+
end
127+
subject += what
128+
129+
meeting_description = Meeting.where(id: @data['meetingid']).first.description
130+
meeting_location = Meeting.where(id: @data['meetingid']).first.location
131+
meeting_date = Meeting.where(id: @data['meetingid']).first.date
132+
meeting_time = Meeting.where(id: @data['meetingid']).first.time
133+
link = link_to("click here", meeting_url(@data['meetingid']))
134+
@message = '<p>' + what + ':</p>'
135+
@message += '<p><i>' + meeting_description.to_s + '</i></p>'
136+
@message += '<p><strong>Location:</strong> ' + meeting_location.to_s + '</p>'
137+
@message += '<p><strong>Date:</strong> ' + meeting_date.to_s + '</p>'
138+
@message += '<p><strong>Time:</strong> ' + meeting_time.to_s + '</p>'
139+
140+
if @data['type'] == 'new_meeting'
141+
@message += '<p>To learn more and attend, ' + link + '!</p>'
142+
else
143+
@message += '<p>To learn more, ' + link + '!</p>'
144+
end
145+
146+
elsif @data['type'] == 'remove_meeting'
147+
subject += @data['user'].to_s + ' has cancelled "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
148+
link = link_to("click here", group_url(@data['groupid']))
149+
@message = '<p>To see ' + @data['group'].to_s + ', ' + link + '!</p>'
150+
151+
elsif @data['type'] == 'join_meeting'
152+
subject += @data['user'].to_s + ' has joined "' + @data['meeting'].to_s + '" for "' + @data['group'].to_s + '"'
153+
link = link_to("click here", meeting_url(@data['meetingid']))
154+
@message = '<p>To see ' + @data['meeting'].to_s + ', ' + link + '!</p>'
155+
156+
end
157+
158+
mail(to: @recipient.email, subject: subject)
159+
end
160+
end
166161
end

Diff for: spec/mailers/notification_mailer_spec.rb

+41-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
11
require "spec_helper"
22

33
describe "NotificationMailer" do
4+
let(:recipient) { FactoryGirl.create(:user1, email: "[email protected]") }
5+
let(:medication) { FactoryGirl.create(:medication, userid: recipient.id) }
6+
let(:reminder) { FactoryGirl.create(:take_medication_reminder, medication_id: medication.id) }
7+
48
describe "#take_medication" do
5-
let(:recipient) { FactoryGirl.create(:user1, email: "[email protected]") }
6-
let!(:medication) { FactoryGirl.create(:medication, userid: recipient.id) }
7-
let!(:reminder) { FactoryGirl.create(:take_medication_reminder, medication_id: medication.id) }
8-
let(:mail) { NotificationMailer.take_medication(reminder) }
9-
10-
it "sends a reminder to a user" do
11-
expect(mail.to).to eq(["[email protected]"])
12-
expect(mail.subject).to eq("Don't forget to take Fancy Medication Name!")
13-
end
9+
subject(:email) { NotificationMailer.take_medication(reminder) }
10+
11+
it { expect(email.to).to eq(["[email protected]"]) }
12+
it { expect(email.subject).to eq("Don't forget to take Fancy Medication Name!") }
1413
end
1514

1615
describe "#refill_medication" do
17-
let(:recipient) { FactoryGirl.create(:user1, email: "[email protected]") }
18-
let!(:medication) { FactoryGirl.create(:medication, userid: recipient.id) }
19-
let!(:reminder) { FactoryGirl.create(:take_medication_reminder, medication_id: medication.id) }
20-
let(:mail) { NotificationMailer.refill_medication(reminder) }
21-
22-
it "sends a reminder to a user" do
23-
expect(mail.to).to eq(["[email protected]"])
24-
expect(mail.subject).to eq("Your refill for Fancy Medication Name is coming up soon!")
16+
subject(:email) { NotificationMailer.refill_medication(reminder) }
17+
18+
it { expect(email.to).to eq(["[email protected]"]) }
19+
it { expect(email.subject).to eq("Your refill for Fancy Medication Name is coming up soon!") }
20+
end
21+
22+
describe 'notification' do
23+
let(:who_triggered_event) { FactoryGirl.create(:user2) }
24+
25+
let(:data) do
26+
JSON.generate(user: who_triggered_event.name,
27+
userid: who_triggered_event.id,
28+
uid: who_triggered_event.uid,
29+
type: type,
30+
uniqueid: 'some_unique_id')
31+
end
32+
33+
context 'when type is accepted_ally_request' do
34+
let(:type) { 'accepted_ally_request' }
35+
36+
subject(:email) { NotificationMailer.notification_email(recipient, data) }
37+
38+
it { expect(email.subject).to eq("if me | #{who_triggered_event.name} accepted your ally request!") }
39+
it { expect(email.body.encoded).to match("<p>Congrats! You can now share Moments, Strategies, and more with #{who_triggered_event.name}.</p>") }
40+
end
41+
42+
context 'when type is new_ally_request' do
43+
let(:type) { 'new_ally_request' }
44+
45+
subject(:email) { NotificationMailer.notification_email(recipient, data) }
46+
47+
it { expect(email.subject).to eq("if me | #{who_triggered_event.name} sent an ally request!") }
48+
it { expect(email.body.encoded).to match("<p>Please <a href=\"http://localhost:3000/allies\">sign in</a> to accept or reject the request!</p>") }
2549
end
2650
end
2751
end

0 commit comments

Comments
 (0)