Commit 331869b02d163756ce3ef06c1c2c651407c99b5e

Authored by Victor Costa
1 parent 3af4c864

Manage environment email templates

app/controllers/admin/environment_email_templates_controller.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class EnvironmentEmailTemplatesController < EmailTemplatesController
  2 +
  3 + protect 'manage_email_templates', :environment
  4 +
  5 + protected
  6 +
  7 + def owner
  8 + environment
  9 + end
  10 +
  11 + before_filter :only => :index do
  12 + @back_to = url_for(:controller => :admin_panel)
  13 + end
  14 +
  15 +end
... ...
app/controllers/email_templates_controller.rb 0 → 100644
... ... @@ -0,0 +1,62 @@
  1 +class EmailTemplatesController < ApplicationController
  2 +
  3 + def index
  4 + @email_templates = owner.email_templates
  5 + end
  6 +
  7 + def show
  8 + @email_template = owner.email_templates.find(params[:id])
  9 +
  10 + respond_to do |format|
  11 + format.html # show.html.erb
  12 + format.json { render json: @email_template }
  13 + end
  14 + end
  15 +
  16 + def show_parsed
  17 + @email_template = owner.email_templates.find(params[:id])
  18 + template_params = {:profile => owner, :environment => environment}
  19 + render json: {:parsed_body => @email_template.parsed_body(template_params), :parsed_subject => @email_template.parsed_subject(template_params)}
  20 + end
  21 +
  22 + def new
  23 + @email_template = owner.email_templates.build(:owner => owner)
  24 + end
  25 +
  26 + def edit
  27 + @email_template = owner.email_templates.find(params[:id])
  28 + end
  29 +
  30 + def create
  31 + @email_template = owner.email_templates.build(params[:email_template])
  32 + @email_template.owner = owner
  33 +
  34 + if @email_template.save
  35 + session[:notice] = _('Email template was successfully created.')
  36 + redirect_to url_for(:action => :index)
  37 + else
  38 + render action: "new"
  39 + end
  40 + end
  41 +
  42 + def update
  43 + @email_template = owner.email_templates.find(params[:id])
  44 +
  45 + if @email_template.update_attributes(params[:email_template])
  46 + session[:notice] = _('Email template was successfully updated.')
  47 + redirect_to url_for(:action => :index)
  48 + else
  49 + render action: "edit"
  50 + end
  51 + end
  52 +
  53 + def destroy
  54 + @email_template = owner.email_templates.find(params[:id])
  55 + @email_template.destroy
  56 +
  57 + respond_to do |format|
  58 + format.html { redirect_to url_for(:action => :index)}
  59 + format.json { head :no_content }
  60 + end
  61 + end
  62 +end
... ...
app/controllers/my_profile/email_templates_controller.rb
... ... @@ -1,63 +0,0 @@
1   -class EmailTemplatesController < MyProfileController
2   -
3   - protect 'manage_email_templates', :profile
4   -
5   - def index
6   - @email_templates = profile.email_templates
7   - end
8   -
9   - def show
10   - @email_template = profile.email_templates.find(params[:id])
11   -
12   - respond_to do |format|
13   - format.html # show.html.erb
14   - format.json { render json: @email_template }
15   - end
16   - end
17   -
18   - def show_parsed
19   - @email_template = profile.email_templates.find(params[:id])
20   - template_params = {:profile => profile, :environment => environment}
21   - render json: {:parsed_body => @email_template.parsed_body(template_params), :parsed_subject => @email_template.parsed_subject(template_params)}
22   - end
23   -
24   - def new
25   - @email_template = profile.email_templates.build(:owner => profile)
26   - end
27   -
28   - def edit
29   - @email_template = profile.email_templates.find(params[:id])
30   - end
31   -
32   - def create
33   - @email_template = profile.email_templates.build(params[:email_template], :owner => profile)
34   -
35   - if @email_template.save
36   - session[:notice] = _('Email template was successfully created.')
37   - redirect_to url_for(:action => :index)
38   - else
39   - render action: "new"
40   - end
41   - end
42   -
43   - def update
44   - @email_template = profile.email_templates.find(params[:id])
45   -
46   - if @email_template.update_attributes(params[:email_template])
47   - session[:notice] = _('Email template was successfully updated.')
48   - redirect_to url_for(:action => :index)
49   - else
50   - render action: "edit"
51   - end
52   - end
53   -
54   - def destroy
55   - @email_template = profile.email_templates.find(params[:id])
56   - @email_template.destroy
57   -
58   - respond_to do |format|
59   - format.html { redirect_to url_for(:action => :index)}
60   - format.json { head :no_content }
61   - end
62   - end
63   -end
app/controllers/my_profile/profile_email_templates_controller.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +class ProfileEmailTemplatesController < EmailTemplatesController
  2 +
  3 + needs_profile
  4 + protect 'manage_email_templates', :profile
  5 +
  6 + protected
  7 +
  8 + def owner
  9 + profile
  10 + end
  11 +
  12 + before_filter :only => :index do
  13 + @back_to = url_for(:controller => :profile_editor)
  14 + end
  15 +
  16 +end
