Commit 091011fb2091a58530be6910222e9a0d1ff42e0d

Authored by Moises Machado
Committed by Antonio Terceiro
1 parent 0261c5e4

ActionItem900: added join community popup

  added an evironment feature to track the join commnity popup
  made the pupup appear in the rigth context
  added ask_to_join? method to person
  moved join to profile controller and added the popup
  added the popup window
app/controllers/my_profile/memberships_controller.rb
@@ -6,15 +6,6 @@ class MembershipsController < MyProfileController @@ -6,15 +6,6 @@ class MembershipsController < MyProfileController
6 @memberships = profile.memberships 6 @memberships = profile.memberships
7 end 7 end
8 8
9 - def join  
10 - @to_join = Profile.find(params[:id])  
11 - if request.post? && params[:confirmation]  
12 - @to_join.add_member(profile)  
13 - flash[:notice] = _('%s administrator still needs to accept you as member.') % @to_join.name if @to_join.closed?  
14 - redirect_to @to_join.url  
15 - end  
16 - end  
17 -  
18 def leave 9 def leave
19 @to_leave = Profile.find(params[:id]) 10 @to_leave = Profile.find(params[:id])
20 11
app/controllers/public/profile_controller.rb
@@ -2,6 +2,7 @@ class ProfileController < PublicController @@ -2,6 +2,7 @@ class ProfileController < PublicController
2 2
3 needs_profile 3 needs_profile
4 before_filter :check_access_to_profile 4 before_filter :check_access_to_profile
  5 + before_filter :login_required, :only => [:join, :refuse_join]
5 6
6 helper TagsHelper 7 helper TagsHelper
7 8
@@ -42,6 +43,21 @@ class ProfileController < PublicController @@ -42,6 +43,21 @@ class ProfileController < PublicController
42 @articles = profile.top_level_articles 43 @articles = profile.top_level_articles
43 end 44 end
44 45
  46 + def join
  47 + if request.post? && params[:confirmation]
  48 + profile.add_member(current_user.person)
  49 + flash[:notice] = _('%s administrator still needs to accept you as member.') % profile.name if profile.closed?
  50 + redirect_to profile.url
  51 + end
  52 + end
  53 +
  54 + def refuse_join
  55 + p = current_user.person
  56 + p.refused_communities << profile
  57 + p.save
  58 + redirect_to profile.url
  59 + end
  60 +
45 protected 61 protected
46 62
47 def check_access_to_profile 63 def check_access_to_profile
app/helpers/application_helper.rb
@@ -761,4 +761,11 @@ module ApplicationHelper @@ -761,4 +761,11 @@ module ApplicationHelper
761 end 761 end
762 end 762 end
763 763
  764 + def ask_to_join?
  765 + return if environment.enabled?(:disable_join_community_popup)
  766 + return unless profile && profile.kind_of?(Community)
  767 + return true unless logged_in?
  768 + user.ask_to_join?(profile)
  769 + end
  770 +
764 end 771 end
app/models/environment.rb
@@ -37,6 +37,7 @@ class Environment &lt; ActiveRecord::Base @@ -37,6 +37,7 @@ class Environment &lt; ActiveRecord::Base
37 'disable_select_city_for_contact' => _('Disable state/city select for contact form'), 37 'disable_select_city_for_contact' => _('Disable state/city select for contact form'),
38 'disable_contact_person' => _('Disable contact for people'), 38 'disable_contact_person' => _('Disable contact for people'),
39 'disable_contact_community' => _('Disable contact for groups/communities'), 39 'disable_contact_community' => _('Disable contact for groups/communities'),
  40 + 'disable_join_community_popup' => _('Disable the popup that ask to join a group/community'),
40 } 41 }
41 end 42 end
42 43
app/models/person.rb
@@ -219,4 +219,15 @@ class Person &lt; Profile @@ -219,4 +219,15 @@ class Person &lt; Profile
219 self.friends.include?(person) 219 self.friends.include?(person)
220 end 220 end
221 221
  222 + has_and_belongs_to_many :refused_communities, :class_name => 'Community', :join_table => 'refused_join_community'
  223 +
  224 + def ask_to_join?(community)
  225 + return false if memberships.include?(community)
  226 + !refused_communities.include?(community)
  227 + end
  228 +
  229 + def refuse_join(community)
  230 + refused_communities << community
  231 + end
  232 +
