Commit 8a4dc901a9dccdf8cf20ab495645e6325cdeb08f

Authored by Victor Costa
1 parent f55f3ef9

Choose email template when accept a task

app/controllers/my_profile/tasks_controller.rb
@@ -4,7 +4,8 @@ class TasksController < MyProfileController @@ -4,7 +4,8 @@ class TasksController < MyProfileController
4 protect :perform_task, :profile, :except => [:index] 4 protect :perform_task, :profile, :except => [:index]
5 5
6 def index 6 def index
7 - @email_templates = profile.email_templates.find_all_by_template_type(:task_rejection) 7 + @rejection_email_templates = profile.email_templates.find_all_by_template_type(:task_rejection)
  8 + @acceptance_email_templates = profile.email_templates.find_all_by_template_type(:task_acceptance)
8 9
9 @filter_type = params[:filter_type].presence 10 @filter_type = params[:filter_type].presence
10 @filter_text = params[:filter_text].presence 11 @filter_text = params[:filter_text].presence
app/helpers/application_helper.rb
@@ -44,6 +44,8 @@ module ApplicationHelper @@ -44,6 +44,8 @@ module ApplicationHelper
44 44
45 include PluginsHelper 45 include PluginsHelper
46 46
  47 + include TaskHelper
  48 +
