Commit bff21134508aa49a5702953e94af1af03b02031b

Authored by Victor Costa
1 parent d215b232

Define multiple types for email templates

app/controllers/public/profile_controller.rb
... ... @@ -353,7 +353,7 @@ class ProfileController < PublicController
353 353  
354 354 def send_mail
355 355 @mailing = profile.mailings.build(params[:mailing])
356   - @email_templates = profile.email_templates
  356 + @email_templates = profile.email_templates.find_all_by_template_type(:organization_members)
357 357 if request.post?
358 358 @mailing.locale = locale
359 359 @mailing.person = user
... ...
app/models/email_template.rb
... ... @@ -14,6 +14,13 @@ class EmailTemplate < ActiveRecord::Base
14 14 @parsed_subject ||= parse(subject, params)
15 15 end
16 16  
  17 + def available_types
  18 + HashWithIndifferentAccess.new ({
  19 + :task_rejection => {:description => _('Task Rejection')},
  20 + :organization_members => {:description => _('Organization Members')}
  21 + })
  22 + end
  23 +
17 24 protected
18 25  
19 26 def parse(source, params)
... ...
app/views/email_templates/_form.html.erb
... ... @@ -5,6 +5,7 @@
5 5 <div class="template-fields">
6 6 <div class="header-fields">
7 7 <%= labelled_form_field(_('Template Name:'), f.text_field(:name)) %>
  8 + <%= labelled_form_field(_('Template Type:'), f.select(:template_type, @email_template.available_types.map {|k,v| [v[:description], k]}, :include_blank => true)) %>
8 9 <%= labelled_form_field(_('Subject:'), f.text_field(:subject)) %>
9 10 </div>
10 11 <div class="available-params">
... ...
app/views/email_templates/index.html.erb
... ... @@ -3,12 +3,14 @@
3 3 <table>
4 4 <tr>
5 5 <th><%= _('Name') %></th>
  6 + <th><%= _('Type') %></th>
6 7 <th></th>
7 8 </tr>
8 9  
9 10 <% @email_templates.each do |email_template| %>
10 11 <tr>
11 12 <td><%= email_template.name %></td>
  13 + <td><%= email_template.available_types[email_template.template_type][:description] if email_template.template_type.present? %></td>
12 14 <td>
13 15 <%= link_to 'Edit', url_for(:controller => :email_templates, :action => :edit, :id => email_template.id) %>
14 16 <%= link_to 'Destroy', url_for(:controller => :email_templates, :action => :destroy, :id => email_template.id), method: :delete, data: { confirm: 'Are you sure?' } %>
... ...
app/views/profile/send_mail.html.erb
... ... @@ -4,11 +4,11 @@
4 4  
5 5 <%= error_messages_for :mailing %>
6 6  
7   -<div class="template-selection">
8   - <% if @email_templates.present? %>
  7 +<% if @email_templates.present? %>
  8 + <div class="template-selection">
9 9 <%= labelled_form_field(_('Select a template:'), select_tag(:template, options_from_collection_for_select(@email_templates, :id, :name), :include_blank => true, 'data-url' => url_for(:controller => 'email_templates', :action => 'show_parsed'))) %>
10   - <% end %>
11   -</div>
  10 + </div>
  11 +<% end %>
12 12  
13 13 <%= form_for :mailing, :url => {:action => 'send_mail'}, :html => {:id => 'mailing-form'} do |f| %>
14 14  
... ...
test/functional/profile_controller_test.rb
... ... @@ -1471,6 +1471,29 @@ class ProfileControllerTest &lt; ActionController::TestCase
1471 1471 assert_redirected_to :action => 'members'
1472 1472 end
1473 1473  
  1474 + should 'display email templates as an option to send mail' do
  1475 + community = fast_create(Community)
  1476 + create_user_with_permission('profile_moderator_user', 'send_mail_to_members', community)
  1477 + login_as('profile_moderator_user')
  1478 +
  1479 + template1 = EmailTemplate.create!(:owner => community, :name => "Template 1", :template_type => :organization_members)
  1480 + template2 = EmailTemplate.create!(:owner => community, :name => "Template 2")
  1481 +
  1482 + get :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'}
  1483 + assert_select '.template-selection'
  1484 + assert_equal [template1], assigns(:email_templates)
  1485 + end
  1486 +
  1487 + should 'do not display email template selection when there is no template for organization members' do
  1488 + community = fast_create(Community)
  1489 + create_user_with_permission('profile_moderator_user', 'send_mail_to_members', community)
  1490 + login_as('profile_moderator_user')
  1491 +
  1492 + get :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'}
  1493 + assert_select '.template-selection', 0
  1494 + assert assigns(:email_templates).empty?
  1495 + end
  1496 +
1474 1497 should 'show all fields to anonymous user' do
1475 1498 viewed = create_user('person_1').person
1476 1499 Environment.any_instance.stubs(:active_person_fields).returns(['sex', 'birth_date'])
... ...