... ...
app/helpers/task_helper.rb
... ... @@ -5,7 +5,7 @@ module TaskHelper
5 5  
6 6 content_tag(
7 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 => include_blank, 'data-url' => url_for(:controller => 'email_templates', :action => 'show_parsed', :profile => profile.identifier))),
  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 => include_blank, 'data-url' => url_for(:controller => 'profile_email_templates', :action => 'show_parsed', :profile => profile.identifier))),
9 9 :class => 'template-selection'
10 10 )
11 11 end
... ...
app/models/email_template.rb
... ... @@ -31,6 +31,10 @@ class EmailTemplate &lt; ActiveRecord::Base
31 31 HashWithIndifferentAccess.new EmailTemplate.available_types.select {|k, v| owner.kind_of?(v[:owner_type])}
32 32 end
33 33  
  34 + def type_description
  35 + available_types.fetch(template_type, {})[:description]
  36 + end
  37 +
34 38 def unique_by_type?
35 39 available_types.fetch(template_type, {})[:unique]
36 40 end
... ...
app/models/environment.rb
... ... @@ -51,6 +51,7 @@ class Environment &lt; ActiveRecord::Base
51 51 'manage_environment_licenses' => N_('Manage environment licenses'),
52 52 'manage_environment_trusted_sites' => N_('Manage environment trusted sites'),
53 53 'edit_appearance' => N_('Edit appearance'),
  54 + 'manage_email_templates' => N_('Manage Email Templates'),
54 55 }
55 56  
56 57 module Roles
... ...
app/views/admin_panel/index.html.erb
... ... @@ -11,6 +11,7 @@
11 11 <tr><td><%= link_to _('Homepage'), :action => 'set_portal_community' %></td></tr>
12 12 <tr><td><%= link_to _('Licenses'), :controller =>'licenses' %></td></tr>
13 13 <tr><td><%= link_to _('Trusted sites'), :controller =>'trusted_sites' %></td></tr>
  14 + <tr><td><%= link_to _('Email templates'), :controller =>'environment_email_templates' %></td></tr>
14 15 </table>
15 16  
16 17 <h2><%= _('Profiles') %></h2>
... ...
app/views/email_templates/_form.html.erb
1   -<%= form_for(@email_template, :url => {:controller => :email_templates, :action => @email_template.persisted? ? :update : :create, :id => @email_template.id}) do |f| %>
  1 +<%= form_for(@email_template, :url => {:action => @email_template.persisted? ? :update : :create, :id => @email_template.id}) do |f| %>
2 2  
3 3 <%= error_messages_for :email_template if @email_template.errors.any? %>
4 4  
... ... @@ -25,7 +25,7 @@
25 25  
26 26 <div class="actions">
27 27 <%= submit_button(:save, _('Save')) %>
28   - <%= button(:back, _('Back'), :controller => :email_templates) %>
  28 + <%= button(:back, _('Back'), :action => :index) %>