47 def locale 49 def locale
48 (@page && !@page.language.blank?) ? @page.language : FastGettext.locale 50 (@page && !@page.language.blank?) ? @page.language : FastGettext.locale
49 end 51 end
app/helpers/task_helper.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +module TaskHelper
  2 +
  3 + def task_email_template(description, email_templates, task)
  4 + return '' unless email_templates.present?
  5 +
  6 + content_tag(
  7 + :div,
  8 + labelled_form_field(description, 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', :profile => profile.identifier))),
  9 + :class => 'template-selection'
  10 + )
  11 + end
  12 +
  13 +end
app/models/article.rb
@@ -792,6 +792,10 @@ class Article < ActiveRecord::Base @@ -792,6 +792,10 @@ class Article < ActiveRecord::Base
792 true 792 true
793 end 793 end
794 794
  795 + def to_liquid
  796 + HashWithIndifferentAccess.new :name => name, :abstract => abstract, :body => body, :id => id, :parent_id => parent_id, :author => author
  797 + end
  798 +
795 private 799 private
796 800
797 def sanitize_tag_list 801 def sanitize_tag_list
app/models/email_template.rb
@@ -17,6 +17,7 @@ class EmailTemplate < ActiveRecord::Base @@ -17,6 +17,7 @@ class EmailTemplate < ActiveRecord::Base
17 def available_types 17 def available_types
18 HashWithIndifferentAccess.new ({ 18 HashWithIndifferentAccess.new ({
19 :task_rejection => {:description => _('Task Rejection')}, 19 :task_rejection => {:description => _('Task Rejection')},
  20 + :task_acceptance => {:description => _('Task Acceptance')},
20 :organization_members => {:description => _('Organization Members')} 21 :organization_members => {:description => _('Organization Members')}
21 }) 22 })
22 end 23 end
app/views/tasks/_task.html.erb
@@ -52,12 +52,14 @@ @@ -52,12 +52,14 @@
52 <%= fields_for "tasks[#{task.id}][task]", task do |f| %> 52 <%= fields_for "tasks[#{task.id}][task]", task do |f| %>
53 <% if task.accept_details %> 53 <% if task.accept_details %>
54 <div id="on-accept-information-<%=task.id%>" style="display: none"> 54 <div id="on-accept-information-<%=task.id%>" style="display: none">
  55 + <%= task_email_template(_('Select an acceptance email template:'), @acceptance_email_templates, task) %>
55 <%= render :partial => partial_for_class(task.class, nil, :accept_details), :locals => {:task => task, :f => f} %> 56 <%= render :partial => partial_for_class(task.class, nil, :accept_details), :locals => {:task => task, :f => f} %>
56 </div> 57 </div>
57 <% end %> 58 <% end %>
58 59
59 <% if task.reject_details %> 60 <% if task.reject_details %>
60 <div id="on-reject-information-<%=task.id%>" style="display: none"> 61 <div id="on-reject-information-<%=task.id%>" style="display: none">
  62 + <%= task_email_template(_('Select a rejection email template:'), @rejection_email_templates, task) %>
61 <%= render :partial => partial_for_class(task.class, nil, :reject_details), :locals => {:task => task, :f => f} %> 63 <%= render :partial => partial_for_class(task.class, nil, :reject_details), :locals => {:task => task, :f => f} %>
62 </div> 64 </div>
63 <% end %> 65 <% end %>
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 -  
7 <%= labelled_form_field(_('Rejection explanation'), f.text_area(:reject_explanation, :rows => 5)) %> 1 <%= labelled_form_field(_('Rejection explanation'), f.text_area(:reject_explanation, :rows => 5)) %>
public/javascripts/tasks.js
@@ -2,16 +2,28 @@ @@ -2,16 +2,28 @@
2 2
3 $("input.task_accept_radio").click(function(){ 3 $("input.task_accept_radio").click(function(){
4 task_id = this.getAttribute("task_id"); 4 task_id = this.getAttribute("task_id");
5 - $('#on-accept-information-' + task_id).show('fast');  
6 - $('#on-reject-information-' + task_id).hide('fast'); 5 + var accept_container = $('#on-accept-information-' + task_id);
  6 + var reject_container = $('#on-reject-information-' + task_id);
  7 +
  8 + accept_container.show('fast');
  9 + reject_container.hide('fast');
7 $('#on-skip-information-' + task_id).hide('fast'); 10 $('#on-skip-information-' + task_id).hide('fast');
  11 +
  12 + reject_container.find('input, select').prop('disabled', true);
  13 + accept_container.find('input, select').prop('disabled', false);
8 }) 14 })
9 15
10 $("input.task_reject_radio").click(function(){ 16 $("input.task_reject_radio").click(function(){
11 task_id = this.getAttribute("task_id"); 17 task_id = this.getAttribute("task_id");
12 - $('#on-accept-information-' + task_id).hide('fast');  
13 - $('#on-reject-information-' + task_id).show('fast'); 18 + var accept_container = $('#on-accept-information-' + task_id);
  19 + var reject_container = $('#on-reject-information-' + task_id);
  20 +
  21 + accept_container.hide('fast');
  22 + reject_container.show('fast');
14 $('#on-skip-information-' + task_id).hide('fast'); 23 $('#on-skip-information-' + task_id).hide('fast');
  24 +
  25 + reject_container.find('input, select').prop('disabled', false);
  26 + accept_container.find('input, select').prop('disabled', true);
15 }) 27 })
16 28
17 $("input.task_skip_radio").click(function(){ 29 $("input.task_skip_radio").click(function(){
test/functional/tasks_controller_test.rb
@@ -636,4 +636,30 @@ class TasksControllerTest &lt; ActionController::TestCase @@ -636,4 +636,30 @@ class TasksControllerTest &lt; ActionController::TestCase
636 assert_equal profile, t.reload.closed_by 636 assert_equal profile, t.reload.closed_by
637 end 637 end
638 638
  639 + should "display email template selection when accept a task" do
  640 + community = fast_create(Community)
  641 + @controller.stubs(:profile).returns(community)
  642 + person = create_user_with_permission('taskviewer', 'view_tasks', community)
  643 + login_as person.user.login
  644 +
  645 + email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_acceptance)
  646 + task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person)
  647 + get :index
  648 + assert_select "#on-accept-information-#{task.id} .template-selection"
  649 + assert_equal [email_template], assigns(:acceptance_email_templates)
  650 + end
  651 +
  652 + should "display email template selection when reject a task" do
  653 + community = fast_create(Community)
  654 + @controller.stubs(:profile).returns(community)
  655 + person = create_user_with_permission('taskviewer', 'view_tasks', community)
  656 + login_as person.user.login
  657 +
  658 + email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_rejection)
  659 + task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person)
  660 + get :index
  661 + assert_select "#on-reject-information-#{task.id} .template-selection"
  662 + assert_equal [email_template], assigns(:rejection_email_templates)
  663 + end
  664 +
639 end 665 end
test/unit/task_helper_test.rb 0 → 100644
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class TaskHelperTest < ActionView::TestCase
  4 +
  5 + include ApplicationHelper
  6 +
  7 + def setup
  8 + @profile = fast_create(Profile)
  9 + @task = fast_create(Task, :target_id => @profile.id)
  10 + end
  11 +
  12 + attr_accessor :task, :profile
  13 +
  14 + should 'return select field for template selection when there is templates to choose' do
  15 + email_templates = 3.times.map { EmailTemplate.new }
  16 + assert_tag_in_string task_email_template('Description', email_templates, task), :tag => 'div', :attributes => {:class => 'template-selection'}
  17 + end
  18 +
  19 + should 'not return select field for template selection when there is no templates to choose' do
  20 + assert task_email_template('Description', [], task).blank?
  21 + end
  22 +
  23 +end
test/unit/task_mailer_test.rb
@@ -195,6 +195,34 @@ class TaskMailerTest &lt; ActiveSupport::TestCase @@ -195,6 +195,34 @@ class TaskMailerTest &lt; ActiveSupport::TestCase
195 assert_equal 'template body - example - my name - explanation', mail.body.to_s 195 assert_equal 'template body - example - my name - explanation', mail.body.to_s
196 end 196 end
197 197
  198 + should 'be able to send accept notification based on a selected template' do
  199 + task = Task.new
  200 + task.expects(:task_finished_message).returns('the message')
  201 +
  202 + profile = fast_create(Community)
  203 + email_template = EmailTemplate.create!(:owner => profile, :name => 'Template 1', :subject => 'template subject - {{environment.name}}', :body => 'template body - {{environment.name}} - {{task.requestor.name}}')
  204 + task.email_template_id = email_template.id
  205 +
  206 + requestor = Profile.new(:name => 'my name')
  207 + requestor.expects(:notification_emails).returns(['requestor@example.com']).at_least_once
  208 +
  209 + environment = Environment.default
  210 + environment.expects(:noreply_email).returns('sender@example.com')
  211 + environment.expects(:default_hostname).returns('example.com')
  212 + environment.expects(:name).returns('example').at_least_once
  213 +
  214 + task.expects(:requestor).returns(requestor).at_least_once
  215 + requestor.expects(:environment).returns(environment).at_least_once
  216 + task.expects(:environment).returns(environment).at_least_once
  217 +
  218 + task.send(:send_notification, :finished).deliver
  219 + assert !ActionMailer::Base.deliveries.empty?
  220 + mail = ActionMailer::Base.deliveries.last
  221 + assert_match /text\/html/, mail.content_type
  222 + assert_equal 'template subject - example', mail.subject.to_s
  223 + assert_equal 'template body - example - my name', mail.body.to_s
  224 + end
  225 +
198 private 226 private
199 def read_fixture(action) 227 def read_fixture(action)
200 IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}") 228 IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}")