diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index 78dcee3..91a18c0 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -4,6 +4,8 @@ class TasksController < MyProfileController protect :perform_task, :profile, :except => [:index] def index + @email_templates = profile.email_templates.find_all_by_template_type(:task_rejection) + @filter_type = params[:filter_type].presence @filter_text = params[:filter_text].presence @filter_responsible = params[:filter_responsible] diff --git a/app/mailers/task_mailer.rb b/app/mailers/task_mailer.rb index cc46f6d..9720c64 100644 --- a/app/mailers/task_mailer.rb +++ b/app/mailers/task_mailer.rb @@ -33,11 +33,14 @@ class TaskMailer < ActionMailer::Base @requestor = task.requestor.name @environment = task.requestor.environment.name @url = url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') + @email_template = task.email_template + template_params = {:environment => task.requestor.environment, :task => task} mail( to: task.requestor.notification_emails, from: self.class.generate_from(task), - subject: '[%s] %s' % [task.requestor.environment.name, task.target_notification_description] + subject: @email_template.present? ? @email_template.parsed_subject(template_params) : '[%s] %s' % [task.requestor.environment.name, task.target_notification_description], + body: @email_template.present? ? @email_template.parsed_body(template_params) : nil ) end diff --git a/app/models/task.rb b/app/models/task.rb index 927031d..adbc30c 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -41,6 +41,8 @@ class Task < ActiveRecord::Base attr_protected :status + settings_items :email_template_id, :type => :integer + def initialize(*args) super self.status = (args.first ? args.first[:status] : nil) || Task::Status::ACTIVE @@ -236,6 +238,17 @@ class Task < ActiveRecord::Base end end + def email_template + @email_template ||= email_template_id.present? ? EmailTemplate.find_by_id(email_template_id) : nil + end + + def to_liquid + HashWithIndifferentAccess.new({ + :requestor => requestor, + :reject_explanation => reject_explanation + }) + end + scope :pending, :conditions => { :status => Task::Status::ACTIVE } scope :hidden, :conditions => { :status => Task::Status::HIDDEN } scope :finished, :conditions => { :status => Task::Status::FINISHED } diff --git a/app/views/tasks/_task_reject_details.html.erb b/app/views/tasks/_task_reject_details.html.erb index 13bc80c..975457f 100644 --- a/app/views/tasks/_task_reject_details.html.erb +++ b/app/views/tasks/_task_reject_details.html.erb @@ -1 +1,7 @@ +<% if @email_templates.present? %> +
+ <%= 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'))) %> +
+<% end %> + <%= labelled_form_field(_('Rejection explanation'), f.text_area(:reject_explanation, :rows => 5)) %> diff --git a/test/unit/task_mailer_test.rb b/test/unit/task_mailer_test.rb index 69e4695..90ce7dc 100644 --- a/test/unit/task_mailer_test.rb +++ b/test/unit/task_mailer_test.rb @@ -166,6 +166,34 @@ class TaskMailerTest < ActiveSupport::TestCase assert_match(/#{url_to_compare}/, mail.body.to_s) end + should 'be able to send rejection notification based on a selected template' do + task = Task.new + task.expects(:task_cancelled_message).returns('the message') + task.reject_explanation = 'explanation' + + profile = fast_create(Community) + 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}}') + task.email_template_id = email_template.id + + requestor = Profile.new(:name => 'my name') + requestor.expects(:notification_emails).returns(['requestor@example.com']).at_least_once + + environment = Environment.default + environment.expects(:noreply_email).returns('sender@example.com') + environment.expects(:default_hostname).returns('example.com') + environment.expects(:name).returns('example').at_least_once + + task.expects(:requestor).returns(requestor).at_least_once + requestor.expects(:environment).returns(environment).at_least_once + task.expects(:environment).returns(environment).at_least_once + + task.send(:send_notification, :cancelled).deliver + assert !ActionMailer::Base.deliveries.empty? + mail = ActionMailer::Base.deliveries.last + assert_equal 'template subject - example', mail.subject.to_s + assert_equal 'template body - example - my name - explanation', mail.body.to_s + end + private def read_fixture(action) IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}") -- libgit2 0.21.2