Commit 0d9bdbdf94f66591b8bc35d9dd9ade08f7890f6a

Authored by Victor Costa
1 parent bff21134

Select task rejection email from template

app/controllers/my_profile/tasks_controller.rb
... ... @@ -4,6 +4,8 @@ class TasksController < MyProfileController
4 4 protect :perform_task, :profile, :except => [:index]
5 5  
6 6 def index
  7 + @email_templates = profile.email_templates.find_all_by_template_type(:task_rejection)
  8 +
7 9 @filter_type = params[:filter_type].presence
8 10 @filter_text = params[:filter_text].presence
9 11 @filter_responsible = params[:filter_responsible]
... ...
app/mailers/task_mailer.rb
... ... @@ -33,11 +33,14 @@ class TaskMailer < ActionMailer::Base
33 33 @requestor = task.requestor.name
34 34 @environment = task.requestor.environment.name
35 35 @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}
36 38  
37 39 mail(
38 40 to: task.requestor.notification_emails,
39 41 from: self.class.generate_from(task),
40   - subject: '[%s] %s' % [task.requestor.environment.name, task.target_notification_description]
  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
41 44 )
42 45 end
43 46  
... ...
app/models/task.rb
... ... @@ -41,6 +41,8 @@ class Task < ActiveRecord::Base
41 41  
42 42 attr_protected :status
43 43  
  44 + settings_items :email_template_id, :type => :integer
  45 +
44 46 def initialize(*args)
45 47 super
46 48 self.status = (args.first ? args.first[:status] : nil) || Task::Status::ACTIVE
... ... @@ -236,6 +238,17 @@ class Task < ActiveRecord::Base
236 238 end
237 239 end
238 240  
  241 + def email_template
  242 + @email_template ||= email_template_id.present? ? EmailTemplate.find_by_id(email_template_id) : nil
  243 + end
  244 +
  245 + def to_liquid
  246 + HashWithIndifferentAccess.new({
  247 + :requestor => requestor,
  248 + :reject_explanation => reject_explanation
  249 + })
  250 + end
  251 +
239 252 scope :pending, :conditions => { :status => Task::Status::ACTIVE }
240 253 scope :hidden, :conditions => { :status => Task::Status::HIDDEN }
241 254 scope :finished, :conditions => { :status => Task::Status::FINISHED }
... ...
app/views/tasks/_task_reject_details.html.erb
  1 +<% if @email_templates.present? %>
  2 + <div class="template-selection">
  3 + <%= labelled_form_field(_('Select a rejection email template:'), select_tag("tasks[#{task.id}][task][email_template_id]", options_from_collection_for_select(@email_templates, :id, :name), :include_blank => true, 'data-url' => url_for(:controller => 'email_templates', :action => 'show_parsed'))) %>
  4 + </div>
  5 +<% end %>
  6 +
1 7 <%= labelled_form_field(_('Rejection explanation'), f.text_area(:reject_explanation, :rows => 5)) %>
... ...
test/unit/task_mailer_test.rb
... ... @@ -166,6 +166,34 @@ class TaskMailerTest &lt; ActiveSupport::TestCase
166 166 assert_match(/#{url_to_compare}/, mail.body.to_s)
167 167 end
168 168  
  169 + should 'be able to send rejection notification based on a selected template' do
  170 + task = Task.new
  171 + task.expects(:task_cancelled_message).returns('the message')
  172 + task.reject_explanation = 'explanation'
  173 +
  174 + profile = fast_create(Community)
  175 + email_template = EmailTemplate.create!(:owner => profile, :name => 'Template 1', :subject => 'template subject - {{environment.name}}', :body => 'template body - {{environment.name}} - {{task.requestor.name}} - {{task.reject_explanation}}')
  176 + task.email_template_id = email_template.id
  177 +
  178 + requestor = Profile.new(:name => 'my name')
  179 + requestor.expects(:notification_emails).returns(['requestor@example.com']).at_least_once
  180 +
  181 + environment = Environment.default
  182 + environment.expects(:noreply_email).returns('sender@example.com')
  183 + environment.expects(:default_hostname).returns('example.com')
  184 + environment.expects(:name).returns('example').at_least_once
  185 +
  186 + task.expects(:requestor).returns(requestor).at_least_once
  187 + requestor.expects(:environment).returns(environment).at_least_once
  188 + task.expects(:environment).returns(environment).at_least_once
  189 +
  190 + task.send(:send_notification, :cancelled).deliver
  191 + assert !ActionMailer::Base.deliveries.empty?
  192 + mail = ActionMailer::Base.deliveries.last
  193 + assert_equal 'template subject - example', mail.subject.to_s
  194 + assert_equal 'template body - example - my name - explanation', mail.body.to_s
  195 + end
  196 +
169 197 private
170 198 def read_fixture(action)
171 199 IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}")
... ...