29 29 </div>
30 30  
31 31 <% end %>
... ...
app/views/email_templates/index.html.erb
... ... @@ -11,10 +11,10 @@
11 11 <% @email_templates.each do |email_template| %>
12 12 <tr>
13 13 <td><%= email_template.name %></td>
14   - <td><%= email_template.available_types[email_template.template_type][:description] if email_template.template_type.present? %></td>
  14 + <td><%= email_template.type_description %></td>
15 15 <td>
16   - <%= button_without_text(:edit, _('Edit'), {:controller => :email_templates, :action => :edit, :id => email_template.id}) %>
17   - <%= button_without_text(:remove, _('Remove'), {:controller => :email_templates, :action => :destroy, :id => email_template.id}, method: :delete, data: { confirm: 'Are you sure?' }) %>
  16 + <%= button_without_text(:edit, _('Edit'), {:action => :edit, :id => email_template.id}) %>
  17 + <%= button_without_text(:remove, _('Remove'), {:action => :destroy, :id => email_template.id}, method: :delete, data: { confirm: 'Are you sure?' }) %>
18 18 </td>
19 19 </tr>
20 20 <% end %>
... ... @@ -22,6 +22,6 @@
22 22  
23 23 <br />
24 24  
25   - <%= button(:new, _('New template'), :controller => :email_templates, :action => :new) %>
26   - <%= button(:back, _('Back to control panel'), :controller => :profile_editor) %>
  25 + <%= button(:new, _('New template'), :action => :new) %>
  26 + <%= button(:back, _('Back'), @back_to) %>
27 27 </div>
... ...
app/views/email_templates/show.html.erb
1 1 <p id="notice"><%= notice %></p>
2 2  
3 3  
4   -<%= link_to 'Edit', url_for(:controller => :email_templates, :action => :edit, :id => @email_template.id) %> |
5   -<%= link_to 'Back', url_for(:controller => :email_templates) %>
  4 +<%= link_to 'Edit', url_for(:action => :edit, :id => @email_template.id) %> |
  5 +<%= link_to 'Back', url_for(:action => :index) %>
... ...
app/views/profile/send_mail.html.erb
... ... @@ -6,7 +6,7 @@
6 6  
7 7 <% if @email_templates.present? %>
8 8 <div class="template-selection">
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'))) %>
  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 => 'profile_email_templates', :action => 'show_parsed'))) %>
10 10 </div>
11 11 <% end %>
12 12  
... ...
app/views/profile_editor/index.html.erb
... ... @@ -72,7 +72,7 @@
72 72  
73 73 <%= control_panel_button(_('Edit welcome page'), 'welcome-page', :action => 'welcome_page') if has_welcome_page %>
74 74  
75   - <%= control_panel_button(_('Email Templates'), 'email-templates', :controller => :email_templates) if profile.organization? %>
  75 + <%= control_panel_button(_('Email Templates'), 'email-templates', :controller => :profile_email_templates) if profile.organization? %>
76 76  
77 77 <% @plugins.dispatch(:control_panel_buttons).each do |button| %>
78 78 <%= control_panel_button(button[:title], button[:icon], button[:url], button[:html_options]) %>
... ...
test/fixtures/roles.yml
... ... @@ -38,6 +38,7 @@ four:
38 38 - manage_environment_organizations
39 39 - manage_environment_templates
40 40 - manage_environment_licenses
  41 + - manage_email_templates
41 42 profile_admin:
42 43 id: 5
43 44 environment_id: 1
... ... @@ -60,6 +61,7 @@ profile_admin:
60 61 - manage_friends
61 62 - validate_enterprise
62 63 - publish_content
  64 + - manage_email_templates
63 65 profile_member:
64 66 id: 6
65 67 environment_id: 1
... ... @@ -100,3 +102,4 @@ environment_administrator:
100 102 - destroy_profile
101 103 - manage_environment_templates
102 104 - manage_environment_licenses
  105 + - manage_email_templates
