Commit d90847dbf38af098a528e725028356f7a6687fbf

Authored by Tallys Martins
1 parent 5a45530a

Send email to environment and profile admins

- Environment admins can now choose the recipients
that will recieve an email sent throw the admin panel.
There are now 3 options: All users(exclusive with other options)
			 Environment Admins(inclusive with opt below)
			 Profile Admins(inclusive with opt above)

Signed-off-by: Artur Bersan de Faria <arturbersan@gmail.com>
Signed-off-by: Brenddon Gontijo <brenddongontijo@msn.com>
Signed-off-by: Matheus Miranda <matheusmirandalacerda@gmail.com>
Signed-off-by: Omar Junior <omarroinuj@gmail.com>
Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
app/controllers/admin/users_controller.rb
... ... @@ -45,7 +45,6 @@ class UsersController &lt; AdminController
45 45 redirect_to :action => :index, :q => params[:q], :filter => params[:filter]
46 46 end
47 47  
48   -
49 48 def destroy_user
50 49 if request.post?
51 50 person = environment.people.find_by id: params[:id]
... ... @@ -58,7 +57,6 @@ class UsersController &lt; AdminController
58 57 redirect_to :action => :index, :q => params[:q], :filter => params[:filter]
59 58 end
60 59  
61   -
62 60 def download
63 61 respond_to do |format|
64 62 format.html
... ... @@ -87,8 +85,11 @@ class UsersController &lt; AdminController
87 85 end
88 86  
89 87 def send_mail
90   - @mailing = environment.mailings.build(params[:mailing])
91 88 if request.post?
  89 + @mailing = environment.mailings.build(params[:mailing])
  90 + @mailing.recipients_roles = []
  91 + @mailing.recipients_roles << "profile_admin" if params[:recipients][:profile_admins].include?("true")
  92 + @mailing.recipients_roles << "environment_administrator" if params[:recipients][:env_admins].include?("true")
92 93 @mailing.locale = locale
93 94 @mailing.person = user
94 95 if @mailing.save
... ...
app/mailers/environment_mailing.rb
1 1 class EnvironmentMailing < Mailing
2 2  
  3 + settings_items :recipients_roles, :type => :array
  4 + attr_accessible :recipients_roles
  5 +
3 6 def recipients(offset=0, limit=100)
4   - source.people.order(:id).offset(offset).limit(limit)
  7 + recipients_by_role.order(:id).offset(offset).limit(limit)
5 8 .joins("LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)")
6 9 .where("m.person_id" => nil)
7 10 end
8 11  
  12 + def recipients_by_role
  13 + if recipients_roles.nil?
  14 + source.people
  15 + else
  16 + roles = Environment::Role.where("key in (?)", self.recipients_roles)
  17 + Person.by_role(roles).where(environment_id: self.source_id)
  18 + end
  19 + end
  20 +
9 21 def each_recipient
10 22 offset = 0
11 23 limit = 100
... ...
app/views/users/_index_buttons.html.erb
1 1 <% button_bar do %>
2 2 <%= button :'text-plain', _('User list as [CSV]'), :action => 'download.csv' %>
3 3 <%= button :'text-html', _('User list as [XML]'), :action => 'download.xml' %>
4   - <%= button :send, _('Send e-mail to all users'), :action => 'send_mail' %>
  4 + <%= button :send, _('Send e-mail to users'), :action => 'send_mail' %>
5 5 <%= button :back, _('Back'), :controller => 'admin_panel' %>
6 6 <% end %>
... ...
app/views/users/send_mail.html.erb
... ... @@ -3,10 +3,19 @@
3 3 <%= error_messages_for :mailing %>
4 4  
5 5 <%= render :file => 'shared/tiny_mce' %>
6   -
7 6 <%= form_for :mailing do |f| %>
  7 + <div class="recipients">
  8 + <%= label_tag(_("Recipients: "), nil, { class: "formlabel" }) %>
  9 + <%= labelled_radio_button(_('All Users'), :send_to, "all", true, { id: "send_to_all" }) %></br>
  10 + <%= labelled_radio_button(_('Only Admins'), :send_to, "admins" , false, { id: "send_to_admins" }) %><br>
  11 + <div class="recipients-checkboxes">
  12 + <%= labelled_check_box(_('Environment Admins'), 'recipients[env_admins]', true, false, { disabled: true, id: "env_admins" }) %>
  13 + <%= labelled_check_box(_('Profile Admins'), 'recipients[profile_admins]', true, false, { disabled: true, id: "profile_admins" }) %>
  14 + </div>
  15 + </div>
8 16 <%= labelled_form_field(_('Subject:'), f.text_field(:subject)) %>
9 17 <%= labelled_form_field(_('Body:'), f.text_area(:body, :class => 'mceEditor')) %>
10 18 <%= submit_button(:send, _('Send')) %>
11 19 <%= button :cancel, _('Cancel e-mail'), :controller => 'users' %>
12 20 <% end %>
  21 +<%= javascript_include_tag "send_email.js" %>
