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

Feature/membership reminderemails #923

Merged
merged 5 commits into from Jan 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 19 additions & 7 deletions Gemfile
Expand Up @@ -24,6 +24,10 @@ gem 'roadie', '~> 2.4.3' # enables rails' layouts for emails
gem 'ransack' # search
gem 'rollbar'
gem 'will_paginate', '~> 3.1.0' # pagination
# Background Tasks
# gem 'sidekiq'
# gem 'sidekiq-scheduler'
# gem 'sidekiq-rollbar'

# Cache
gem 'redis'
Expand Down Expand Up @@ -62,38 +66,46 @@ gem 'uglifier' # for heroku asset precompilation. :-(
#########
# Templating
#########
gem 'render_anywhere', require: false
gem 'slim-rails'
gem 'arbre'

group :development, :development_public, :development_remote, :test do
group :development do
# list the fields in models
gem 'annotate'
# capturing emails
gem 'letter_opener_web'
end

group :development, :test do
# pretty printing of objects (for debugging)
gem 'awesome_print'
# debugging! `binding.pry` to initiate!
gem 'pry-byebug'
# pretty logs!
gem 'formatted_rails_logger'
# managing and traversing time in specs
gem 'delorean'
# fast web server
gem 'puma'
# linting
gem 'rubocop'
# list the fields in models
gem 'annotate'
# capturing emails
gem 'letter_opener_web'

# hopefully eventually used in AMS...
gem 'case_transform'
# gem 'case_transform-rust-extensions', require: 'case_transform'
end

group :test do
# managing and traversing time in specs
gem 'delorean'

# Mimicking objects
gem 'metahash-rb', require: 'metahash'

# The test runner
gem 'rspec'
gem 'rspec-rails'
gem 'fuubar'
gem 'rspec-sidekiq'

gem 'factory_girl_rails'
gem 'factory_girl', github: 'thoughtbot/factory_girl'
Expand Down
88 changes: 53 additions & 35 deletions Gemfile.lock
Expand Up @@ -14,21 +14,21 @@ GIT
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.2.6)
actionpack (= 4.2.6)
actionview (= 4.2.6)
activejob (= 4.2.6)
actionmailer (4.2.7.1)
actionpack (= 4.2.7.1)
actionview (= 4.2.7.1)
activejob (= 4.2.7.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.6)
actionview (= 4.2.6)
activesupport (= 4.2.6)
actionpack (4.2.7.1)
actionview (= 4.2.7.1)
activesupport (= 4.2.7.1)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.6)
activesupport (= 4.2.6)
actionview (4.2.7.1)
activesupport (= 4.2.7.1)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
Expand All @@ -37,17 +37,17 @@ GEM
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
jsonapi (= 0.1.1.beta2)
activejob (4.2.6)
activesupport (= 4.2.6)
activejob (4.2.7.1)
activesupport (= 4.2.7.1)
globalid (>= 0.3.0)
activemodel (4.2.6)
activesupport (= 4.2.6)
activemodel (4.2.7.1)
activesupport (= 4.2.7.1)
builder (~> 3.1)
activerecord (4.2.6)
activemodel (= 4.2.6)
activesupport (= 4.2.6)
activerecord (4.2.7.1)
activemodel (= 4.2.7.1)
activesupport (= 4.2.7.1)
arel (~> 6.0)
activesupport (4.2.6)
activesupport (4.2.7.1)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
Expand All @@ -58,7 +58,9 @@ GEM
annotate (2.7.1)
activerecord (>= 3.2, < 6.0)
rake (>= 10.4, < 12.0)
arel (6.0.3)
arbre (1.1.1)
activesupport (>= 3.0.0)
arel (6.0.4)
ast (2.3.0)
attr_encrypted (1.3.3)
encryptor (>= 1.3.0)
Expand All @@ -83,7 +85,8 @@ GEM
codeclimate-test-reporter (1.0.3)
simplecov
coderay (1.1.1)
concurrent-ruby (1.0.2)
concurrent-ruby (1.0.4)
connection_pool (2.2.1)
css_parser (1.3.6)
addressable
dante (0.2.0)
Expand Down Expand Up @@ -163,7 +166,7 @@ GEM
multi_json (1.12.1)
netrc (0.11.0)
newrelic_rpm (3.11.2.286)
nokogiri (1.6.8.1)
nokogiri (1.7.0.1)
mini_portile2 (~> 2.1.0)
oj (2.17.5)
oj_mimic_json (1.0.1)
Expand Down Expand Up @@ -193,30 +196,32 @@ GEM
puma (3.6.2)
rack (1.6.5)
rack-cors (0.4.0)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
rails (4.2.6)
actionmailer (= 4.2.6)
actionpack (= 4.2.6)
actionview (= 4.2.6)
activejob (= 4.2.6)
activemodel (= 4.2.6)
activerecord (= 4.2.6)
activesupport (= 4.2.6)
rails (4.2.7.1)
actionmailer (= 4.2.7.1)
actionpack (= 4.2.7.1)
actionview (= 4.2.7.1)
activejob (= 4.2.7.1)
activemodel (= 4.2.7.1)
activerecord (= 4.2.7.1)
activesupport (= 4.2.7.1)
bundler (>= 1.3.0, < 2.0)
railties (= 4.2.6)
railties (= 4.2.7.1)
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7)
rails-dom-testing (1.0.8)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
nokogiri (~> 1.6)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (4.2.6)
actionpack (= 4.2.6)
activesupport (= 4.2.6)
railties (4.2.7.1)
actionpack (= 4.2.7.1)
activesupport (= 4.2.7.1)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (2.1.0)
Expand All @@ -228,6 +233,8 @@ GEM
i18n
polyamorous (~> 1.3)
redis (3.3.2)
render_anywhere (0.0.12)
rails (>= 3.0.7)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rest-client (1.8.0)
Expand Down Expand Up @@ -261,6 +268,9 @@ GEM
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-sidekiq (2.2.0)
rspec (~> 3.0, >= 3.0.0)
sidekiq (>= 2.4.0)
rspec-support (3.5.0)
rubocop (0.45.0)
parser (>= 2.3.1.1, < 3.0)
Expand All @@ -276,6 +286,11 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
sidekiq (4.2.7)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (~> 3.2, >= 3.2.1)
simplecov (0.12.0)
docile (~> 1.1.0)
json (>= 1.8, < 3)
Expand All @@ -291,7 +306,7 @@ GEM
railties (>= 3.1)
slim (~> 3.0)
slop (3.6.0)
sprockets (3.7.0)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (2.3.3)
Expand Down Expand Up @@ -327,6 +342,7 @@ DEPENDENCIES
active_model_serializers
annotate
api_cache!
arbre
attr_encrypted
awesome_print
aws-sdk
Expand Down Expand Up @@ -360,10 +376,12 @@ DEPENDENCIES
rails (~> 4.2)
ransack
redis
render_anywhere
roadie (~> 2.4.3)
rollbar
rspec
rspec-rails
rspec-sidekiq
rubocop
sass (~> 3.3.14)
sass-rails
Expand Down
3 changes: 3 additions & 0 deletions app/jobs/application_job.rb
@@ -0,0 +1,3 @@
class ApplicationJob < ActiveJob::Base

