From 091011fb2091a58530be6910222e9a0d1ff42e0d Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Tue, 27 Jan 2009 09:47:32 -0300 Subject: [PATCH] ActionItem900: added join community popup --- app/controllers/my_profile/memberships_controller.rb | 9 --------- app/controllers/public/profile_controller.rb | 16 ++++++++++++++++ app/helpers/application_helper.rb | 7 +++++++ app/models/environment.rb | 1 + app/models/person.rb | 11 +++++++++++ app/views/blocks/profile_info_actions/community.rhtml | 2 +- app/views/layouts/application.rhtml | 4 ++++ app/views/memberships/join.rhtml | 11 ----------- app/views/profile/join.rhtml | 11 +++++++++++ app/views/shared/join_community_popup.rhtml | 10 ++++++++++ db/migrate/060_add_refuse_join_table.rb | 12 ++++++++++++ db/schema.rb | 7 ++++++- public/stylesheets/common.css | 20 ++++++++++++++++++++ test/functional/memberships_controller_test.rb | 28 +--------------------------- test/functional/profile_controller_test.rb | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- test/unit/application_helper_test.rb | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/folder_test.rb | 2 +- test/unit/person_test.rb | 17 +++++++++++++++++ 18 files changed, 256 insertions(+), 51 deletions(-) delete mode 100644 app/views/memberships/join.rhtml create mode 100644 app/views/profile/join.rhtml create mode 100644 app/views/shared/join_community_popup.rhtml create mode 100644 db/migrate/060_add_refuse_join_table.rb diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index 74daf44..b7fb269 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -6,15 +6,6 @@ class MembershipsController < MyProfileController @memberships = profile.memberships end - def join - @to_join = Profile.find(params[:id]) - if request.post? && params[:confirmation] - @to_join.add_member(profile) - flash[:notice] = _('%s administrator still needs to accept you as member.') % @to_join.name if @to_join.closed? - redirect_to @to_join.url - end - end - def leave @to_leave = Profile.find(params[:id]) diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 1893333..44c5ca8 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -2,6 +2,7 @@ class ProfileController < PublicController needs_profile before_filter :check_access_to_profile + before_filter :login_required, :only => [:join, :refuse_join] helper TagsHelper @@ -42,6 +43,21 @@ class ProfileController < PublicController @articles = profile.top_level_articles end + def join + if request.post? && params[:confirmation] + profile.add_member(current_user.person) + flash[:notice] = _('%s administrator still needs to accept you as member.') % profile.name if profile.closed? + redirect_to profile.url + end + end + + def refuse_join + p = current_user.person + p.refused_communities << profile + p.save + redirect_to profile.url + end + protected def check_access_to_profile diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2617ad9..d43eb14 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -761,4 +761,11 @@ module ApplicationHelper end end + def ask_to_join? + return if environment.enabled?(:disable_join_community_popup) + return unless profile && profile.kind_of?(Community) + return true unless logged_in? + user.ask_to_join?(profile) + end + end diff --git a/app/models/environment.rb b/app/models/environment.rb index 601fe7e..f63abc1 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -37,6 +37,7 @@ class Environment < ActiveRecord::Base 'disable_select_city_for_contact' => _('Disable state/city select for contact form'), 'disable_contact_person' => _('Disable contact for people'), 'disable_contact_community' => _('Disable contact for groups/communities'), + 'disable_join_community_popup' => _('Disable the popup that ask to join a group/community'), } end diff --git a/app/models/person.rb b/app/models/person.rb index 693e818..9c3890c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -219,4 +219,15 @@ class Person < Profile self.friends.include?(person) end + has_and_belongs_to_many :refused_communities, :class_name => 'Community', :join_table => 'refused_join_community' + + def ask_to_join?(community) + return false if memberships.include?(community) + !refused_communities.include?(community) + end + + def refuse_join(community) + refused_communities << community + end + end diff --git a/app/views/blocks/profile_info_actions/community.rhtml b/app/views/blocks/profile_info_actions/community.rhtml index 422ea07..b4be4b2 100644 --- a/app/views/blocks/profile_info_actions/community.rhtml +++ b/app/views/blocks/profile_info_actions/community.rhtml @@ -7,7 +7,7 @@ <% else %>
  • - <%= 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') %> + <%= link_to content_tag('span', _('Join')), { :profile => profile.identifier, :controller => 'profile', :action => 'join' }, :class => 'button with-text icon-add', :title => _('Join this community') %>
  • <% end %> diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml index 7029d87..474111f 100644 --- a/app/views/layouts/application.rhtml +++ b/app/views/layouts/application.rhtml @@ -187,5 +187,9 @@ <%= render :file => 'shared/theme_test_panel' %> <% end %> + <% if ask_to_join? %> + <%= render :file => 'shared/join_community_popup' %> + <% end %> + diff --git a/app/views/memberships/join.rhtml b/app/views/memberships/join.rhtml deleted file mode 100644 index 6496a29..0000000 --- a/app/views/memberships/join.rhtml +++ /dev/null @@ -1,11 +0,0 @@ -

    <%= _('Joining %s') % @to_join.name %>

    - -

    -<%= _('Are you sure you want to join %s?') % @to_join.name %> -

    - -<% form_tag do %> - <%= hidden_field_tag(:confirmation, 1) %> - <%= submit_button(:ok, _("Yes, I want to join.") % @to_join.name) %> - <%= button(:cancel, _("No, I don't want."), :action => 'index') %> -<% end %> diff --git a/app/views/profile/join.rhtml b/app/views/profile/join.rhtml new file mode 100644 index 0000000..ebd44b0 --- /dev/null +++ b/app/views/profile/join.rhtml @@ -0,0 +1,11 @@ +

    <%= _('Joining %s') % profile.name %>

    + +

    +<%= _('Are you sure you want to join %s?') % profile.name %> +

    + +<% form_tag do %> + <%= hidden_field_tag(:confirmation, 1) %> + <%= submit_button(:ok, _("Yes, I want to join.") % profile.name) %> + <%= button(:cancel, _("No, I don't want."), :action => 'index') %> +<% end %> diff --git a/app/views/shared/join_community_popup.rhtml b/app/views/shared/join_community_popup.rhtml new file mode 100644 index 0000000..fdfcdd5 --- /dev/null +++ b/app/views/shared/join_community_popup.rhtml @@ -0,0 +1,10 @@ +
    +

    <%= __('Do you want to join this community?') %>

    + + <% button_bar do %> + <%= button(:ok, _('Yes'), :controller => 'profile', :action => 'join', :profile => profile.identifier) %> + <%= button_to_function(:cancel, _('Not now'), "$('join-community-popup').hide()") %> + <%= button(:cancel, _('No and don\'t ask again'), :controller => 'profile', :action => 'refuse_join', :profile => profile.identifier) %> + <% end %> +
    +<%= draggable_element('join-community-popup') %> diff --git a/db/migrate/060_add_refuse_join_table.rb b/db/migrate/060_add_refuse_join_table.rb new file mode 100644 index 0000000..98ced06 --- /dev/null +++ b/db/migrate/060_add_refuse_join_table.rb @@ -0,0 +1,12 @@ +class AddRefuseJoinTable < ActiveRecord::Migration + def self.up + create_table :refused_join_community, :id => false do |t| + t.integer :person_id + t.integer :community_id + end + end + + def self.down + drop_table :refused_join_community + end +end diff --git a/db/schema.rb b/db/schema.rb index a4d4c22..611e511 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 59) do +ActiveRecord::Schema.define(:version => 60) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -237,6 +237,11 @@ ActiveRecord::Schema.define(:version => 59) do add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" + create_table "refused_join_community", :id => false, :force => true do |t| + t.integer "person_id" + t.integer "community_id" + end + create_table "region_validators", :id => false, :force => true do |t| t.integer "region_id" t.integer "organization_id" diff --git a/public/stylesheets/common.css b/public/stylesheets/common.css index fc669cf..ca8e8b8 100644 --- a/public/stylesheets/common.css +++ b/public/stylesheets/common.css @@ -419,6 +419,26 @@ div.pending-tasks { margin: 1em; } +/* join community popup */ +#join-community-popup { + z-index: 1000; + position: absolute; + width: 350px; + height: 180; + right: 50px; + top: 50px; + background: white; + border: 2px solid black; +} + +#join-community-popup { + text-align: center; + cursor: move; +} +#join-community-popup .button-bar { + margin: 1em; +} + #content #not-found, #content #access-denied { padding: 20px; diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index edcdbbc..db1702d 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -27,32 +27,13 @@ class MembershipsControllerTest < Test::Unit::TestCase def test_valid_xhtml assert_valid_xhtml end - + should 'list current memberships' do get :index, :profile => profile.identifier assert_kind_of Array, assigns(:memberships) end - should 'present confirmation before joining a profile' do - community = Community.create!(:name => 'my test community') - get :join, :profile => profile.identifier, :id => community.id - - assert_response :success - assert_template 'join' - end - - should 'actually join profile' do - community = Community.create!(:name => 'my test community') - post :join, :profile => profile.identifier, :id => community.id, :confirmation => '1' - - assert_response :redirect - assert_redirected_to community.url - - profile = Profile.find(@profile.id) - assert profile.memberships.include?(community), 'profile should be actually added to the community' - end - should 'present new community form' do get :new_community, :profile => profile.identifier assert_response :success @@ -143,13 +124,6 @@ class MembershipsControllerTest < Test::Unit::TestCase assert_not_includes profile.memberships, community end - should 'create task when join to closed organization' do - community = Community.create!(:name => 'my test community', :closed => true) - assert_difference AddMember, :count do - post :join, :profile => profile.identifier, :id => community.id, :confirmation => '1' - end - end - should 'current user is added as admin after create new community' do 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 '} assert_equal Profile::Roles.admin, profile.find_roles(Community.find_by_identifier('my-shiny-new-community')).first.role diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 1d8d94c..496d1ff 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -67,7 +67,7 @@ class ProfileControllerTest < Test::Unit::TestCase login_as(@profile.identifier) community = Community.create!(:name => 'my test community') get :index, :profile => community.identifier - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/join/#{community.id}" } + assert_tag :tag => 'a', :attributes => { :href => "/profile/#{community.identifier}/join" } end should 'not show Join This Community button for member users' do @@ -404,4 +404,57 @@ class ProfileControllerTest < Test::Unit::TestCase assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{community.identifier}/new" } end + should 'present confirmation before joining a profile' do + community = Community.create!(:name => 'my test community') + login_as @profile.identifier + get :join, :profile => community.identifier + + assert_response :success + assert_template 'join' + end + + should 'actually join profile' do + community = Community.create!(:name => 'my test community') + login_as @profile.identifier + post :join, :profile => community.identifier, :confirmation => '1' + + assert_response :redirect + assert_redirected_to community.url + + profile = Profile.find(@profile.id) + assert profile.memberships.include?(community), 'profile should be actually added to the community' + end + + should 'create task when join to closed organization' do + community = Community.create!(:name => 'my test community', :closed => true) + login_as @profile.identifier + assert_difference AddMember, :count do + post :join, :profile => community.identifier, :confirmation => '1' + end + end + + should 'require login to join community' do + community = Community.create!(:name => 'my test community', :closed => true) + get :join, :profile => community.identifier + + assert_redirected_to :controller => 'account', :action => 'login' + end + + should 'require login to refuse join community' do + community = Community.create!(:name => 'my test community', :closed => true) + get :refuse_join, :profile => community.identifier + + assert_redirected_to :controller => 'account', :action => 'login' + end + + should 'register join refusal' do + community = Community.create!(:name => 'my test community', :closed => true) + login_as @profile.identifier + + get :refuse_join, :profile => community.identifier + + p = Person.find(@profile.id) + assert_includes p.refused_communities, community + end + end diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index e6a71c8..505643c 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -332,6 +332,90 @@ class ApplicationHelperTest < Test::Unit::TestCase assert_equal 'SIGNUP_FIELD', optional_field(profile, 'field', 'SIGNUP_FIELD') end + should 'not ask_to_join unless profile defined' do + e = Environment.default + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false) + stubs(:environment).returns(e) + + stubs(:profile).returns(nil) + assert ! ask_to_join? + end + + should 'not ask_to_join unless profile is community' do + e = Environment.default + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false) + stubs(:environment).returns(e) + + p = create_user('test_user').person + stubs(:profile).returns(p) + assert ! ask_to_join? + end + + should 'ask_to_join if its not logged and in a community' do + e = Environment.default + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false) + stubs(:environment).returns(e) + + c = Community.create(:name => 'test_comm', :identifier => 'test_comm') + stubs(:profile).returns(c) + stubs(:logged_in?).returns(false) + assert ask_to_join? + end + + should 'ask_to_join if user say so' do + e = Environment.default + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false) + stubs(:environment).returns(e) + + c = Community.create(:name => 'test_comm', :identifier => 'test_comm') + stubs(:profile).returns(c) + stubs(:logged_in?).returns(true) + p = create_user('test_user').person + p.stubs(:ask_to_join?).with(c).returns(true) + stubs(:user).returns(p) + + assert ask_to_join? + end + + should 'not ask_to_join if user say no' do + e = Environment.default + e.stubs(:enabled?).with(:disable_join_community_popup).returns(false) + stubs(:environment).returns(e) + c = Community.create(:name => 'test_comm', :identifier => 'test_comm') + stubs(:profile).returns(c) + stubs(:logged_in?).returns(true) + p = create_user('test_user').person + p.stubs(:ask_to_join?).with(c).returns(false) + stubs(:user).returns(p) + + assert !ask_to_join? + end + + should 'not ask_to_join if environment say no even if its not logged and in a community' do + e = Environment.default + e.stubs(:enabled?).with(:disable_join_community_popup).returns(true) + stubs(:environment).returns(e) + c = Community.create(:name => 'test_comm', :identifier => 'test_comm') + stubs(:profile).returns(c) + stubs(:logged_in?).returns(false) + assert !ask_to_join? + end + + should 'not ask_to_join if environment say no even if user say so' do + e = Environment.default + e.stubs(:enabled?).with(:disable_join_community_popup).returns(true) + stubs(:environment).returns(e) + c = Community.create(:name => 'test_comm', :identifier => 'test_comm') + stubs(:profile).returns(c) + stubs(:logged_in?).returns(true) + p = create_user('test_user').person + p.stubs(:ask_to_join?).with(c).returns(true) + stubs(:user).returns(p) + + assert !ask_to_join? + end + + protected def url_for(args = {}) diff --git a/test/unit/folder_test.rb b/test/unit/folder_test.rb index d4c89f4..97fd9eb 100644 --- a/test/unit/folder_test.rb +++ b/test/unit/folder_test.rb @@ -47,7 +47,7 @@ class FolderTest < ActiveSupport::TestCase end should 'can display hits' do - profile = create_user('test_user').person + profile = create_user('testuser').person a = Folder.create!(:name => 'Test article', :profile => profile) assert_equal false, a.can_display_hits? end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 6210ed6..fe1f5ac 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -495,4 +495,21 @@ class PersonTest < Test::Unit::TestCase assert !p1.is_a_friend?(p2) end + should 'refuse join community' do + p = create_user('test_user').person + c = Community.create!(:name => 'Test community', :identifier => 'test_community') + + assert p.ask_to_join?(c) + p.refuse_join(c) + assert !p.ask_to_join?(c) + end + + should 'not ask to join for a member' do + p = create_user('test_user').person + c = Community.create!(:name => 'Test community', :identifier => 'test_community') + c.add_member(p) + + assert !p.ask_to_join?(c) + end + end -- libgit2 0.21.2