Skip to content

Commit

Permalink
Add Exercise-15 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
v-kolesnikov committed Aug 21, 2015
1 parent 1144484 commit a820f1e
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -37,6 +37,8 @@ gem "aws-sdk-v1"
gem "aws-sdk"
# Levenshtein distance between two byte strings
gem "damerau-levenshtein"
# Cron jobs
gem "whenever", require: false

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger cli
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -57,6 +57,7 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
chronic (0.10.2)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.7)
Expand Down Expand Up @@ -296,6 +297,8 @@ GEM
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)
whenever (0.9.4)
chronic (>= 0.6.3)
xpath (2.0.0)
nokogiri (~> 1.3)

Expand Down Expand Up @@ -333,6 +336,7 @@ DEPENDENCIES
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)
whenever

BUNDLED WITH
1.10.6
4 changes: 4 additions & 0 deletions app/mailers/application_mailer.rb
@@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "Flashcards"
layout "mailer"
end
7 changes: 7 additions & 0 deletions app/mailers/notifications_mailer.rb
@@ -0,0 +1,7 @@
class NotificationsMailer < ApplicationMailer
def pending_cards(user)
@user = user
@cards = user.cards_for_review
mail(to: @user.email, subject: "Cards review")
end
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Expand Up @@ -16,4 +16,10 @@ def cards
def cards_for_review
default_deck ? default_deck.cards.for_review : cards.for_review
end

def self.notify_review
User.all.each do |user|
NotificationsMailer.pending_cards(user).deliver_later
end
end
end
5 changes: 5 additions & 0 deletions app/views/layouts/mailer.html.erb
@@ -0,0 +1,5 @@
<html>
<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.erb
@@ -0,0 +1 @@
<%= yield %>
18 changes: 18 additions & 0 deletions app/views/notifications_mailer/pending_cards.html.erb
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hello, <%= @user.email %> </h1>
<p>
The following cards are expected review:
<% @cards.each do |card| %>
<p> <%= card.translated_text %> </p>
<% end %>
</p>
<p>
To login to the site, just follow this link: <%= ENV["APP_HOST"] %>
</p>
</body>
</html>
17 changes: 15 additions & 2 deletions config/application.rb
@@ -1,6 +1,6 @@
require File.expand_path('../boot', __FILE__)
require File.expand_path("../boot", __FILE__)

require 'rails/all'
require "rails/all"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand All @@ -16,6 +16,19 @@ class Application < Rails::Application
storage: "filesystem"
}

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
authentication: :plain,
user_name: ENV["SMTP_USER"],
password: ENV["SMTP_PASSWORD"],
enable_starttls_auto: true
}
config.action_mailer.default_url_options = {
host: ENV["APP_HOST"]
}

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
Expand Down
24 changes: 24 additions & 0 deletions config/schedule.rb
@@ -0,0 +1,24 @@
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron

# Example:
#
# set :output, "/path/to/my/cron_log.log"
#
# every 2.hours do
# command "/usr/bin/some_great_command"
# runner "MyModel.some_method"
# rake "some:great:rake:task"
# end
#
# every 4.days do
# runner "AnotherModel.prune_old_records"
# end

# Learn more: http://github.com/javan/whenever

every 1.day, at: "8:00 am" do
runner "User.notify_review"
end
6 changes: 6 additions & 0 deletions lib/tasks/scheduler.rake
@@ -0,0 +1,6 @@
desc "Heroku scheduler tasks"
task notify_review: :environment do
puts "Sending out email reminders for review."
User.notify_review
puts "Emails sent!"
end
11 changes: 11 additions & 0 deletions spec/factories/user.rb
Expand Up @@ -12,5 +12,16 @@
create_list(:deck, evaluator.decks_count, user: user)
end
end

factory :user_with_cards do
transient do
cards_count 1
end

after(:create) do |user, evaluator|
create_list(:deck_with_cards, 1, cards_count: evaluator.cards_count,
user: user)
end
end
end
end
6 changes: 6 additions & 0 deletions spec/features/deck_spec.rb
Expand Up @@ -33,4 +33,10 @@
click_button "Update Deck"
expect(deck.user.decks.first.title).to eq "TestDeck"
end

it "remove deck" do
visit decks_path
click_link "Удалить"
expect(page).to have_content("Колоды (0)")
end
end
20 changes: 20 additions & 0 deletions spec/mailers/notifications_mailer_spec.rb
@@ -0,0 +1,20 @@
require "rails_helper"

RSpec.describe NotificationsMailer do
let!(:user) { create(:user_with_cards, cards_count: 1) }

before(:each) do
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
NotificationsMailer.pending_cards(user).deliver_now
end

it "should send an email" do
expect(ActionMailer::Base.deliveries.count).to be 1
end

after(:each) do
ActionMailer::Base.deliveries.clear
end
end
6 changes: 6 additions & 0 deletions spec/mailers/previews/notifications_mailer_preview.rb
@@ -0,0 +1,6 @@
# Preview all emails at http://localhost:3000/rails/mailers/notifications_mailer
class NotificationsMailerPreview < ActionMailer::Preview
def pending_cards
NotificationsMailer.pending_cards(User.first)
end
end

0 comments on commit a820f1e

Please sign in to comment.