end
30 changes: 30 additions & 0 deletions app/jobs/membership_reminder_job.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true
#
# run through all memberships, and see if they
# have a week before their membership ends.
# This should run nightly.
class MembershipReminderJob < ApplicationJob
ONE_WEEK = 1.week.freeze

def perform
now = Time.now

Organization.all.each do |org|
org.members.each do |member|
renewal = member.latest_active_renewal(org)

next unless renewal
next unless should_send_email(renewal, now)

MembershipMailer.one_week_expiration(member, renewal, org).deliver_now
end
end
end

def should_send_email(renewal, now)
expires_at = renewal.expires_at
when_to_remind = expires_at - ONE_WEEK

when_to_remind < now
end
end
21 changes: 20 additions & 1 deletion app/mailers/application_mailer.rb
@@ -1,4 +1,23 @@
# frozen_string_literal: true
require 'render_anywhere'
class ApplicationMailer < ActionMailer::Base
default from: APPLICATION_CONFIG['support_email']
layout "email"

layout 'email'

def arbre(env: {})
raise 'No Block Given' unless block_given?

RenderAnywhere::RenderingController.new.render_to_string(
inline: yield, layout: 'email', type: :arbre, locals: env
)
end

def slim(env: {})
raise 'No Block Given' unless block_given?

RenderAnywhere::RenderingController.new.render_to_string(
inline: yield, layout: 'email', type: :slim, locals: env
)
end
end
33 changes: 33 additions & 0 deletions app/mailers/membership_mailer.rb
@@ -0,0 +1,33 @@
# frozen_string_literal: true
class MembershipMailer < ApplicationMailer
# only called from the membership_reminder_job,
# so this should not be called with deliver later.
def one_week_expiration(member, renewal, org)
template_environment = {
org: org,
member: member,
renewal: renewal
}

template = slim(env: template_environment) do
%q(
= content_for :header
= org.name

h3 Hello, #{member.name},

| Your membership for #{org.name} is set to expire at #{renewal.expires_at.to_s(:short)}.
br
| Please visit <a href=#{org.url}>#{org.url}</a> to purchase a membership renewal.
br
br
)
end

subject = "Your membership for #{org.name} is going to expire soon"

mail(to: member.email, subject: subject) do |format|
format.html { render text: template }
end
end
end
9 changes: 4 additions & 5 deletions app/mailers/order_mailer.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class OrderMailer < ApplicationMailer
def receipt(for_order: nil)
@order = for_order
Expand All @@ -20,16 +21,14 @@ def receipt(for_order: nil)
if (org_email = @host.try(:notify_email)).present?
all_purchases_notification = @host.try(:email_all_purchases)
membership_notification = (
@order.has_membership? and @host.try(:email_membership_purchases)
@order.has_membership? && @host.try(:email_membership_purchases)
)

bcc_emails = org_email.split(/[,;]/).map(&:strip)

options[:bcc] = [org_email] if all_purchases_notification or membership_notification
options[:bcc] = [org_email] if all_purchases_notification || membership_notification
end

if to_email.present?
mail(options)
end
mail(options) if to_email.present?
end
end
3 changes: 3 additions & 0 deletions config/application.rb
Expand Up @@ -29,6 +29,9 @@ class Application < Rails::Application
Kernel.const_set(config_name.to_s, config)
end

config.active_job.queue_adapter = :sidekiq


# Upgrading from rails 4.1.x to 4.2.x
# Currently, Active Record suppresses errors raised within
# `after_rollback`/`after_commit` callbacks and only print them to the logs.
Expand Down