222 end 233 end
app/views/blocks/profile_info_actions/community.rhtml
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 </li> 7 </li>
8 <% else %> 8 <% else %>
9 <li> 9 <li>
10 - <%= link_to content_tag('span', _('Join')), { :profile => user.identifier, :controller => 'memberships', :action => 'join', :id => profile.id }, :class => 'button with-text icon-add', :title => _('Join this community') %> 10 + <%= link_to content_tag('span', _('Join')), { :profile => profile.identifier, :controller => 'profile', :action => 'join' }, :class => 'button with-text icon-add', :title => _('Join this community') %>
11 </li> 11 </li>
12 <% end %> 12 <% end %>
13 13
app/views/layouts/application.rhtml
@@ -187,5 +187,9 @@ @@ -187,5 +187,9 @@
187 <%= render :file => 'shared/theme_test_panel' %> 187 <%= render :file => 'shared/theme_test_panel' %>
188 <% end %> 188 <% end %>
189 189
  190 + <% if ask_to_join? %>
  191 + <%= render :file => 'shared/join_community_popup' %>
  192 + <% end %>
  193 +
190 </body> 194 </body>
191 </html> 195 </html>
app/views/memberships/join.rhtml
@@ -1,11 +0,0 @@ @@ -1,11 +0,0 @@
1 -<h1><%= _('Joining %s') % @to_join.name %></h1>  
2 -  
3 -<p>  
4 -<%= _('Are you sure you want to join %s?') % @to_join.name %>  
5 -</p>  
6 -  
7 -<% form_tag do %>  
8 - <%= hidden_field_tag(:confirmation, 1) %>  
9 - <%= submit_button(:ok, _("Yes, I want to join.") % @to_join.name) %>  
10 - <%= button(:cancel, _("No, I don't want."), :action => 'index') %>  
11 -<% end %>  
app/views/profile/join.rhtml 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +<h1><%= _('Joining %s') % profile.name %></h1>
  2 +
  3 +<p>
  4 +<%= _('Are you sure you want to join %s?') % profile.name %>
  5 +</p>
  6 +
  7 +<% form_tag do %>
  8 + <%= hidden_field_tag(:confirmation, 1) %>
  9 + <%= submit_button(:ok, _("Yes, I want to join.") % profile.name) %>
  10 + <%= button(:cancel, _("No, I don't want."), :action => 'index') %>
  11 +<% end %>
app/views/shared/join_community_popup.rhtml 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +<div id='join-community-popup'>
  2 + <h3> <%= __('Do you want to join this community?') %> </h3>
  3 +
  4 + <% button_bar do %>
  5 + <%= button(:ok, _('Yes'), :controller => 'profile', :action => 'join', :profile => profile.identifier) %>
  6 + <%= button_to_function(:cancel, _('Not now'), "$('join-community-popup').hide()") %>
  7 + <%= button(:cancel, _('No and don\'t ask again'), :controller => 'profile', :action => 'refuse_join', :profile => profile.identifier) %>
  8 + <% end %>
  9 +</div>
  10 +<%= draggable_element('join-community-popup') %>
db/migrate/060_add_refuse_join_table.rb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +class AddRefuseJoinTable < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :refused_join_community, :id => false do |t|
  4 + t.integer :person_id
  5 + t.integer :community_id
  6 + end
  7 + end
  8 +
  9 + def self.down
  10 + drop_table :refused_join_community
  11 + end
  12 +end
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 # 9 #
10 # It's strongly recommended to check this file into your version control system. 10 # It's strongly recommended to check this file into your version control system.
11 11
12 -ActiveRecord::Schema.define(:version => 59) do 12 +ActiveRecord::Schema.define(:version => 60) do
13 13
14 create_table "article_versions", :force => true do |t| 14 create_table "article_versions", :force => true do |t|
15 t.integer "article_id" 15 t.integer "article_id"
@@ -237,6 +237,11 @@ ActiveRecord::Schema.define(:version =&gt; 59) do @@ -237,6 +237,11 @@ ActiveRecord::Schema.define(:version =&gt; 59) do
237 237
238 add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" 238 add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id"
239 239
  240 + create_table "refused_join_community", :id => false, :force => true do |t|
  241 + t.integer "person_id"
  242 + t.integer "community_id"
  243 + end
  244 +
