Commit 933d37711a7b49d2cef28bfac618f1922c546031
Exists in
staging
and in
4 other branches
Merge branch 'email_template' into production
Conflicts: app/models/article.rb test/functional/tasks_controller_test.rb
Showing
11 changed files
with
117 additions
and
11 deletions
Show diff stats
app/controllers/my_profile/tasks_controller.rb
| ... | ... | @@ -4,7 +4,8 @@ class TasksController < MyProfileController |
| 4 | 4 | protect :perform_task, :profile, :except => [:index, :save_tags, :search_tags] |
| 5 | 5 | |
| 6 | 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 | 10 | @filter_type = params[:filter_type].presence |
| 10 | 11 | @filter_text = params[:filter_text].presence | ... | ... |
app/helpers/application_helper.rb
| ... | ... | @@ -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
| ... | ... | @@ -821,6 +821,10 @@ class Article < ActiveRecord::Base |
| 821 | 821 | "content_viewer/view_page" |
| 822 | 822 | end |
| 823 | 823 | |
| 824 | + def to_liquid | |
| 825 | + HashWithIndifferentAccess.new :name => name, :abstract => abstract, :body => body, :id => id, :parent_id => parent_id, :author => author | |
| 826 | + end | |
| 827 | + | |
| 824 | 828 | private |
| 825 | 829 | |
| 826 | 830 | def sanitize_tag_list | ... | ... |
app/models/email_template.rb
| ... | ... | @@ -17,6 +17,7 @@ class EmailTemplate < ActiveRecord::Base |
| 17 | 17 | def available_types |
| 18 | 18 | HashWithIndifferentAccess.new ({ |
| 19 | 19 | :task_rejection => {:description => _('Task Rejection')}, |
| 20 | + :task_acceptance => {:description => _('Task Acceptance')}, | |
| 20 | 21 | :organization_members => {:description => _('Organization Members')} |
| 21 | 22 | }) |
| 22 | 23 | end | ... | ... |
app/views/tasks/_task.html.erb
| ... | ... | @@ -52,12 +52,14 @@ |
| 52 | 52 | <%= fields_for "tasks[#{task.id}][task]", task do |f| %> |
| 53 | 53 | <% if task.accept_details %> |
| 54 | 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 | 56 | <%= render :partial => partial_for_class(task.class, nil, :accept_details), :locals => {:task => task, :f => f} %> |
| 56 | 57 | </div> |
| 57 | 58 | <% end %> |
| 58 | 59 | |
| 59 | 60 | <% if task.reject_details %> |
| 60 | 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 | 63 | <%= render :partial => partial_for_class(task.class, nil, :reject_details), :locals => {:task => task, :f => f} %> |
| 62 | 64 | </div> |
| 63 | 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 | 1 | <%= labelled_form_field(_('Rejection explanation'), f.text_area(:reject_explanation, :rows => 5)) %> | ... | ... |
public/javascripts/tasks.js
| ... | ... | @@ -106,16 +106,28 @@ |
| 106 | 106 | |
| 107 | 107 | $("input.task_accept_radio").click(function(){ |
| 108 | 108 | task_id = this.getAttribute("task_id"); |
| 109 | - $('#on-accept-information-' + task_id).show('fast'); | |
| 110 | - $('#on-reject-information-' + task_id).hide('fast'); | |
| 109 | + var accept_container = $('#on-accept-information-' + task_id); | |
| 110 | + var reject_container = $('#on-reject-information-' + task_id); | |
| 111 | + | |
| 112 | + accept_container.show('fast'); | |
| 113 | + reject_container.hide('fast'); | |
| 111 | 114 | $('#on-skip-information-' + task_id).hide('fast'); |
| 115 | + | |
| 116 | + reject_container.find('input, select').prop('disabled', true); | |
| 117 | + accept_container.find('input, select').prop('disabled', false); | |
| 112 | 118 | }) |
| 113 | 119 | |
| 114 | 120 | $("input.task_reject_radio").click(function(){ |
| 115 | 121 | task_id = this.getAttribute("task_id"); |
| 116 | - $('#on-accept-information-' + task_id).hide('fast'); | |
| 117 | - $('#on-reject-information-' + task_id).show('fast'); | |
| 122 | + var accept_container = $('#on-accept-information-' + task_id); | |
| 123 | + var reject_container = $('#on-reject-information-' + task_id); | |
| 124 | + | |
| 125 | + accept_container.hide('fast'); | |
| 126 | + reject_container.show('fast'); | |
| 118 | 127 | $('#on-skip-information-' + task_id).hide('fast'); |
| 128 | + | |
| 129 | + reject_container.find('input, select').prop('disabled', false); | |
| 130 | + accept_container.find('input, select').prop('disabled', true); | |
| 119 | 131 | }) |
| 120 | 132 | |
| 121 | 133 | $("input.task_skip_radio").click(function(){ | ... | ... |
test/functional/tasks_controller_test.rb
| ... | ... | @@ -762,4 +762,30 @@ class TasksControllerTest < ActionController::TestCase |
| 762 | 762 | assert_equal [task], assigns(:tasks) |
| 763 | 763 | end |
| 764 | 764 | |
| 765 | + should "display email template selection when accept a task" do | |
| 766 | + community = fast_create(Community) | |
| 767 | + @controller.stubs(:profile).returns(community) | |
| 768 | + person = create_user_with_permission('taskviewer', 'view_tasks', community) | |
| 769 | + login_as person.user.login | |
| 770 | + | |
| 771 | + email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_acceptance) | |
| 772 | + task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person) | |
| 773 | + get :index | |
| 774 | + assert_select "#on-accept-information-#{task.id} .template-selection" | |
| 775 | + assert_equal [email_template], assigns(:acceptance_email_templates) | |
| 776 | + end | |
| 777 | + | |
| 778 | + should "display email template selection when reject a task" do | |
| 779 | + community = fast_create(Community) | |
| 780 | + @controller.stubs(:profile).returns(community) | |
| 781 | + person = create_user_with_permission('taskviewer', 'view_tasks', community) | |
| 782 | + login_as person.user.login | |
| 783 | + | |
| 784 | + email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_rejection) | |
| 785 | + task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person) | |
| 786 | + get :index | |
| 787 | + assert_select "#on-reject-information-#{task.id} .template-selection" | |
| 788 | + assert_equal [email_template], assigns(:rejection_email_templates) | |
| 789 | + end | |
| 790 | + | |
| 765 | 791 | end | ... | ... |
| ... | ... | @@ -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 < ActiveSupport::TestCase |
| 195 | 195 | assert_equal 'template body - example - my name - explanation', mail.body.to_s |
| 196 | 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 | 226 | private |
| 199 | 227 | def read_fixture(action) |
| 200 | 228 | IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}") | ... | ... |