Commit 3cdf7126e9312a27d5b9292707e2f18a1094f7d2

Authored by Marcos Pereira
1 parent 80ebad1f

adds member selection to organization mailing

Signed-off-by: Gustavo Jaruga <darkshades@gmail.com>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
app/controllers/my_profile/profile_members_controller.rb
... ... @@ -10,10 +10,20 @@ class ProfileMembersController &lt; MyProfileController
10 10  
11 11 @data[:members] = profile.members_by(field,@filters[:name]).by_role(@filters[:roles])
12 12 session[:members_filtered] = @data[:members].map{|m|m.id} if request.post?
13   - @data[:roles] = Profile::Roles.organization_member_roles(environment.id)
  13 + @data[:roles] = Profile::Roles.organization_member_roles(environment.id) | Profile::Roles.organization_custom_roles(environment.id, profile.id)
14 14  
15 15 end
16 16  
  17 + def send_mail
  18 + session[:members_filtered] = params[:members_filtered].select{|value| value!="0"}
  19 + if session[:members_filtered].present?
  20 + redirect_to :controller => :profile, :action => :send_mail
  21 + else
  22 + session[:notice] = _("Select at least one member.")
  23 + redirect_to :action => :index
  24 + end
  25 + end
  26 +
17 27 def update_roles
18 28 @roles = params[:roles] ? environment.roles.find(params[:roles].select{|r|!r.to_i.zero?}) : []
19 29 @roles = @roles.select{|r| r.has_kind?('Profile') }
... ...
app/mailers/organization_mailing.rb
... ... @@ -5,13 +5,14 @@ class OrganizationMailing &lt; Mailing
5 5 end
6 6  
7 7 def recipients(offset=0, limit=100)
  8 + result = source.members.order(:id).offset(offset).limit(limit)
  9 +
8 10 if data.present? and data.is_a?(Hash) and data[:members_filtered]
9   - result = source.members.where('profiles.id IN (?)', data[:members_filtered])
  11 + result = result.where('profiles.id IN (?)', data[:members_filtered])
10 12 end
11 13  
12 14 if result.blank?
13   - result = source.members.order(:id).offset(offset).limit(limit)
14   - .joins("LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)")
  15 + result = result.joins("LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)")
15 16 .where("m.person_id" => nil)
16 17 end
17 18 result
... ...
app/models/profile.rb
... ... @@ -49,6 +49,9 @@ class Profile &lt; ActiveRecord::Base
49 49 def self.organization_member_roles(env_id)
50 50 all_roles(env_id).select{ |r| r.key.match(/^profile_/) unless r.key.blank? || !r.profile_id.nil?}
51 51 end
  52 + def self.organization_custom_roles(env_id, profile_id)
  53 + all_roles(env_id).where('profile_id = ?', profile_id)
  54 + end
52 55 def self.all_roles(env_id)
53 56 Role.where(environment_id: env_id)
54 57 end
... ...
app/views/profile_members/_index_buttons.html.erb
... ... @@ -5,7 +5,7 @@
5 5 <%= button :person, _('Invite people to join'), :controller => 'invite', :action => 'invite_friends' %>
6 6 <% end %>
7 7 <% if profile.community? and user.has_permission?(:send_mail_to_members, profile) %>
8   - <%= button :send, _('Send e-mail to members'), :controller => 'profile', :action => 'send_mail' %>
  8 + <%= submit_button(:send, _('Send e-mail to members')) %>
9 9 <% end %>
10 10 <% @plugins.dispatch(:manage_members_extra_buttons).each do |plugin_button| %>
11 11 <%= button plugin_button[:icon], plugin_button[:title], plugin_button[:url] %>
... ...
app/views/profile_members/_members_list.html.erb
... ... @@ -2,17 +2,21 @@
2 2 <% collection = @collection == :profile_admins ? profile.admins : members %>
3 3 <% title = @title ? @title : _('Current members') %>
4 4 <% remove_action = @remove_action ? @remove_action : {:action => 'unassociate'} %>
  5 +<%= javascript_include_tag params[:controller] %>
5 6  
6 7 <h3><%= title %></h3>
7 8  
8 9 <table>
  10 + <col width="1">
9 11 <tr>
  12 + <th><%= check_box_tag 'checkbox-all', 1, false, :onClick => "toggle(this)" %></th>
10 13 <th><%= _('Member') %></th>
11 14 <th><%= _('Actions') %></th>
12 15 </tr>
13 16  
14 17 <% collection.each do |m| %>
15 18 <tr title="<%= m.name %>">
  19 + <td><%= labelled_check_box('', 'members_filtered[]', m.id.to_s, false, :id => 'checkbox-'+m.identifier) %></td>