240 create_table "region_validators", :id => false, :force => true do |t| 245 create_table "region_validators", :id => false, :force => true do |t|
241 t.integer "region_id" 246 t.integer "region_id"
242 t.integer "organization_id" 247 t.integer "organization_id"
public/stylesheets/common.css
@@ -419,6 +419,26 @@ div.pending-tasks { @@ -419,6 +419,26 @@ div.pending-tasks {
419 margin: 1em; 419 margin: 1em;
420 } 420 }
421 421
  422 +/* join community popup */
  423 +#join-community-popup {
  424 + z-index: 1000;
  425 + position: absolute;
  426 + width: 350px;
  427 + height: 180;
  428 + right: 50px;
  429 + top: 50px;
  430 + background: white;
  431 + border: 2px solid black;
  432 +}
  433 +
  434 +#join-community-popup {
  435 + text-align: center;
  436 + cursor: move;
  437 +}
  438 +#join-community-popup .button-bar {
  439 + margin: 1em;
  440 +}
  441 +
422 #content #not-found, 442 #content #not-found,
423 #content #access-denied { 443 #content #access-denied {
424 padding: 20px; 444 padding: 20px;
test/functional/memberships_controller_test.rb
@@ -27,32 +27,13 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase @@ -27,32 +27,13 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase
27 def test_valid_xhtml 27 def test_valid_xhtml
28 assert_valid_xhtml 28 assert_valid_xhtml
29 end 29 end
30 - 30 +
31 should 'list current memberships' do 31 should 'list current memberships' do
32 get :index, :profile => profile.identifier 32 get :index, :profile => profile.identifier
33 33
34 assert_kind_of Array, assigns(:memberships) 34 assert_kind_of Array, assigns(:memberships)
35 end 35 end
36 36
37 - should 'present confirmation before joining a profile' do  
38 - community = Community.create!(:name => 'my test community')  
39 - get :join, :profile => profile.identifier, :id => community.id  
40 -  
41 - assert_response :success  
42 - assert_template 'join'  
43 - end  
44 -  
45 - should 'actually join profile' do  
46 - community = Community.create!(:name => 'my test community')  
47 - post :join, :profile => profile.identifier, :id => community.id, :confirmation => '1'  
48 -  
49 - assert_response :redirect  
50 - assert_redirected_to community.url  
51 -  
52 - profile = Profile.find(@profile.id)  
53 - assert profile.memberships.include?(community), 'profile should be actually added to the community'  
54 - end  
55 -  
56 should 'present new community form' do 37 should 'present new community form' do
57 get :new_community, :profile => profile.identifier 38 get :new_community, :profile => profile.identifier
58 assert_response :success 39 assert_response :success
@@ -143,13 +124,6 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase @@ -143,13 +124,6 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase
143 assert_not_includes profile.memberships, community 124 assert_not_includes profile.memberships, community
144 end 125 end
145 126
146 - should 'create task when join to closed organization' do  
147 - community = Community.create!(:name => 'my test community', :closed => true)  
148 - assert_difference AddMember, :count do  
149 - post :join, :profile => profile.identifier, :id => community.id, :confirmation => '1'  
150 - end  
151 - end  
152 -  
153 should 'current user is added as admin after create new community' do 127 should 'current user is added as admin after create new community' do
154 post :new_community, :profile => profile.identifier, :community => { :name => 'My shiny new community', :description => 'This is a community devoted to anything interesting we find in the internet '} 128 post :new_community, :profile => profile.identifier, :community => { :name => 'My shiny new community', :description => 'This is a community devoted to anything interesting we find in the internet '}
155 assert_equal Profile::Roles.admin, profile.find_roles(Community.find_by_identifier('my-shiny-new-community')).first.role 129 assert_equal Profile::Roles.admin, profile.find_roles(Community.find_by_identifier('my-shiny-new-community')).first.role
test/functional/profile_controller_test.rb
@@ -67,7 +67,7 @@ class ProfileControllerTest &lt; Test::Unit::TestCase @@ -67,7 +67,7 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
67 login_as(@profile.identifier) 67 login_as(@profile.identifier)
68 community = Community.create!(:name => 'my test community') 68 community = Community.create!(:name => 'my test community')
69 get :index, :profile => community.identifier 69 get :index, :profile => community.identifier
70 - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/join/#{community.id}" } 70 + assert_tag :tag => 'a', :attributes => { :href => "/profile/#{community.identifier}/join" }
71 end 71 end
72 72
73 should 'not show Join This Community button for member users' do 73 should 'not show Join This Community button for member users' do
@@ -404,4 +404,57 @@ class ProfileControllerTest &lt; Test::Unit::TestCase @@ -404,4 +404,57 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
404 assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{community.identifier}/new" } 404 assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{community.identifier}/new" }
405 end 405 end
406 406
  407 + should 'present confirmation before joining a profile' do
  408 + community = Community.create!(:name => 'my test community')
  409 + login_as @profile.identifier
  410 + get :join, :profile => community.identifier
  411 +
  412 + assert_response :success
  413 + assert_template 'join'
  414 + end
  415 +
  416 + should 'actually join profile' do
  417 + community = Community.create!(:name => 'my test community')
  418 + login_as @profile.identifier
  419 + post :join, :profile => community.identifier, :confirmation => '1'
  420 +
  421 + assert_response :redirect
  422 + assert_redirected_to community.url
  423 +
  424 + profile = Profile.find(@profile.id)
  425 + assert profile.memberships.include?(community), 'profile should be actually added to the community'
  426 + end
  427 +
  428 + should 'create task when join to closed organization' do
  429 + community = Community.create!(:name => 'my test community', :closed => true)
  430 + login_as @profile.identifier
  431 + assert_difference AddMember, :count do
  432 + post :join, :profile => community.identifier, :confirmation => '1'
  433 + end
  434 + end
  435 +
  436 + should 'require login to join community' do
  437 + community = Community.create!(:name => 'my test community', :closed => true)
  438 + get :join, :profile => community.identifier
  439 +
  440 + assert_redirected_to :controller => 'account', :action => 'login'
  441 + end
  442 +
  443 + should 'require login to refuse join community' do
  444 + community = Community.create!(:name => 'my test community', :closed => true)
  445 + get :refuse_join, :profile => community.identifier
  446 +
  447 + assert_redirected_to :controller => 'account', :action => 'login'
  448 + end
  449 +
  450 + should 'register join refusal' do
  451 + community = Community.create!(:name => 'my test community', :closed => true)
  452 + login_as @profile.identifier
  453 +
  454 + get :refuse_join, :profile => community.identifier
  455 +
  456 + p = Person.find(@profile.id)
  457 + assert_includes p.refused_communities, community
  458 + end
  459 +
