Commit 9fef020f6be6ef0b468451b953c9f5f1dc156ee7
1 parent
331869b0
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Email template for change password notification
Showing
6 changed files
with
24 additions
and
9 deletions
Show diff stats
app/mailers/task_mailer.rb
1 | 1 | class TaskMailer < ActionMailer::Base |
2 | 2 | |
3 | + include EmailTemplateHelper | |
4 | + | |
3 | 5 | def target_notification(task, message) |
4 | 6 | @message = extract_message(message) |
5 | 7 | @target = task.target.name |
... | ... | @@ -33,15 +35,13 @@ class TaskMailer < ActionMailer::Base |
33 | 35 | @requestor = task.requestor.name |
34 | 36 | @environment = task.requestor.environment.name |
35 | 37 | @url = url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') |
36 | - @email_template = task.email_template | |
37 | - template_params = {:environment => task.requestor.environment, :task => task} | |
38 | 38 | |
39 | - mail( | |
39 | + mail_with_template( | |
40 | 40 | to: task.requestor.notification_emails, |
41 | 41 | from: self.class.generate_from(task), |
42 | - subject: @email_template.present? ? @email_template.parsed_subject(template_params) : '[%s] %s' % [task.requestor.environment.name, task.target_notification_description], | |
43 | - body: @email_template.present? ? @email_template.parsed_body(template_params) : nil, | |
44 | - content_type: @email_template.present? ? "text/html" : nil | |
42 | + subject: '[%s] %s' % [task.requestor.environment.name, task.target_notification_description], | |
43 | + email_template: task.email_template, | |
44 | + template_params: {:environment => task.requestor.environment, :task => task, :message => @message, :url => @url, :requestor => task.requestor} | |
45 | 45 | ) |
46 | 46 | end |
47 | 47 | ... | ... |
app/models/change_password.rb
... | ... | @@ -26,6 +26,13 @@ class ChangePassword < Task |
26 | 26 | validates_presence_of :password_confirmation, :on => :update, :if => lambda { |change| change.status != Task::Status::CANCELLED } |
27 | 27 | validates_confirmation_of :password, :if => lambda { |change| change.status != Task::Status::CANCELLED } |
28 | 28 | |
29 | + before_save :set_email_template | |
30 | + | |
31 | + def set_email_template | |
32 | + template = environment.email_templates.find_by_template_type(:user_change_password) | |
33 | + data[:email_template_id] = template.id unless template.nil? | |
34 | + end | |
35 | + | |
29 | 36 | def environment |
30 | 37 | requestor.environment unless requestor.nil? |
31 | 38 | end | ... | ... |
app/models/email_template.rb
... | ... | @@ -23,7 +23,8 @@ class EmailTemplate < ActiveRecord::Base |
23 | 23 | :task_rejection => {:description => _('Task Rejection'), :owner_type => Profile}, |
24 | 24 | :task_acceptance => {:description => _('Task Acceptance'), :owner_type => Profile}, |
25 | 25 | :organization_members => {:description => _('Organization Members'), :owner_type => Profile}, |
26 | - :user_activation => {:description => _('User Activation'), :unique => true, :owner_type => Environment} | |
26 | + :user_activation => {:description => _('User Activation'), :unique => true, :owner_type => Environment}, | |
27 | + :user_change_password => {:description => _('Change User Password'), :unique => true, :owner_type => Environment} | |
27 | 28 | } |
28 | 29 | end |
29 | 30 | ... | ... |
app/models/task.rb
... | ... | @@ -245,7 +245,8 @@ class Task < ActiveRecord::Base |
245 | 245 | def to_liquid |
246 | 246 | HashWithIndifferentAccess.new({ |
247 | 247 | :requestor => requestor, |
248 | - :reject_explanation => reject_explanation | |
248 | + :reject_explanation => reject_explanation, | |
249 | + :code => code | |
249 | 250 | }) |
250 | 251 | end |
251 | 252 | ... | ... |
test/unit/change_password_test.rb
... | ... | @@ -71,4 +71,10 @@ class ChangePasswordTest < ActiveSupport::TestCase |
71 | 71 | assert_match(/#{task.requestor.name} wants to change its password/, email.subject) |
72 | 72 | end |
73 | 73 | |
74 | + should 'set email template when it exists' do | |
75 | + template = EmailTemplate.create!(:template_type => :user_change_password, :name => 'template1', :owner => Environment.default) | |
76 | + task = ChangePassword.create!(:requestor => person) | |
77 | + assert_equal template.id, task.email_template_id | |
78 | + end | |
79 | + | |
74 | 80 | end | ... | ... |
test/unit/email_template_test.rb
... | ... | @@ -42,7 +42,7 @@ class EmailTemplateTest < ActiveSupport::TestCase |
42 | 42 | |
43 | 43 | should 'return available types when the owner is an environment' do |
44 | 44 | template = EmailTemplate.new(:owner => Environment.default) |
45 | - assert_equal [:user_activation], template.available_types.symbolize_keys.keys | |
45 | + assert_equal [:user_activation, :user_change_password], template.available_types.symbolize_keys.keys | |
46 | 46 | end |
47 | 47 | |
48 | 48 | should 'return available types when the owner is a profile' do | ... | ... |