16 20 <td><%= link_to_profile m.short_name, m.identifier, :title => m.name %> </td>
17 21 <td>
18 22 <div class="members-buttons-cell">
... ...
app/views/profile_members/index.html.erb
1 1 <h1><%= h profile.short_name(50) %></h1>
2 2  
3   -<%= render :partial => 'index_buttons' %>
4   -
5 3 <%= render :partial => 'members_filter' %>
6 4  
7   -<div id="members-list">
8   - <%= render :partial => 'members_list' %>
9   -</div>
  5 +<%= form_tag 'profile_members/send_mail', :method => 'post' do %>
  6 + <div id="members-list">
  7 + <%= render :partial => 'members_list' %>
  8 + </div>
  9 +
  10 + <%= render :partial => 'index_buttons' %>
10 11  
11   -<%= render :partial => 'index_buttons' %>
  12 +<% end %>
... ...
features/send_email_to_organization_members.feature
... ... @@ -31,7 +31,8 @@ Feature: send emails to organization members
31 31 Scenario: Send e-mail to members
32 32 Given I am logged in as "joaosilva"
33 33 And I go to Sample Community's members management
34   - And I follow "Send e-mail to members"
  34 + And I check "checkbox-manoel"
  35 + And I press "Send e-mail to members"
35 36 And I fill in "Subject" with "Hello, member!"
36 37 And I fill in "Body" with "We have some news"
37 38 When I press "Send"
... ... @@ -40,7 +41,8 @@ Feature: send emails to organization members
40 41 Scenario: Not send e-mail to members if subject is blank
41 42 Given I am logged in as "joaosilva"
42 43 And I go to Sample Community's members management
43   - And I follow "Send e-mail to members"
  44 + And I check "checkbox-manoel"
  45 + And I press "Send e-mail to members"
44 46 And I fill in "Body" with "We have some news"
45 47 When I press "Send"
46 48 Then I should be on /profile/sample-community/send_mail
... ... @@ -48,7 +50,8 @@ Feature: send emails to organization members
48 50 Scenario: Not send e-mail to members if body is blank
49 51 Given I am logged in as "joaosilva"
50 52 And I go to Sample Community's members management
51   - And I follow "Send e-mail to members"
  53 + And I check "checkbox-manoel"
  54 + And I press "Send e-mail to members"
52 55 And I fill in "Subject" with "Hello, user!"
53 56 When I press "Send"
54 57 Then I should be on /profile/sample-community/send_mail
... ... @@ -56,7 +59,8 @@ Feature: send emails to organization members
56 59 Scenario: Cancel creation of mailing
57 60 Given I am logged in as "joaosilva"
58 61 And I go to Sample Community's members management
59   - And I follow "Send e-mail to members"
  62 + And I check "checkbox-manoel"
  63 + And I press "Send e-mail to members"
60 64 When I follow "Cancel e-mail"
61 65 Then I should be on Sample Community's members management
62 66  
... ...
public/javascripts/profile_members.js
... ... @@ -14,5 +14,12 @@
14 14 });
15 15 }
16 16 });
  17 +})(jQuery);
17 18  
18   -})(jQuery);
19 19 \ No newline at end of file
  20 +
  21 +function toggle(source) {
  22 + checkboxes = document.getElementsByName('members_filtered[]');
  23 + for(var i=0, n=checkboxes.length;i<n;i++) {
  24 + checkboxes[i].checked = source.checked;
  25 + }
  26 +}
... ...
test/functional/profile_controller_test.rb
... ... @@ -1497,7 +1497,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1497 1497  
1498 1498 @request.session[:members_filtered] = [Profile.last.id+1]
1499 1499 post :send_mail, :profile => community.identifier, :mailing => {:subject => 'RUN!!', :body => 'Run to the hills!!'}
1500   - assert_equivalent community.members, OrganizationMailing.last.recipients
  1500 + assert_empty OrganizationMailing.last.recipients
1501 1501 end
1502 1502  
1503 1503 should 'save mailing' do
... ...
test/functional/profile_members_controller_test.rb
... ... @@ -196,7 +196,7 @@ class ProfileMembersControllerTest &lt; ActionController::TestCase
196 196 login_as :test_user
197 197  
198 198 get :index, :profile => community.identifier
199   - assert_tag :tag => 'a', :attributes => {:href => /send_mail/}
  199 + assert_tag :tag => 'input', :attributes => {:value => 'Send e-mail to members'}
200 200 end
201 201  
202 202 should 'not display send email to members if doesn\'t have the permission' do
... ...