Skip to content
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
wants to merge 9 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions app/controllers/email_notifications_controller.rb
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
18 changes: 18 additions & 0 deletions app/mailers/challenge_mailer.rb
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|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Това ще изпрати имейл и на админите. Трябва да го променим.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На второ четене, не мисля, че е проблем. Аз бих искал да получавам имейл, за да съм сигурен, че нещата работят. А и всеки admin може да си го изключи при нужда. Така е добре.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, и аз с тази идея не го лимитирах.

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
18 changes: 18 additions & 0 deletions app/mailers/task_mailer.rb
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
5 changes: 5 additions & 0 deletions app/views/challenge_mailer/new_challenge_for_user.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Здравей, <%= @user_name %>!

Публикувано е ново предизвикателство - <%= @challenge_name %>. Срокът за предаване на решения е до <%= l @challenge_end_date %>.

Можеш да прочетеш условието тук: <%= @challenge_url %>
7 changes: 7 additions & 0 deletions app/views/challenges/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
= admin_only do
- unless ChallengeCheckWorker.queued? @challenge.id
= link_to 'Пусни тестовете', challenge_check_path(@challenge), method: :create, class: :action
= admin_only do
- unless @challenge.hidden?
= link_to 'Извести студентите по имейл',
send_emails_challenge_path(@challenge),
method: :create,
class: :action,
data: {confirm: 'Сигурен ли си, че искаш да изпратиш писма до всички за това предизвикателство?'}

- if ChallengeCheckWorker.queued? @challenge.id
%p
Expand Down
2 changes: 2 additions & 0 deletions app/views/profiles/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
%fieldset
%legend Известия по поща
= form.input :comment_notification
= form.input :task_notification
= form.input :challenge_notification

%fieldset
%legend Промяна на парола
Expand Down
5 changes: 5 additions & 0 deletions app/views/task_mailer/new_task_for_user.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Здравей, <%= @user_name %>!

Публикувана е нова задача - <%= @task_name %>. Срокът за предаване на решения е до <%= l @task_end_date %>.

Можеш да прочетеш условието тук: <%= @task_url %>
7 changes: 7 additions & 0 deletions app/views/tasks/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
= link_to 'Предай решение', task_my_solution_path(@task), class: :action
- if @current_user_solution
= link_to 'Коментари към моето решение', @current_user_solution, class: :action
= admin_only do
- unless @task.hidden?
= link_to 'Извести студентите по имейл',
send_emails_task_path(@task),
method: :create,
class: :action,
data: {confirm: 'Сигурен ли си, че искаш да изпратиш писма до всички за тази задача?'}

- if admin? or @task.closed?
%p= link_to 'Предадени решения', task_solutions_path(@task)
Expand Down
4 changes: 3 additions & 1 deletion config/locales/bg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ bg:
phone: Мобилен телефон
site: Сайт
about: Разкажете ни за себе си
comment_notification: Искам да получавам писма за коментари по решенията ми
comment_notification: Искам да получавам имейл за коментари по решенията ми
task_notification: Искам да получавам имейл за нови задачи
challenge_notification: Искам да получавам имейл за нови предизвикателства
sign_up:
full_name: Три имена
faculty_number: Факултетен номер
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
end
resource :my_solution, only: %w(show update)
resource :check, controller: :task_checks, only: :create

post :send_emails, on: :member, to: 'email_notifications#new_task'
end

resources :challenges, except: :destroy do
resource :my_solution, only: %w(show update), controller: :my_challenge_solutions
resource :check, controller: :challenge_checks, only: :create

post :send_emails, on: :member, to: 'email_notifications#new_challenge'
end

resources :revisions, only: [] do
Expand Down
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
42 changes: 42 additions & 0 deletions spec/controllers/email_notifications_controller_spec.rb
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
39 changes: 39 additions & 0 deletions spec/mailers/challenge_mailer_spec.rb
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
39 changes: 39 additions & 0 deletions spec/mailers/task_mailer_spec.rb
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 spec/support/shared_examples/user_notificaiton_mailer_examples.rb
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