... ...
test/functional/email_templates_controller_test.rb
... ... @@ -1,68 +0,0 @@
1   -require 'test_helper'
2   -
3   -class EmailTemplatesControllerTest < ActionController::TestCase
4   -
5   - setup do
6   - @profile = fast_create(Community)
7   - @email_template = EmailTemplate.create!(:name => 'template', :owner => @profile)
8   - @person = create_user_with_permission('templatemanager', 'manage_email_templates', @profile)
9   - login_as(@person.user.login)
10   - end
11   -
12   - attr_accessor :profile, :person
13   -
14   - test "should get index" do
15   - get :index, :profile => profile.identifier
16   - assert_response :success
17   - assert_not_nil assigns(:email_templates)
18   - end
19   -
20   - test "should get new" do
21   - get :new, :profile => profile.identifier
22   - assert_response :success
23   - end
24   -
25   - test "should create email_template" do
26   - assert_difference('EmailTemplate.count') do
27   - post :create, email_template: { :name => 'test' }, :profile => profile.identifier
28   - end
29   -
30   - assert_redirected_to url_for(:action => :index)
31   - end
32   -
33   - test "should show email_template" do
34   - get :show, id: @email_template, :profile => profile.identifier
35   - assert_response :success
36   - end
37   -
38   - test "should get edit" do
39   - get :edit, id: @email_template, :profile => profile.identifier
40   - assert_response :success
41   - end
42   -
43   - test "should update email_template" do
44   - put :update, id: @email_template, email_template: { }, :profile => profile.identifier
45   - assert_redirected_to url_for(:action => :index)
46   - end
47   -
48   - test "should destroy email_template" do
49   - assert_difference('EmailTemplate.count', -1) do
50   - delete :destroy, id: @email_template, :profile => profile.identifier
51   - end
52   -
53   - assert_redirected_to url_for(:action => :index)
54   - end
55   -
56   - test "should get parsed template" do
57   - environment = Environment.default
58   - @email_template.subject = '{{profile.name}} - {{profile.identifier}}'
59   - @email_template.body = '{{profile.name}} - {{profile.identifier}} - {{environment.name}}'
60   - @email_template.save!
61   - get :show_parsed, id: @email_template, :profile => profile.identifier
62   - assert_response :success
63   - json_response = ActiveSupport::JSON.decode(@response.body)
64   - assert_equal "#{profile.name} - #{profile.identifier}", json_response['parsed_subject']
65   - assert_equal "#{profile.name} - #{profile.identifier} - #{environment.name}", json_response['parsed_body']
66   - end
67   -
68   -end
test/functional/environment_email_templates_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,65 @@
  1 +require 'test_helper'
  2 +
  3 +class EnvironmentEmailTemplatesControllerTest < ActionController::TestCase
  4 +
  5 + setup do
  6 + @email_template = EmailTemplate.create!(:name => 'template', :owner => Environment.default)
  7 + person = create_user_with_permission('template_manager', 'manage_email_templates', Environment.default)
  8 + login_as(person.user.login)
  9 + end
  10 +
  11 + test "should get index" do
  12 + get :index
  13 + assert_response :success
  14 + assert_not_nil assigns(:email_templates)
  15 + end
  16 +
  17 + test "should get new" do
  18 + get :new
  19 + assert_response :success
  20 + end
  21 +
  22 + test "should create email_template" do
  23 + assert_difference('EmailTemplate.count') do
  24 + post :create, email_template: { :name => 'test' }
  25 + end
  26 +
  27 + assert_redirected_to url_for(:action => :index)
  28 + end
  29 +
  30 + test "should show email_template" do
  31 + get :show, id: @email_template
  32 + assert_response :success
  33 + end
  34 +
  35 + test "should get edit" do
  36 + get :edit, id: @email_template
  37 + assert_response :success
  38 + end
  39 +
  40 + test "should update email_template" do
  41 + put :update, id: @email_template, email_template: { }
  42 + assert_redirected_to url_for(:action => :index)
  43 + end
  44 +
  45 + test "should destroy email_template" do
  46 + assert_difference('EmailTemplate.count', -1) do
  47 + delete :destroy, id: @email_template
  48 + end
  49 +
  50 + assert_redirected_to url_for(:action => :index)
  51 + end
  52 +
  53 + test "should get parsed template" do
  54 + environment = Environment.default
  55 + @email_template.subject = '{{environment.name}}'
  56 + @email_template.body = '{{environment.name}}'
  57 + @email_template.save!
  58 + get :show_parsed, id: @email_template
  59 + assert_response :success
  60 + json_response = ActiveSupport::JSON.decode(@response.body)
  61 + assert_equal "#{environment.name}", json_response['parsed_subject']
  62 + assert_equal "#{environment.name}", json_response['parsed_body']
  63 + end
  64 +
  65 +end