407 end 460 end
test/unit/application_helper_test.rb
@@ -332,6 +332,90 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase @@ -332,6 +332,90 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase
332 assert_equal '<span>SIGNUP_FIELD</span>', optional_field(profile, 'field', 'SIGNUP_FIELD') 332 assert_equal '<span>SIGNUP_FIELD</span>', optional_field(profile, 'field', 'SIGNUP_FIELD')
333 end 333 end
334 334
  335 + should 'not ask_to_join unless profile defined' do
  336 + e = Environment.default
  337 + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false)
  338 + stubs(:environment).returns(e)
  339 +
  340 + stubs(:profile).returns(nil)
  341 + assert ! ask_to_join?
  342 + end
  343 +
  344 + should 'not ask_to_join unless profile is community' do
  345 + e = Environment.default
  346 + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false)
  347 + stubs(:environment).returns(e)
  348 +
  349 + p = create_user('test_user').person
  350 + stubs(:profile).returns(p)
  351 + assert ! ask_to_join?
  352 + end
  353 +
  354 + should 'ask_to_join if its not logged and in a community' do
  355 + e = Environment.default
  356 + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false)
  357 + stubs(:environment).returns(e)
  358 +
  359 + c = Community.create(:name => 'test_comm', :identifier => 'test_comm')
  360 + stubs(:profile).returns(c)
  361 + stubs(:logged_in?).returns(false)
  362 + assert ask_to_join?
  363 + end
  364 +
  365 + should 'ask_to_join if user say so' do
  366 + e = Environment.default
  367 + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false)
  368 + stubs(:environment).returns(e)
  369 +
  370 + c = Community.create(:name => 'test_comm', :identifier => 'test_comm')
  371 + stubs(:profile).returns(c)
  372 + stubs(:logged_in?).returns(true)
  373 + p = create_user('test_user').person
  374 + p.stubs(:ask_to_join?).with(c).returns(true)
  375 + stubs(:user).returns(p)
  376 +
  377 + assert ask_to_join?
  378 + end
  379 +
  380 + should 'not ask_to_join if user say no' do
  381 + e = Environment.default
  382 + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false)
  383 + stubs(:environment).returns(e)
  384 + c = Community.create(:name => 'test_comm', :identifier => 'test_comm')
  385 + stubs(:profile).returns(c)
  386 + stubs(:logged_in?).returns(true)
  387 + p = create_user('test_user').person
  388 + p.stubs(:ask_to_join?).with(c).returns(false)
  389 + stubs(:user).returns(p)
  390 +
  391 + assert !ask_to_join?
  392 + end
  393 +
  394 + should 'not ask_to_join if environment say no even if its not logged and in a community' do
  395 + e = Environment.default
  396 + e.stubs(:enabled?).with(:disable_join_community_popup).returns(true)
  397 + stubs(:environment).returns(e)
  398 + c = Community.create(:name => 'test_comm', :identifier => 'test_comm')
  399 + stubs(:profile).returns(c)
  400 + stubs(:logged_in?).returns(false)
  401 + assert !ask_to_join?
  402 + end
  403 +
  404 + should 'not ask_to_join if environment say no even if user say so' do
  405 + e = Environment.default
  406 + e.stubs(:enabled?).with(:disable_join_community_popup).returns(true)
  407 + stubs(:environment).returns(e)
  408 + c = Community.create(:name => 'test_comm', :identifier => 'test_comm')
  409 + stubs(:profile).returns(c)
  410 + stubs(:logged_in?).returns(true)
  411 + p = create_user('test_user').person
  412 + p.stubs(:ask_to_join?).with(c).returns(true)
  413 + stubs(:user).returns(p)
  414 +
  415 + assert !ask_to_join?
  416 + end
  417 +
  418 +
