Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add email notifications for tasks and challenges
- Loading branch information
1 parent
7d36321
commit 20a284f
Showing
14 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class ChallengeMailer < ActionMailer::Base | ||
default from: Language.email_sender, reply_to: Language.email | ||
|
||
def new_challenge(challenge) | ||
User.where(challenge_notification: true).each do |user| | ||
ChallengeMailer.delay.new_challenge_for_user(challenge, user) | ||
end | ||
end | ||
|
||
def new_challenge_for_user(challenge, user) | ||
@user_name = user.first_name | ||
@challenge_name = challenge.name | ||
@challenge_url = challenge_url(challenge) | ||
@challenge_end_date = challenge.closes_at.strftime('%d.%m.%Y %H:%M') | ||
|
||
mail to: user.email, | ||
subject: "Ново предизвикателство - #{challenge.name}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class TaskMailer < ActionMailer::Base | ||
default from: Language.email_sender, reply_to: Language.email | ||
|
||
def new_task(task) | ||
User.where(task_notification: true).each do |user| | ||
TaskMailer.delay.new_task_for_user(task, user) | ||
end | ||
end | ||
|
||
def new_task_for_user(task, user) | ||
@user_name = user.first_name | ||
@task_name = task.name | ||
@task_url = task_url(task) | ||
@task_end_date = task.closes_at.strftime('%d.%m.%Y %H:%M') | ||
|
||
mail to: user.email, | ||
subject: "Нова задача - #{task.name}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class ChallengeObserver < ActiveRecord::Observer | ||
def after_create(challenge) | ||
ChallengeMailer.new_challenge challenge unless challenge.hidden or challenge.checked | ||
end | ||
|
||
def after_update(challenge) | ||
if challenge.hidden_changed? and not challenge.hidden and not challenge.checked | ||
ChallengeMailer.new_challenge challenge | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class TaskObserver < ActiveRecord::Observer | ||
def after_create(task) | ||
TaskMailer.new_task task unless task.hidden or task.checked | ||
end | ||
|
||
def after_update(task) | ||
if task.hidden_changed? and not task.hidden and not task.checked | ||
TaskMailer.new_task task | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Здравей, <%= @user_name %>! | ||
|
||
Публикувано е ново предизвикателство - <%= @challenge_name %>. Срокът за предаване на решения е до <%= @challenge_end_date %>. | ||
|
||
Можеш да прочетеш условието на следният адрес: <%= @challenge_url %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Здравей, <%= @user_name %>! | ||
|
||
Публикувана е нова задача - <%= @task_name %>. Срокът за предаване на решения е до <%= @task_end_date %>. | ||
|
||
Можеш да прочетеш условието на следният адрес: <%= @task_url %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
db/migrate/20140922181427_add_challenge_and_task_notifications_to_user.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddChallengeAndTaskNotificationsToUser < ActiveRecord::Migration | ||
def change | ||
add_column :users, :challenge_notification, :boolean, default: true, null: false | ||
add_column :users, :task_notification, :boolean, default: true, null: false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'spec_helper' | ||
|
||
describe ChallengeMailer do | ||
let(:challenge) { double 'challenge' } | ||
|
||
describe '#new_challenge' do | ||
let(:mailer_delay) { double 'ChallengeMailer.delay' } | ||
|
||
before do | ||
ChallengeMailer.stub(:delay) { mailer_delay } | ||
end | ||
|
||
it 'sends multiple emails' do | ||
users = [ | ||
create(:user, challenge_notification: true), | ||
create(:user, challenge_notification: true), | ||
] | ||
|
||
users.each do |user| | ||
mailer_delay.should_receive(:new_challenge_for_user).with(challenge, user) | ||
end | ||
|
||
ChallengeMailer.new_challenge challenge | ||
end | ||
|
||
it 'does not send emails to unsubscribed users' do | ||
create(:user, challenge_notification: false) | ||
users = [ | ||
create(:user, challenge_notification: true), | ||
create(:user, challenge_notification: true), | ||
] | ||
|
||
users.each do |user| | ||
mailer_delay.should_receive(:new_challenge_for_user).with(challenge, user) | ||
end | ||
|
||
ChallengeMailer.new_challenge challenge | ||
end | ||
end | ||
|
||
describe '#new_challenge_for_user' do | ||
subject { ChallengeMailer.new_challenge_for_user(challenge, user) } | ||
|
||
let(:user) { double 'user' } | ||
let(:time) { Time.now } | ||
|
||
before do | ||
user.stub first_name: 'John', | ||
email: 'john@doe.com' | ||
|
||
challenge.stub name: 'Challenge name', | ||
closes_at: time | ||
end | ||
|
||
it { should have_subject 'Ново предизвикателство - Challenge name' } | ||
it { should deliver_to 'john@doe.com' } | ||
it { should have_body_text 'John' } | ||
it { should have_body_text 'Challenge name' } | ||
it { should have_body_text challenge_url(challenge) } | ||
it { should have_body_text time.strftime('%d.%m.%Y %H:%M') } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'spec_helper' | ||
|
||
describe TaskMailer do | ||
let(:task) { double 'task' } | ||
|
||
describe '#new_task' do | ||
let(:mailer_delay) { double 'TaskMailer.delay' } | ||
|
||
before do | ||
TaskMailer.stub(:delay) { mailer_delay } | ||
end | ||
|
||
it 'sends multiple emails' do | ||
users = [ | ||
create(:user, task_notification: true), | ||
create(:user, task_notification: true), | ||
] | ||
|
||
users.each do |user| | ||
mailer_delay.should_receive(:new_task_for_user).with(task, user) | ||
end | ||
|
||
TaskMailer.new_task task | ||
end | ||
|
||
it 'does not send emails to unsubscribed users' do | ||
create(:user, task_notification: false) | ||
users = [ | ||
create(:user, task_notification: true), | ||
create(:user, task_notification: true), | ||
] | ||
|
||
users.each do |user| | ||
mailer_delay.should_receive(:new_task_for_user).with(task, user) | ||
end | ||
|
||
TaskMailer.new_task task | ||
end | ||
end | ||
|
||
describe '#new_task_for_user' do | ||
subject { TaskMailer.new_task_for_user(task, user) } | ||
|
||
let(:user) { double 'user' } | ||
let(:time) { Time.now } | ||
|
||
before do | ||
user.stub first_name: 'John', | ||
email: 'john@doe.com' | ||
|
||
task.stub name: 'Task name', | ||
closes_at: time | ||
end | ||
|
||
it { should have_subject 'Нова задача - Task name' } | ||
it { should deliver_to 'john@doe.com' } | ||
it { should have_body_text 'John' } | ||
it { should have_body_text 'Task name' } | ||
it { should have_body_text task_url(task) } | ||
it { should have_body_text time.strftime('%d.%m.%Y %H:%M') } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'spec_helper' | ||
|
||
describe ChallengeObserver do | ||
describe '#after_create' do | ||
it "notifies users via email" do | ||
challenge = build :challenge, hidden: false, checked: false | ||
ChallengeMailer.should_receive(:new_challenge).with(challenge) | ||
challenge.save! | ||
end | ||
|
||
it "does not notify users if the challenge is hidden" do | ||
challenge = build :challenge, hidden: true, checked: false | ||
ChallengeMailer.should_not_receive(:new_challenge) | ||
challenge.save! | ||
end | ||
|
||
it "does not notify users if the challenge is checked" do | ||
challenge = build :challenge, hidden: false, checked: true | ||
ChallengeMailer.should_not_receive(:new_challenge) | ||
challenge.save! | ||
end | ||
end | ||
|
||
describe '#after_update' do | ||
it "notifies users if #hidden changes from true to false" do | ||
challenge = create :challenge, hidden: true | ||
challenge.hidden = false | ||
|
||
ChallengeMailer.should_receive(:new_challenge).with(challenge) | ||
|
||
challenge.save! | ||
end | ||
|
||
it "does not notify users if the hidden attribute does not change" do | ||
challenge = create :challenge, hidden: false | ||
challenge.name = 'test' | ||
|
||
ChallengeMailer.should_not_receive(:new_challenge) | ||
|
||
challenge.save! | ||
end | ||
|
||
it "does not notify users if the challenge is checked" do | ||
challenge = create :challenge, hidden: true | ||
challenge.hidden = false | ||
challenge.checked = true | ||
|
||
ChallengeMailer.should_not_receive(:new_challenge) | ||
|
||
challenge.save! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'spec_helper' | ||
|
||
describe TaskObserver do | ||
describe '#after_create' do | ||
it "notifies users via email" do | ||
task = build :task, hidden: false, checked: false | ||
TaskMailer.should_receive(:new_task).with(task) | ||
task.save! | ||
end | ||
|
||
it "does not notify users if the task is hidden" do | ||
task = build :task, hidden: true, checked: false | ||
TaskMailer.should_not_receive(:new_task) | ||
task.save! | ||
end | ||
|
||
it "does not notify users if the task is checked" do | ||
task = build :task, hidden: false, checked: true | ||
TaskMailer.should_not_receive(:new_task) | ||
task.save! | ||
end | ||
end | ||
|
||
describe '#after_update' do | ||
it "notifies users if #hidden changes from true to false" do | ||
task = create :task, hidden: true, checked: false | ||
task.hidden = false | ||
|
||
TaskMailer.should_receive(:new_task).with(task) | ||
|
||
task.save! | ||
end | ||
|
||
it "does not notify users if the hidden attribute does not change" do | ||
task = create :task, hidden: false, checked: false | ||
task.name = 'test' | ||
|
||
TaskMailer.should_not_receive(:new_task) | ||
|
||
task.save! | ||
end | ||
|
||
it "does not notify users if the task is checked" do | ||
task = create :task, hidden: true, checked: false | ||
task.hidden = false | ||
task.checked = true | ||
|
||
TaskMailer.should_not_receive(:new_task) | ||
|
||
task.save! | ||
end | ||
end | ||
end |