diff options
-rw-r--r-- | Capfile | 1 | ||||
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 4 | ||||
-rw-r--r-- | app/mailers/vote_mailer.rb | 7 | ||||
-rw-r--r-- | app/models/quote.rb | 4 | ||||
-rw-r--r-- | app/views/vote_mailer/daily_report_email.html.haml | 8 | ||||
-rw-r--r-- | app/views/vote_mailer/daily_report_email.text.erb | 5 | ||||
-rw-r--r-- | config/schedule.rb | 23 | ||||
-rw-r--r-- | lib/tasks/tasks.rake | 10 | ||||
-rw-r--r-- | test/mailers/previews/vote_mailer_preview.rb | 11 | ||||
-rw-r--r-- | test/mailers/vote_mailer_test.rb | 12 |
11 files changed, 86 insertions, 0 deletions
diff --git a/Capfile b/Capfile index 1ace4ce..8915892 100644 --- a/Capfile +++ b/Capfile | |||
@@ -14,6 +14,7 @@ require "capistrano/bundler" | |||
14 | require "capistrano/rails/assets" | 14 | require "capistrano/rails/assets" |
15 | require "capistrano/rails/migrations" | 15 | require "capistrano/rails/migrations" |
16 | require "capistrano/passenger" | 16 | require "capistrano/passenger" |
17 | require "whenever/capistrano" | ||
17 | 18 | ||
18 | # Load custom tasks from `lib/capistrano/tasks` if you have any defined | 19 | # Load custom tasks from `lib/capistrano/tasks` if you have any defined |
19 | Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } | 20 | Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } |
diff --git a/Gemfile b/Gemfile index de04f85..d74e090 100644 --- a/Gemfile +++ b/Gemfile | |||
@@ -85,3 +85,4 @@ gem 'active_storage_validations' | |||
85 | gem "image_processing", ">= 1.2" | 85 | gem "image_processing", ">= 1.2" |
86 | gem "meta-tags" | 86 | gem "meta-tags" |
87 | gem 'rails_autolink' | 87 | gem 'rails_autolink' |
88 | gem 'whenever', "~> 1.0.0", require: false | ||
diff --git a/Gemfile.lock b/Gemfile.lock index ce12201..2980acc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock | |||
@@ -135,6 +135,7 @@ GEM | |||
135 | rack (>= 1.0.0) | 135 | rack (>= 1.0.0) |
136 | rack-test (>= 0.5.4) | 136 | rack-test (>= 0.5.4) |
137 | xpath (>= 2.0, < 4.0) | 137 | xpath (>= 2.0, < 4.0) |
138 | chronic (0.10.2) | ||
138 | climate_control (0.2.0) | 139 | climate_control (0.2.0) |
139 | coffee-rails (4.2.2) | 140 | coffee-rails (4.2.2) |
140 | coffee-script (>= 2.2.0) | 141 | coffee-script (>= 2.2.0) |
@@ -364,6 +365,8 @@ GEM | |||
364 | websocket-driver (0.7.6) | 365 | websocket-driver (0.7.6) |
365 | websocket-extensions (>= 0.1.0) | 366 | websocket-extensions (>= 0.1.0) |
366 | websocket-extensions (0.1.5) | 367 | websocket-extensions (0.1.5) |
368 | whenever (1.0.0) | ||
369 | chronic (>= 0.6.3) | ||
367 | will_paginate (4.0.0) | 370 | will_paginate (4.0.0) |
368 | xpath (3.2.0) | 371 | xpath (3.2.0) |
369 | nokogiri (~> 1.8) | 372 | nokogiri (~> 1.8) |
@@ -418,6 +421,7 @@ DEPENDENCIES | |||
418 | tzinfo-data | 421 | tzinfo-data |
419 | web-console (>= 3.3.0) | 422 | web-console (>= 3.3.0) |
420 | webrick (~> 1.7) | 423 | webrick (~> 1.7) |
424 | whenever (~> 1.0.0) | ||
421 | will_paginate (~> 4.0) | 425 | will_paginate (~> 4.0) |
422 | 426 | ||
423 | BUNDLED WITH | 427 | BUNDLED WITH |
diff --git a/app/mailers/vote_mailer.rb b/app/mailers/vote_mailer.rb new file mode 100644 index 0000000..77ac3d1 --- /dev/null +++ b/app/mailers/vote_mailer.rb | |||
@@ -0,0 +1,7 @@ | |||
1 | class VoteMailer < ApplicationMailer | ||
2 | def daily_report_email | ||
3 | @entries = params[:votes].group_by { |v| "#{v.votable_type}_#{v.votable_id}" }.values | ||
4 | # weird way of getting the admin's email | ||
5 | mail to: User.first.email, subject: "[Four Island] You have upvotes!" | ||
6 | end | ||
7 | end | ||
diff --git a/app/models/quote.rb b/app/models/quote.rb index 518d53b..3301667 100644 --- a/app/models/quote.rb +++ b/app/models/quote.rb | |||
@@ -17,6 +17,10 @@ class Quote < ApplicationRecord | |||
17 | scope :published, -> { where(state: :published) } | 17 | scope :published, -> { where(state: :published) } |
18 | scope :pending, -> { where(state: :pending) } | 18 | scope :pending, -> { where(state: :pending) } |
19 | 19 | ||
20 | def title | ||
21 | "Quote \##{id}" | ||
22 | end | ||
23 | |||
20 | def published_date | 24 | def published_date |
21 | created_at.strftime("%B %d %Y at %I:%M:%S") + created_at.strftime(" %p").downcase + created_at.strftime(" %Z") | 25 | created_at.strftime("%B %d %Y at %I:%M:%S") + created_at.strftime(" %p").downcase + created_at.strftime(" %Z") |
22 | end | 26 | end |
diff --git a/app/views/vote_mailer/daily_report_email.html.haml b/app/views/vote_mailer/daily_report_email.html.haml new file mode 100644 index 0000000..ab54df2 --- /dev/null +++ b/app/views/vote_mailer/daily_report_email.html.haml | |||
@@ -0,0 +1,8 @@ | |||
1 | %p | ||
2 | Users have upvoted content on Four Island in the last day! | ||
3 | %ul | ||
4 | - @entries.each do |entry| | ||
5 | %li | ||
6 | %strong= pluralize(entry.size, "person") | ||
7 | left an upvote on | ||
8 | = link_to entry.first.votable.title, entry.first.votable, style: "font-weight: bold; font-style: italic" | ||
diff --git a/app/views/vote_mailer/daily_report_email.text.erb b/app/views/vote_mailer/daily_report_email.text.erb new file mode 100644 index 0000000..22ea368 --- /dev/null +++ b/app/views/vote_mailer/daily_report_email.text.erb | |||
@@ -0,0 +1,5 @@ | |||
1 | Users have upvoted content on Four Island in the last day! | ||
2 | |||
3 | <% @entries.each do |entry| %> | ||
4 | - <%= pluralize(entry.size, "person") %> left an upvote on <%= entry.first.votable.title %> (<%= url_for(entry.first.votable) %>) | ||
5 | <% end %> | ||
diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 0000000..e53ebf4 --- /dev/null +++ b/config/schedule.rb | |||
@@ -0,0 +1,23 @@ | |||
1 | # Use this file to easily define all of your cron jobs. | ||
2 | # | ||
3 | # It's helpful, but not entirely necessary to understand cron before proceeding. | ||
4 | # http://en.wikipedia.org/wiki/Cron | ||
5 | |||
6 | # Example: | ||
7 | # | ||
8 | # set :output, "/path/to/my/cron_log.log" | ||
9 | # | ||
10 | # every 2.hours do | ||
11 | # command "/usr/bin/some_great_command" | ||
12 | # runner "MyModel.some_method" | ||
13 | # rake "some:great:rake:task" | ||
14 | # end | ||
15 | # | ||
16 | # every 4.days do | ||
17 | # runner "AnotherModel.prune_old_records" | ||
18 | # end | ||
19 | |||
20 | # Learn more: http://github.com/javan/whenever | ||
21 | every 1.day, at: "6:00am" do | ||
22 | rake "thoughts:email_upvote_report" | ||
23 | end | ||
diff --git a/lib/tasks/tasks.rake b/lib/tasks/tasks.rake new file mode 100644 index 0000000..fee22f4 --- /dev/null +++ b/lib/tasks/tasks.rake | |||
@@ -0,0 +1,10 @@ | |||
1 | namespace :thoughts do | ||
2 | desc "Email a review of the last day's upvotes" | ||
3 | task :email_upvote_report => :environment do | ||
4 | votes = Vote.where("created_at > ?", 1.day.ago).where(upvote: 1).all | ||
5 | |||
6 | unless votes.empty? | ||
7 | VoteMailer.with(votes: votes).daily_report_email.deliver | ||
8 | end | ||
9 | end | ||
10 | end | ||
diff --git a/test/mailers/previews/vote_mailer_preview.rb b/test/mailers/previews/vote_mailer_preview.rb new file mode 100644 index 0000000..dfc5da1 --- /dev/null +++ b/test/mailers/previews/vote_mailer_preview.rb | |||
@@ -0,0 +1,11 @@ | |||
1 | # Preview all emails at http://localhost:3000/rails/mailers/vote_mailer | ||
2 | class VoteMailerPreview < ActionMailer::Preview | ||
3 | |||
4 | # Preview this email at http://localhost:3000/rails/mailers/vote_mailer/daily_report_email | ||
5 | def daily_report_email | ||
6 | # Duplicating logic from the task... love it | ||
7 | votes = Vote.where("created_at > ?", 1.day.ago).where(upvote: 1).all | ||
8 | VoteMailer.with(votes: votes).daily_report_email | ||
9 | end | ||
10 | |||
11 | end | ||
diff --git a/test/mailers/vote_mailer_test.rb b/test/mailers/vote_mailer_test.rb new file mode 100644 index 0000000..efe8236 --- /dev/null +++ b/test/mailers/vote_mailer_test.rb | |||
@@ -0,0 +1,12 @@ | |||
1 | require "test_helper" | ||
2 | |||
3 | class VoteMailerTest < ActionMailer::TestCase | ||
4 | test "daily_report_email" do | ||
5 | mail = VoteMailer.daily_report_email | ||
6 | assert_equal "Daily report email", mail.subject | ||
7 | assert_equal ["to@example.org"], mail.to | ||
8 | assert_equal ["from@example.com"], mail.from | ||
9 | assert_match "Hi", mail.body.encoded | ||
10 | end | ||
11 | |||
12 | end | ||