335 protected 419 protected
336 420
337 def url_for(args = {}) 421 def url_for(args = {})
test/unit/folder_test.rb
@@ -47,7 +47,7 @@ class FolderTest &lt; ActiveSupport::TestCase @@ -47,7 +47,7 @@ class FolderTest &lt; ActiveSupport::TestCase
47 end 47 end
48 48
49 should 'can display hits' do 49 should 'can display hits' do
50 - profile = create_user('test_user').person 50 + profile = create_user('testuser').person
51 a = Folder.create!(:name => 'Test article', :profile => profile) 51 a = Folder.create!(:name => 'Test article', :profile => profile)
52 assert_equal false, a.can_display_hits? 52 assert_equal false, a.can_display_hits?
53 end 53 end
test/unit/person_test.rb
@@ -495,4 +495,21 @@ class PersonTest &lt; Test::Unit::TestCase @@ -495,4 +495,21 @@ class PersonTest &lt; Test::Unit::TestCase
495 assert !p1.is_a_friend?(p2) 495 assert !p1.is_a_friend?(p2)
496 end 496 end
497 497
  498 + should 'refuse join community' do
  499 + p = create_user('test_user').person
  500 + c = Community.create!(:name => 'Test community', :identifier => 'test_community')
  501 +
  502 + assert p.ask_to_join?(c)
  503 + p.refuse_join(c)
  504 + assert !p.ask_to_join?(c)
  505 + end
  506 +
  507 + should 'not ask to join for a member' do
  508 + p = create_user('test_user').person
  509 + c = Community.create!(:name => 'Test community', :identifier => 'test_community')
  510 + c.add_member(p)
  511 +
  512 + assert !p.ask_to_join?(c)
  513 + end
  514 +
498 end 515 end