From 8a4dc901a9dccdf8cf20ab495645e6325cdeb08f Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 6 Jul 2015 11:43:07 -0300 Subject: [PATCH] Choose email template when accept a task --- app/controllers/my_profile/tasks_controller.rb | 3 ++- app/helpers/application_helper.rb | 2 ++ app/helpers/task_helper.rb | 13 +++++++++++++ app/models/article.rb | 4 ++++ app/models/email_template.rb | 1 + app/views/tasks/_task.html.erb | 2 ++ app/views/tasks/_task_reject_details.html.erb | 6 ------ public/javascripts/tasks.js | 20 ++++++++++++++++---- test/functional/tasks_controller_test.rb | 26 ++++++++++++++++++++++++++ test/unit/task_helper_test.rb | 23 +++++++++++++++++++++++ test/unit/task_mailer_test.rb | 28 ++++++++++++++++++++++++++++ 11 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 app/helpers/task_helper.rb create mode 100644 test/unit/task_helper_test.rb diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index 91a18c0..08d379f 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -4,7 +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) + @rejection_email_templates = profile.email_templates.find_all_by_template_type(:task_rejection) + @acceptance_email_templates = profile.email_templates.find_all_by_template_type(:task_acceptance) @filter_type = params[:filter_type].presence @filter_text = params[:filter_text].presence diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2a181fd..3a869a0 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -44,6 +44,8 @@ module ApplicationHelper include PluginsHelper + include TaskHelper + def locale (@page && !@page.language.blank?) ? @page.language : FastGettext.locale end diff --git a/app/helpers/task_helper.rb b/app/helpers/task_helper.rb new file mode 100644 index 0000000..c6d2dab --- /dev/null +++ b/app/helpers/task_helper.rb @@ -0,0 +1,13 @@ +module TaskHelper + + def task_email_template(description, email_templates, task) + return '' unless email_templates.present? + + content_tag( + :div, + 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))), + :class => 'template-selection' + ) + end + +end diff --git a/app/models/article.rb b/app/models/article.rb index 2a97e99..c6a46b8 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -792,6 +792,10 @@ class Article < ActiveRecord::Base true end + def to_liquid + HashWithIndifferentAccess.new :name => name, :abstract => abstract, :body => body, :id => id, :parent_id => parent_id, :author => author + end + private def sanitize_tag_list diff --git a/app/models/email_template.rb b/app/models/email_template.rb index e1ffe80..557bd56 100644 --- a/app/models/email_template.rb +++ b/app/models/email_template.rb @@ -17,6 +17,7 @@ class EmailTemplate < ActiveRecord::Base def available_types HashWithIndifferentAccess.new ({ :task_rejection => {:description => _('Task Rejection')}, + :task_acceptance => {:description => _('Task Acceptance')}, :organization_members => {:description => _('Organization Members')} }) end diff --git a/app/views/tasks/_task.html.erb b/app/views/tasks/_task.html.erb index d1a4cd2..4abe101 100644 --- a/app/views/tasks/_task.html.erb +++ b/app/views/tasks/_task.html.erb @@ -52,12 +52,14 @@ <%= fields_for "tasks[#{task.id}][task]", task do |f| %> <% if task.accept_details %> <% end %> <% if task.reject_details %> <% end %> diff --git a/app/views/tasks/_task_reject_details.html.erb b/app/views/tasks/_task_reject_details.html.erb index 975457f..13bc80c 100644 --- a/app/views/tasks/_task_reject_details.html.erb +++ b/app/views/tasks/_task_reject_details.html.erb @@ -1,7 +1 @@ -<% 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/public/javascripts/tasks.js b/public/javascripts/tasks.js index 3a2947c..125c463 100644 --- a/public/javascripts/tasks.js +++ b/public/javascripts/tasks.js @@ -2,16 +2,28 @@ $("input.task_accept_radio").click(function(){ task_id = this.getAttribute("task_id"); - $('#on-accept-information-' + task_id).show('fast'); - $('#on-reject-information-' + task_id).hide('fast'); + var accept_container = $('#on-accept-information-' + task_id); + var reject_container = $('#on-reject-information-' + task_id); + + accept_container.show('fast'); + reject_container.hide('fast'); $('#on-skip-information-' + task_id).hide('fast'); + + reject_container.find('input, select').prop('disabled', true); + accept_container.find('input, select').prop('disabled', false); }) $("input.task_reject_radio").click(function(){ task_id = this.getAttribute("task_id"); - $('#on-accept-information-' + task_id).hide('fast'); - $('#on-reject-information-' + task_id).show('fast'); + var accept_container = $('#on-accept-information-' + task_id); + var reject_container = $('#on-reject-information-' + task_id); + + accept_container.hide('fast'); + reject_container.show('fast'); $('#on-skip-information-' + task_id).hide('fast'); + + reject_container.find('input, select').prop('disabled', false); + accept_container.find('input, select').prop('disabled', true); }) $("input.task_skip_radio").click(function(){ diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 49d0d6d..c44bb21 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -636,4 +636,30 @@ class TasksControllerTest < ActionController::TestCase assert_equal profile, t.reload.closed_by end + should "display email template selection when accept a task" do + community = fast_create(Community) + @controller.stubs(:profile).returns(community) + person = create_user_with_permission('taskviewer', 'view_tasks', community) + login_as person.user.login + + email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_acceptance) + task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person) + get :index + assert_select "#on-accept-information-#{task.id} .template-selection" + assert_equal [email_template], assigns(:acceptance_email_templates) + end + + should "display email template selection when reject a task" do + community = fast_create(Community) + @controller.stubs(:profile).returns(community) + person = create_user_with_permission('taskviewer', 'view_tasks', community) + login_as person.user.login + + email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_rejection) + task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person) + get :index + assert_select "#on-reject-information-#{task.id} .template-selection" + assert_equal [email_template], assigns(:rejection_email_templates) + end + end diff --git a/test/unit/task_helper_test.rb b/test/unit/task_helper_test.rb new file mode 100644 index 0000000..289a693 --- /dev/null +++ b/test/unit/task_helper_test.rb @@ -0,0 +1,23 @@ +require_relative "../test_helper" + +class TaskHelperTest < ActionView::TestCase + + include ApplicationHelper + + def setup + @profile = fast_create(Profile) + @task = fast_create(Task, :target_id => @profile.id) + end + + attr_accessor :task, :profile + + should 'return select field for template selection when there is templates to choose' do + email_templates = 3.times.map { EmailTemplate.new } + assert_tag_in_string task_email_template('Description', email_templates, task), :tag => 'div', :attributes => {:class => 'template-selection'} + end + + should 'not return select field for template selection when there is no templates to choose' do + assert task_email_template('Description', [], task).blank? + end + +end diff --git a/test/unit/task_mailer_test.rb b/test/unit/task_mailer_test.rb index d83a0e5..1d04e7a 100644 --- a/test/unit/task_mailer_test.rb +++ b/test/unit/task_mailer_test.rb @@ -195,6 +195,34 @@ class TaskMailerTest < ActiveSupport::TestCase assert_equal 'template body - example - my name - explanation', mail.body.to_s end + should 'be able to send accept notification based on a selected template' do + task = Task.new + task.expects(:task_finished_message).returns('the message') + + 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.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, :finished).deliver + assert !ActionMailer::Base.deliveries.empty? + mail = ActionMailer::Base.deliveries.last + assert_match /text\/html/, mail.content_type + assert_equal 'template subject - example', mail.subject.to_s + assert_equal 'template body - example - my name', mail.body.to_s + end + private def read_fixture(action) IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}") -- libgit2 0.21.2