... ...
features/send_email_to_environment_members.feature
... ... @@ -43,3 +43,29 @@ Feature: send emails to environment members users
43 43 Then I should be on /admin/users/send_mail
44 44 When I follow "Cancel e-mail"
45 45 Then I should be on /admin/users
  46 +
  47 + Scenario: Should display recipients options
  48 + Given I am logged in as admin
  49 + And I go to /admin/users/send_mail
  50 + Then I should see "Recipients"
  51 + Then I should see "All Users"
  52 + Then I should see "Only Admins"
  53 + Then I should see "Environment Admins"
  54 + Then I should see "Profile Admins"
  55 +
  56 + Scenario: All users should be marked as default recipients
  57 + Given I am logged in as admin
  58 + And I go to /admin/users/send_mail
  59 + Then the "send_to_all" radio button should be checked
  60 + Then the "send_to_admins" radio button should not be checked
  61 +
  62 + @selenium
  63 + Scenario: Should disable checkboxes when recipients is set to All users
  64 + Given I am logged in as admin
  65 + And I go to /admin/users/send_mail
  66 + Then the field "#profile_admins" should be disabled
  67 + Then the field "#env_admins" should be disabled
  68 + When I choose "Only Admins"
  69 + Then the field "#profile_admins" should be enabled
  70 + Then the field "#env_admins" should be enabled
  71 +
... ...
public/javascripts/send_email.js 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +var all_users_radio_btn = document.getElementById("send_to_all");
  2 +var admins_radio_btn = document.getElementById("send_to_admins");
  3 +var env_admins_checkbox = document.getElementById("env_admins");
  4 +var profile_admins_checkbox = document.getElementById("profile_admins");
  5 +
  6 +admins_radio_btn.onchange = function() {
  7 + change_checkbox_state("admins");
  8 +};
  9 +
  10 +all_users_radio_btn.onchange = function() {
  11 + change_checkbox_state("all_users");
  12 +
  13 +};
  14 +
  15 +
  16 +function change_checkbox_state (recipients) {
  17 + if(recipients == "all_users"){
  18 + env_admins_checkbox.disabled = true;
  19 + profile_admins_checkbox.disabled = true;
  20 + }else {
  21 + env_admins_checkbox.disabled = false;
  22 + profile_admins_checkbox.disabled = false;
  23 + }
  24 +
  25 +};
... ...
test/functional/users_controller_test.rb
... ... @@ -141,4 +141,36 @@ class UsersControllerTest &lt; ActionController::TestCase
141 141 assert_redirected_to :action => 'index'
142 142 end
143 143  
  144 + should 'redirect to index after send email with success' do
  145 + post :send_mail, mailing: { subject: "Subject", body: "Body" }, recipients: { profile_admins: "false", env_admins: "false" }
  146 + assert_redirected_to :action => 'index'
  147 + assert_match /The e-mails are being sent/, session[:notice]
  148 + end
  149 +
  150 + should 'mailing recipients_roles should be empty when none is set' do
  151 + post :send_mail, mailing: { subject: "Subject", body: "Body" }, recipients: { profile_admins: "false", env_admins: "false" }
  152 + mailing = EnvironmentMailing.last
  153 + assert_equal mailing.recipients_roles, []
  154 + end
  155 +
  156 + should 'mailing recipients_roles should be set correctly' do
  157 + post :send_mail, mailing: { subject: "Subject", body: "Body" }, recipients: { profile_admins: "true", env_admins: "true" }
  158 + mailing = EnvironmentMailing.last
  159 + assert_equal mailing.recipients_roles, ["profile_admin", "environment_administrator"]
  160 + end
  161 +
  162 + should 'send mail to admins recipients' do
  163 + admin_user = create_user('new_admin').person
  164 + admin_user_2 = create_user('new_admin_2').person
  165 +
  166 + environment.add_admin admin_user
  167 + environment.add_admin admin_user_2
  168 +
  169 + assert_equal 2, environment.admins.count
  170 + assert_difference "MailingSent.count", 2 do
  171 + post :send_mail, mailing: { subject: "UnB", body: "Hail UnB" }, recipients: { profile_admins: "false", env_admins: "true" }
  172 + process_delayed_job_queue
  173 + end
  174 + end
  175 +
144 176 end
... ...
test/unit/environment_mailing_test.rb
... ... @@ -82,6 +82,49 @@ class EnvironmentMailingTest &lt; ActiveSupport::TestCase
82 82 assert_equal [person_1], mailing.recipients(0, 1)
83 83 end
84 84  
  85 + should 'return all environment admins when recipients_roles is set to environment_administrator' do
  86 + environment.add_admin person_1
  87 +
  88 + mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
  89 + mailing.recipients_roles = ["environment_administrator"]
  90 + mailing.save
  91 +
  92 + assert_equivalent(environment.admins, mailing.recipients)
  93 + end
  94 +
  95 + should 'return all people with role profile_admin when recipients_roles is set to profile_admin' do
  96 + environment.add_admin person_1
  97 +
  98 + mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
  99 + mailing.recipients_roles = ["profile_admin"]
  100 + mailing.save
  101 + role = Role.find_by(key: 'profile_admin', environment_id: environment)
  102 + profile_admins = Person.by_role(role).where(environment_id: environment)
  103 +
  104 + assert_equivalent(profile_admins, mailing.recipients)
  105 + end
  106 +
  107 + should 'return all people when recipients_roles is not set' do
  108 + environment.add_admin person_1
  109 +
  110 + mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
  111 + mailing.save
  112 +
  113 + assert_equivalent(environment.people, mailing.recipients)
  114 + end
  115 +
  116 + should 'return profile_admins and environment admins when both roles are set as recipients' do
  117 + environment.add_admin person_1
  118 +
  119 + mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
  120 + mailing.recipients_roles = ["profile_admin", "environment_administrator"]
  121 + mailing.save
  122 + role = Role.find_by(key: 'profile_admin', environment_id: environment)
  123 + profile_admins = Person.by_role(role).where(environment_id: environment)
  124 +
  125 + assert_equivalent(profile_admins+environment.admins, mailing.recipients)
  126 + end
  127 +
85 128 should 'return true if already sent mailing to a recipient' do
86 129 mailing = create_mailing(environment, :person => person_1)
87 130 process_delayed_job_queue
... ...