mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Adding an enterprise welcome email
This commit is contained in:
@@ -4,6 +4,12 @@ class EnterpriseMailer < Spree::BaseMailer
|
||||
|
||||
layout 'mailer'
|
||||
|
||||
def welcome(enterprise)
|
||||
@enterprise = enterprise
|
||||
mail(:to => enterprise.email, :from => from_address,
|
||||
:subject => "#{enterprise.name} is now on #{Spree::Config[:site_name]}")
|
||||
end
|
||||
|
||||
def confirmation_instructions(record, token, opts={})
|
||||
@token = token
|
||||
find_enterprise(record)
|
||||
|
||||
@@ -55,12 +55,16 @@ class Enterprise < ActiveRecord::Base
|
||||
validate :enforce_ownership_limit, if: lambda { owner_id_changed? && !owner_id.nil? }
|
||||
validates_length_of :description, :maximum => 255
|
||||
|
||||
before_save :email_check, if: lambda{ email_changed? }
|
||||
before_save :confirmation_check, if: lambda{ email_changed? }
|
||||
|
||||
before_validation :ensure_owner_is_manager, if: lambda { owner_id_changed? && !owner_id.nil? }
|
||||
before_validation :set_unused_address_fields
|
||||
after_validation :geocode_address
|
||||
|
||||
# TODO: Later versions of devise have a dedicated after_confirmation callback, so use that
|
||||
after_update :welcome_after_confirm, if: lambda { confirmation_token_changed? && confirmation_token.nil? }
|
||||
after_create :send_welcome_email, if: lambda { email_is_known? }
|
||||
|
||||
scope :by_name, order('name')
|
||||
scope :visible, where(:visible => true)
|
||||
scope :confirmed, where('confirmed_at IS NOT NULL')
|
||||
@@ -299,13 +303,29 @@ class Enterprise < ActiveRecord::Base
|
||||
|
||||
private
|
||||
|
||||
def email_check
|
||||
def email_is_known?
|
||||
owner.enterprises.confirmed.map(&:email).include?(email)
|
||||
end
|
||||
|
||||
def confirmation_check
|
||||
# Skip confirmation/reconfirmation if the new email has already been confirmed
|
||||
if owner.enterprises.confirmed.map(&:email).include?(email)
|
||||
if email_is_known?
|
||||
new_record? ? skip_confirmation! : skip_reconfirmation!
|
||||
end
|
||||
end
|
||||
|
||||
def welcome_after_confirm
|
||||
# Send welcome email if we are confirming a newly created enterprise
|
||||
# Note: this callback only runs on email confirmation
|
||||
if confirmed? && unconfirmed_email.nil? && !unconfirmed_email_changed?
|
||||
send_welcome_email
|
||||
end
|
||||
end
|
||||
|
||||
def send_welcome_email
|
||||
EnterpriseMailer.welcome(self).deliver
|
||||
end
|
||||
|
||||
def strip_url(url)
|
||||
url.andand.sub(/(https?:\/\/)?/, '')
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
%h3{:style => "margin: 0;padding: 0;font-family: \"HelveticaNeue-Light\", \"Helvetica Neue Light\", \"Helvetica Neue\", Helvetica, Arial, \"Lucida Grande\", sans-serif;line-height: 1.1;margin-bottom: 15px;color: #000;font-weight: 500;font-size: 27px;"}
|
||||
= "Welcome, #{@resource.contact}!"
|
||||
= "Hi, #{@resource.contact}!"
|
||||
%p.lead{:style => "margin: 0;padding: 0;font-family: \"Helvetica Neue\", \"Helvetica\", Helvetica, Arial, sans-serif;margin-bottom: 10px;font-weight: normal;font-size: 17px;line-height: 1.6;"}
|
||||
= "Please confirm email address for your enterprise "
|
||||
%strong
|
||||
|
||||
25
app/views/enterprise_mailer/welcome.html.haml
Normal file
25
app/views/enterprise_mailer/welcome.html.haml
Normal file
@@ -0,0 +1,25 @@
|
||||
%h3{:style => "margin: 0;padding: 0;font-family: \"HelveticaNeue-Light\", \"Helvetica Neue Light\", \"Helvetica Neue\", Helvetica, Arial, \"Lucida Grande\", sans-serif;line-height: 1.1;margin-bottom: 15px;color: #000;font-weight: 500;font-size: 27px;"}
|
||||
= "Welcome, #{@enterprise.contact}!"
|
||||
%p.lead{:style => "margin: 0;padding: 0;font-family: \"Helvetica Neue\", \"Helvetica\", Helvetica, Arial, sans-serif;margin-bottom: 10px;font-weight: normal;font-size: 17px;line-height: 1.6;"}
|
||||
%strong
|
||||
= "#{@enterprise.name}"
|
||||
= " is now on #{Spree::Config[:site_name]}."
|
||||
|
||||
%p
|
||||
|
||||
/ Callout Panel
|
||||
%p.callout{:style => "margin: 0; padding: 15px;font-family: \"Helvetica Neue\", \"Helvetica\", Helvetica, Arial, sans-serif;margin-bottom: 15px;font-weight: normal;font-size: 14px;line-height: 1.6;background-color: #e1f0f5;"}
|
||||
= "Click the link below to update your profile or manage other details for "
|
||||
%strong
|
||||
= "#{@enterprise.name}."
|
||||
%br
|
||||
%strong
|
||||
= link_to "Manage #{@enterprise.name} »", spree.admin_url
|
||||
|
||||
%p
|
||||
|
||||
%p{:style => "margin: 0;padding: 0;font-family: \"Helvetica Neue\", \"Helvetica\", Helvetica, Arial, sans-serif;margin-bottom: 10px;font-weight: normal;font-size: 14px;line-height: 1.6;"}
|
||||
We're so excited that you're joining the Open Food Network! Don't hestitate to get in touch if you have any questions.
|
||||
%p
|
||||
|
||||
= render 'shared/mailers/social_and_contact'
|
||||
@@ -13,4 +13,11 @@ describe EnterpriseMailer do
|
||||
mail = ActionMailer::Base.deliveries.first
|
||||
expect(mail.subject).to eq "Please confirm your email for #{enterprise.name}"
|
||||
end
|
||||
|
||||
it "should send a welcome email when given an enterprise" do
|
||||
EnterpriseMailer.welcome(enterprise).deliver
|
||||
ActionMailer::Base.deliveries.count.should == 1
|
||||
mail = ActionMailer::Base.deliveries.first
|
||||
expect(mail.subject).to eq "#{enterprise.name} is now on #{Spree::Config[:site_name]}"
|
||||
end
|
||||
end
|
||||
@@ -8,16 +8,32 @@ describe Enterprise do
|
||||
let!(:user) { create_enterprise_user( enterprise_limit: 2 ) }
|
||||
let!(:enterprise) { create(:enterprise, owner: user) }
|
||||
|
||||
it "when the email address has not already been confirmed" do
|
||||
mail_message = double "Mail::Message"
|
||||
expect(EnterpriseMailer).to receive(:confirmation_instructions).and_return mail_message
|
||||
mail_message.should_receive :deliver
|
||||
create(:enterprise, owner: user, email: "unknown@email.com", confirmed_at: nil )
|
||||
context "when the email address has not already been confirmed" do
|
||||
it "sends a confirmation email" do
|
||||
mail_message = double "Mail::Message"
|
||||
expect(EnterpriseMailer).to receive(:confirmation_instructions).and_return mail_message
|
||||
mail_message.should_receive :deliver
|
||||
create(:enterprise, owner: user, email: "unknown@email.com", confirmed_at: nil )
|
||||
end
|
||||
|
||||
it "does not send a welcome email" do
|
||||
expect(EnterpriseMailer).to_not receive(:welcome)
|
||||
create(:enterprise, owner: user, email: "unknown@email.com", confirmed_at: nil )
|
||||
end
|
||||
end
|
||||
|
||||
it "when the email address has already been confirmed" do
|
||||
expect(EnterpriseMailer).to_not receive(:confirmation_instructions)
|
||||
e = create(:enterprise, owner: user, email: enterprise.email, confirmed_at: nil)
|
||||
context "when the email address has already been confirmed" do
|
||||
it "does not send a confirmation email" do
|
||||
expect(EnterpriseMailer).to_not receive(:confirmation_instructions)
|
||||
create(:enterprise, owner: user, email: enterprise.email, confirmed_at: nil)
|
||||
end
|
||||
|
||||
it "sends a welcome email" do
|
||||
mail_message = double "Mail::Message"
|
||||
expect(EnterpriseMailer).to receive(:welcome).and_return mail_message
|
||||
mail_message.should_receive :deliver
|
||||
create(:enterprise, owner: user, email: enterprise.email, confirmed_at: nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,6 +54,35 @@ describe Enterprise do
|
||||
enterprise.update_attributes!(email: "second.known.email@email.com")
|
||||
end
|
||||
end
|
||||
|
||||
describe "on email confirmation" do
|
||||
let!(:user) { create_enterprise_user( enterprise_limit: 2 ) }
|
||||
let!(:unconfirmed_enterprise) { create(:enterprise, owner: user, confirmed_at: nil) }
|
||||
|
||||
context "when we are confirming an email address for the first time for the enterprise" do
|
||||
it "sends a welcome email" do
|
||||
# unconfirmed_email is blank if we are not reconfirming an email
|
||||
unconfirmed_enterprise.unconfirmed_email = nil
|
||||
unconfirmed_enterprise.save!
|
||||
|
||||
mail_message = double "Mail::Message"
|
||||
expect(EnterpriseMailer).to receive(:welcome).and_return mail_message
|
||||
mail_message.should_receive :deliver
|
||||
unconfirmed_enterprise.confirm!
|
||||
end
|
||||
end
|
||||
|
||||
context "when we are reconfirming the email address for the enterprise" do
|
||||
it "does not send a welcome email" do
|
||||
# unconfirmed_email is present if we are reconfirming an email
|
||||
unconfirmed_enterprise.unconfirmed_email = "unconfirmed@email.com"
|
||||
unconfirmed_enterprise.save!
|
||||
|
||||
expect(EnterpriseMailer).to_not receive(:welcome)
|
||||
unconfirmed_enterprise.confirm!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "associations" do
|
||||
|
||||
Reference in New Issue
Block a user