... ...
test/functional/profile_editor_controller_test.rb
... ... @@ -629,7 +629,7 @@ class ProfileEditorControllerTest &lt; ActionController::TestCase
629 629 should 'display email template link for organizations in control panel' do
630 630 profile = fast_create(Organization)
631 631 get :index, :profile => profile.identifier
632   - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/email_templates" }
  632 + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/profile_email_templates" }
633 633 end
634 634  
635 635 should 'not display email template link in control panel for person' do
... ...
test/functional/profile_email_templates_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,68 @@
  1 +require 'test_helper'
  2 +
  3 +class ProfileEmailTemplatesControllerTest < ActionController::TestCase
  4 +
  5 + setup do
  6 + @profile = fast_create(Community)
  7 + @email_template = EmailTemplate.create!(:name => 'template', :owner => @profile)
  8 + @person = create_user_with_permission('templatemanager', 'manage_email_templates', @profile)
  9 + login_as(@person.user.login)
  10 + end
  11 +
  12 + attr_accessor :profile, :person
  13 +
  14 + test "should get index" do
  15 + get :index, :profile => profile.identifier
  16 + assert_response :success
  17 + assert_not_nil assigns(:email_templates)
  18 + end
  19 +
  20 + test "should get new" do
  21 + get :new, :profile => profile.identifier
  22 + assert_response :success
  23 + end
  24 +
  25 + test "should create email_template" do
  26 + assert_difference('EmailTemplate.count') do
  27 + post :create, email_template: { :name => 'test' }, :profile => profile.identifier
  28 + end
  29 +
  30 + assert_redirected_to url_for(:action => :index)
  31 + end
  32 +
  33 + test "should show email_template" do
  34 + get :show, id: @email_template, :profile => profile.identifier
  35 + assert_response :success
  36 + end
  37 +
  38 + test "should get edit" do
  39 + get :edit, id: @email_template, :profile => profile.identifier
  40 + assert_response :success
  41 + end
  42 +
  43 + test "should update email_template" do
  44 + put :update, id: @email_template, email_template: { }, :profile => profile.identifier
  45 + assert_redirected_to url_for(:action => :index)
  46 + end
  47 +
  48 + test "should destroy email_template" do
  49 + assert_difference('EmailTemplate.count', -1) do
  50 + delete :destroy, id: @email_template, :profile => profile.identifier
  51 + end
  52 +
  53 + assert_redirected_to url_for(:action => :index)
  54 + end
  55 +
  56 + test "should get parsed template" do
  57 + environment = Environment.default
  58 + @email_template.subject = '{{profile.name}} - {{profile.identifier}}'
  59 + @email_template.body = '{{profile.name}} - {{profile.identifier}} - {{environment.name}}'
  60 + @email_template.save!
  61 + get :show_parsed, id: @email_template, :profile => profile.identifier
  62 + assert_response :success
  63 + json_response = ActiveSupport::JSON.decode(@response.body)
  64 + assert_equal "#{profile.name} - #{profile.identifier}", json_response['parsed_subject']
  65 + assert_equal "#{profile.name} - #{profile.identifier} - #{environment.name}", json_response['parsed_body']
  66 + end
  67 +
  68 +end
... ...