Commit fc9326eed40b0e1c0c6efaf3a6dfe24a047c53a2
1 parent
ba00654d
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Send activation code with email template
Showing
7 changed files
with
85 additions
and
2 deletions
Show diff stats
@@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
1 | +module EmailTemplateHelper | ||
2 | + | ||
3 | + def mail_with_template(params={}) | ||
4 | + params[:body] = params[:email_template].present? ? params[:email_template].parsed_body(params[:template_params]) : params[:body] | ||
5 | + params[:subject] = params[:email_template].present? ? params[:email_template].parsed_subject(params[:template_params]) : params[:subject] | ||
6 | + mail(params.except(:email_template)) | ||
7 | + end | ||
8 | + | ||
9 | +end |
app/mailers/user_mailer.rb
1 | class UserMailer < ActionMailer::Base | 1 | class UserMailer < ActionMailer::Base |
2 | + | ||
3 | + include EmailTemplateHelper | ||
4 | + | ||
2 | def activation_email_notify(user) | 5 | def activation_email_notify(user) |
3 | user_email = "#{user.login}@#{user.email_domain}" | 6 | user_email = "#{user.login}@#{user.email_domain}" |
4 | @name = user.name | 7 | @name = user.name |
@@ -22,10 +25,12 @@ class UserMailer < ActionMailer::Base | @@ -22,10 +25,12 @@ class UserMailer < ActionMailer::Base | ||
22 | @redirection = (true if user.return_to) | 25 | @redirection = (true if user.return_to) |
23 | @join = (user.community_to_join if user.community_to_join) | 26 | @join = (user.community_to_join if user.community_to_join) |
24 | 27 | ||
25 | - mail( | 28 | + mail_with_template( |
26 | from: "#{user.environment.name} <#{user.environment.contact_email}>", | 29 | from: "#{user.environment.name} <#{user.environment.contact_email}>", |
27 | to: user.email, | 30 | to: user.email, |
28 | subject: _("[%s] Activate your account") % [user.environment.name], | 31 | subject: _("[%s] Activate your account") % [user.environment.name], |
32 | + template_params: {:environment => user.environment, :activation_code => @activation_code, :redirection => @redirection, :join => @join}, | ||
33 | + email_template: user.environment.email_templates.find_by_template_type(:user_activation), | ||
29 | ) | 34 | ) |
30 | end | 35 | end |
31 | 36 |
app/models/email_template.rb
@@ -6,6 +6,10 @@ class EmailTemplate < ActiveRecord::Base | @@ -6,6 +6,10 @@ class EmailTemplate < ActiveRecord::Base | ||
6 | 6 | ||
7 | validates_presence_of :name | 7 | validates_presence_of :name |
8 | 8 | ||
9 | + validates :name, uniqueness: { scope: [:owner_type, :owner_id] } | ||
10 | + | ||
11 | + validates :template_type, uniqueness: { scope: [:owner_type, :owner_id] }, if: :unique_by_type? | ||
12 | + | ||
9 | def parsed_body(params) | 13 | def parsed_body(params) |
10 | @parsed_body ||= parse(body, params) | 14 | @parsed_body ||= parse(body, params) |
11 | end | 15 | end |
@@ -18,10 +22,15 @@ class EmailTemplate < ActiveRecord::Base | @@ -18,10 +22,15 @@ class EmailTemplate < ActiveRecord::Base | ||
18 | HashWithIndifferentAccess.new ({ | 22 | HashWithIndifferentAccess.new ({ |
19 | :task_rejection => {:description => _('Task Rejection')}, | 23 | :task_rejection => {:description => _('Task Rejection')}, |
20 | :task_acceptance => {:description => _('Task Acceptance')}, | 24 | :task_acceptance => {:description => _('Task Acceptance')}, |
21 | - :organization_members => {:description => _('Organization Members')} | 25 | + :organization_members => {:description => _('Organization Members')}, |
26 | + :user_activation => {:description => _('User Activation'), :unique => true} | ||
22 | }) | 27 | }) |
23 | end | 28 | end |
24 | 29 | ||
30 | + def unique_by_type? | ||
31 | + available_types.fetch(template_type, {})[:unique] | ||
32 | + end | ||
33 | + | ||
25 | protected | 34 | protected |
26 | 35 | ||
27 | def parse(source, params) | 36 | def parse(source, params) |
app/models/environment.rb
@@ -21,6 +21,7 @@ class Environment < ActiveRecord::Base | @@ -21,6 +21,7 @@ class Environment < ActiveRecord::Base | ||
21 | 21 | ||
22 | has_many :tasks, :dependent => :destroy, :as => 'target' | 22 | has_many :tasks, :dependent => :destroy, :as => 'target' |
23 | has_many :search_terms, :as => :context | 23 | has_many :search_terms, :as => :context |
24 | + has_many :email_templates, :foreign_key => :owner_id | ||
24 | 25 | ||
25 | IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/ | 26 | IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/ |
26 | 27 |
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +require_relative "../test_helper" | ||
2 | + | ||
3 | +class EmailTemplateHelperTest < ActionView::TestCase | ||
4 | + | ||
5 | + should 'replace body and subject with parsed values from template' do | ||
6 | + template = mock | ||
7 | + template.expects(:parsed_body).returns('parsed body') | ||
8 | + template.expects(:parsed_subject).returns('parsed subject') | ||
9 | + params = {:subject => 'subject', :body => 'body', :email_template => template} | ||
10 | + expects(:mail).with({:subject => 'parsed subject', :body => 'parsed body'}) | ||
11 | + mail_with_template(params) | ||
12 | + end | ||
13 | + | ||
14 | + should 'do not change params if there is no email template' do | ||
15 | + params = {:subject => 'subject', :body => 'body'} | ||
16 | + expects(:mail).with(params) | ||
17 | + mail_with_template(params) | ||
18 | + end | ||
19 | + | ||
20 | +end |
test/unit/email_template_test.rb
@@ -19,4 +19,25 @@ class EmailTemplateTest < ActiveSupport::TestCase | @@ -19,4 +19,25 @@ class EmailTemplateTest < ActiveSupport::TestCase | ||
19 | assert_equal 'Hi John', template.parsed_subject({:person => 'John'}) | 19 | assert_equal 'Hi John', template.parsed_subject({:person => 'John'}) |
20 | end | 20 | end |
21 | 21 | ||
22 | + should 'not create template with the same name of other' do | ||
23 | + template1 = EmailTemplate.new(:template_type => :type1, :name => 'template') | ||
24 | + template2 = EmailTemplate.new(:template_type => :type1, :name => 'template') | ||
25 | + assert template1.save | ||
26 | + assert !template2.save | ||
27 | + end | ||
28 | + | ||
29 | + should 'not create duplicated template when template type is unique' do | ||
30 | + template1 = EmailTemplate.new(:template_type => :user_activation, :name => 'template1') | ||
31 | + template2 = EmailTemplate.new(:template_type => :user_activation, :name => 'template2') | ||
32 | + assert template1.save | ||
33 | + assert !template2.save | ||
34 | + end | ||
35 | + | ||
36 | + should 'create duplicated template when template type is not unique' do | ||
37 | + template1 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template1') | ||
38 | + template2 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template2') | ||
39 | + assert template1.save | ||
40 | + assert template2.save | ||
41 | + end | ||
42 | + | ||
22 | end | 43 | end |
test/unit/user_mailer_test.rb
@@ -26,6 +26,24 @@ fast_create(Person)) | @@ -26,6 +26,24 @@ fast_create(Person)) | ||
26 | assert_match /profile\/some-user\/friends\/suggest/, email.body.to_s | 26 | assert_match /profile\/some-user\/friends\/suggest/, email.body.to_s |
27 | end | 27 | end |
28 | 28 | ||
29 | + should 'deliver activation code email' do | ||
30 | + assert_difference 'ActionMailer::Base.deliveries.size' do | ||
31 | + u = create_user('some-user') | ||
32 | + UserMailer.activation_code(u).deliver | ||
33 | + end | ||
34 | + end | ||
35 | + | ||
36 | + should 'deliver activation code email with template' do | ||
37 | + EmailTemplate.create!(:template_type => :user_activation, :name => 'template1', :subject => 'activation template subject', :body => 'activation template body', :owner => Environment.default) | ||
38 | + assert_difference 'ActionMailer::Base.deliveries.size' do | ||
39 | + u = create_user('some-user') | ||
40 | + UserMailer.activation_code(u).deliver | ||
41 | + end | ||
42 | + mail = ActionMailer::Base.deliveries.last | ||
43 | + assert_equal 'activation template subject', mail.subject.to_s | ||
44 | + assert_equal 'activation template body', mail.body.to_s | ||
45 | + end | ||
46 | + | ||
29 | private | 47 | private |
30 | 48 | ||
31 | def read_fixture(action) | 49 | def read_fixture(action) |