From 2f70f8a493a1257249f2d9ea6ec20785f7d1bae4 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 8 Feb 2008 23:38:50 +0000 Subject: [PATCH] ActionItem41: initial implementation of community creation --- app/controllers/my_profile/manage_communities_controller.rb | 17 ++++++++++++++++- app/models/community.rb | 7 +++++++ app/models/person.rb | 6 +++++- test/fixtures/roles.yml | 10 ++++++++++ test/functional/manage_communities_controller_test.rb | 32 ++++++++++++++++++++++++++++++-- test/test_helper.rb | 2 +- test/unit/community_test.rb | 9 +++++++++ test/unit/person_test.rb | 9 +++++++++ 8 files changed, 87 insertions(+), 5 deletions(-) diff --git a/app/controllers/my_profile/manage_communities_controller.rb b/app/controllers/my_profile/manage_communities_controller.rb index 4322c6e..cc98f0d 100644 --- a/app/controllers/my_profile/manage_communities_controller.rb +++ b/app/controllers/my_profile/manage_communities_controller.rb @@ -1,2 +1,17 @@ -class ManageCommunitiesController < ApplicationController +class ManageCommunitiesController < MyProfileController + + def index + @communities = profile.community_memberships + end + + def new + @community = Community.new(params[:community]) + if request.post? + if @community.save + @community.add_member(profile) + redirect_to :action => 'index' + end + end + end + end diff --git a/app/models/community.rb b/app/models/community.rb index 421c214..8865abb 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -7,4 +7,11 @@ class Community < Organization self.identifier = value.to_slug end + # FIXME should't this method be in Profile class? + # + # Adds a person as member of this Community (FIXME). + def add_member(person) + self.affiliate(person, Profile::Roles.member) + end + end diff --git a/app/models/person.rb b/app/models/person.rb index 89a62f9..9fdca97 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -24,7 +24,11 @@ class Person < Profile end def enterprise_memberships - memberships.select{|p|p.class == Enterprise} + memberships(:type => Enterprise.name) + end + + def community_memberships + memberships(:type => Community.name) end diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index ffee52b..d6c0ed2 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -27,3 +27,13 @@ four: - manage_environment_categories - manage_environment_roles - manage_environment_validators +profile_admin: + id: 5 + key: 'profile_admin' + name: 'Profile Administrator' + system: true +profile_member: + id: 6 + key: 'profile_member' + name: 'Profile Member' + system: true diff --git a/test/functional/manage_communities_controller_test.rb b/test/functional/manage_communities_controller_test.rb index b430b39..5ebc0b8 100644 --- a/test/functional/manage_communities_controller_test.rb +++ b/test/functional/manage_communities_controller_test.rb @@ -5,14 +5,42 @@ require 'manage_communities_controller' class ManageCommunitiesController; def rescue_action(e) raise e end; end class ManageCommunitiesControllerTest < Test::Unit::TestCase + def setup @controller = ManageCommunitiesController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + + @profile = create_user('mytestuser').person + end + attr_reader :profile + + should 'present new community form' do + get :new, :profile => profile.identifier + assert_response :success + assert_template 'new' + end + + should 'be able to create a new community' do + assert_difference Community, :count do + post :new, :profile => profile.identifier, :community => { :name => 'My shiny new community', :description => 'This is a community devoted to anything interesting we find in the internet '} + assert_response :redirect + assert_redirected_to :action => 'index' + + assert Community.find_by_identifier('my-shiny-new-community').members.include?(profile), "Creator user should be added as member of the community just created" + end + end + + should 'list current communities' do + get :index, :profile => profile.identifier + assert_kind_of Array, assigns(:communities) + assert_response :success + assert_template 'index' end - should 'have tests' do - flunk 'pending' + should 'link to new community creation' do + get :index, :profile => profile.identifier + assert_tag :tag => 'a', :attributes => { :href => '/myprofile/mytestuser/manage_communities/new' } end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 7561036..b53f6b3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -29,7 +29,7 @@ class Test::Unit::TestCase include AuthenticatedTestHelper - fixtures :environments + fixtures :environments, :roles def self.all_fixtures Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |item| diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index a493cd0..0fc0f30 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -18,4 +18,13 @@ class CommunityTest < Test::Unit::TestCase assert_equal 'the description of the community', c.description end + should 'allow to add new members' do + c = Community.create!(:name => 'my test community') + p = create_user('mytestuser').person + + c.add_member(p) + + assert c.members.include?(p), "Community should add the new member" + end + end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index e7b55aa..63514da 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -34,6 +34,15 @@ class PersonTest < Test::Unit::TestCase assert p.memberships.include?(e) assert p.enterprise_memberships.include?(e) end + + should 'belong to communities' do + c = Community.create!(:name => 'my test community') + p = create_user('mytestuser').person + + c.add_member(p) + + assert p.community_memberships.include?(c), "Community should add a new member" + end def test_can_have_user u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') -- libgit2 0.21.2