-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add email notifications for tasks and challenges #148
Open
georgyangelov
wants to merge
9
commits into
skanev:master
Choose a base branch
from
georgyangelov:email-notifications
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
20a284f
Add email notifications for tasks and challenges
georgyangelov d000fbb
Use the locale default time instead of #strftime
georgyangelov 4663204
Remove challenge and task observers for emails
georgyangelov e64dd3c
Add email send buttons to challenges and tasks
georgyangelov 2f4a97c
Use shared examples for challenge and task mailers
georgyangelov 9183dfa
Split long lines to multiple ones
georgyangelov fdbcdea
Small style and notice text change
georgyangelov 259bd04
Move some tests out of the shared ones for the mailers
georgyangelov 1329330
Small style changes and reword some of the texts
georgyangelov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 @@ | ||
class EmailNotificationsController < ApplicationController | ||
before_action :require_admin | ||
|
||
def new_challenge | ||
challenge = Challenge.find params[:id] | ||
ChallengeMailer.new_challenge challenge unless challenge.hidden? | ||
|
||
redirect_to challenge_path(challenge), notice: 'Засилихме писмата' | ||
end | ||
|
||
def new_task | ||
task = Task.find params[:id] | ||
TaskMailer.new_task task unless task.hidden? | ||
|
||
redirect_to task_path(task), notice: 'Засилихме писмата' | ||
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,18 @@ | ||
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 | ||
|
||
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,18 @@ | ||
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 | ||
|
||
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,5 @@ | ||
Здравей, <%= @user_name %>! | ||
|
||
Публикувано е ново предизвикателство - <%= @challenge_name %>. Срокът за предаване на решения е до <%= l @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
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 %>. Срокът за предаване на решения е до <%= l @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
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,42 @@ | ||
require 'spec_helper' | ||
|
||
shared_examples_for 'an email sender' do |action, model, mailer| | ||
log_in_as :admin | ||
|
||
it 'denies access to non-admins' do | ||
current_user.stub admin?: false | ||
post action, id: '1' | ||
response.should deny_access | ||
end | ||
|
||
it 'sends emails' do | ||
challenge = create model, hidden: false, id: 1 | ||
|
||
mailer.should_receive(action).with(challenge) | ||
post action, id: '1' | ||
end | ||
|
||
it "does not schedule the emails if the #{model} is hidden" do | ||
hidden_challenge = create model, hidden: true, id: 1 | ||
|
||
mailer.should_not_receive(action) | ||
post action, id: '1' | ||
end | ||
|
||
it "redirects to the #{model}" do | ||
create model, hidden: false, id: 1 | ||
|
||
post action, id: '1' | ||
controller.should redirect_to send("#{model}_path", '1') | ||
end | ||
end | ||
|
||
describe EmailNotificationsController do | ||
describe "POST new_challenge" do | ||
it_behaves_like 'an email sender', :new_challenge, :challenge, ChallengeMailer | ||
end | ||
|
||
describe "POST new_task" do | ||
it_behaves_like 'an email sender', :new_task, :task, TaskMailer | ||
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,39 @@ | ||
require 'spec_helper' | ||
|
||
describe ChallengeMailer do | ||
describe '#new_challenge' do | ||
it_behaves_like 'a user notification mailer', | ||
ChallengeMailer, | ||
:new_challenge, | ||
:new_challenge_for_user, | ||
:challenge_notification | ||
end | ||
|
||
describe "#new_challenge_for_user" do | ||
subject { ChallengeMailer.new_challenge_for_user challenge, user } | ||
|
||
let(:user) { double 'user' } | ||
let(:time) { Time.now } | ||
|
||
let(:challenge) { double 'challenge' } | ||
|
||
before do | ||
user.stub({ | ||
first_name: 'John', | ||
email: '[email protected]' | ||
}) | ||
|
||
challenge.stub({ | ||
name: 'Name', | ||
closes_at: time | ||
}) | ||
end | ||
|
||
it { should have_subject 'Ново предизвикателство - Name' } | ||
it { should deliver_to '[email protected]' } | ||
it { should have_body_text 'John' } | ||
it { should have_body_text 'Name' } | ||
it { should have_body_text challenge_url(challenge) } | ||
it { should have_body_text I18n.l(time) } | ||
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,39 @@ | ||
require 'spec_helper' | ||
|
||
describe TaskMailer do | ||
describe '#new_task' do | ||
it_behaves_like 'a user notification mailer', | ||
TaskMailer, | ||
:new_task, | ||
:new_task_for_user, | ||
:task_notification | ||
end | ||
|
||
describe "#new_task_for_user" do | ||
subject { TaskMailer.new_task_for_user task, user } | ||
|
||
let(:user) { double 'user' } | ||
let(:time) { Time.now } | ||
|
||
let(:task) { double 'task' } | ||
|
||
before do | ||
user.stub({ | ||
first_name: 'John', | ||
email: '[email protected]' | ||
}) | ||
|
||
task.stub({ | ||
name: 'Name', | ||
closes_at: time | ||
}) | ||
end | ||
|
||
it { should have_subject 'Нова задача - Name' } | ||
it { should deliver_to '[email protected]' } | ||
it { should have_body_text 'John' } | ||
it { should have_body_text 'Name' } | ||
it { should have_body_text task_url(task) } | ||
it { should have_body_text I18n.l(time) } | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
spec/support/shared_examples/user_notificaiton_mailer_examples.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,27 @@ | ||
shared_examples_for 'a user notification mailer' do |mailer, method, email_method, user_filter| | ||
let(:mailer_delay) { double "#{mailer}.delay" } | ||
let(:model_instance) { double 'model' } | ||
|
||
before do | ||
mailer.stub(:delay) { mailer_delay } | ||
end | ||
|
||
it 'sends multiple emails' do | ||
first_user = create :user, user_filter => true | ||
second_user = create :user, user_filter => true | ||
|
||
mailer_delay.should_receive(email_method).with(model_instance, first_user) | ||
mailer_delay.should_receive(email_method).with(model_instance, second_user) | ||
|
||
mailer.public_send method, model_instance | ||
end | ||
|
||
it 'does not send emails to unsubscribed users' do | ||
create :user, user_filter => false | ||
user = create :user, user_filter => true | ||
|
||
mailer_delay.should_receive(email_method).with(model_instance, user) | ||
|
||
mailer.public_send method, model_instance | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Това ще изпрати имейл и на админите. Трябва да го променим.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
На второ четене, не мисля, че е проблем. Аз бих искал да получавам имейл, за да съм сигурен, че нещата работят. А и всеки admin може да си го изключи при нужда. Така е добре.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, и аз с тази идея не го лимитирах.