diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index 517e9f5..bd08e66 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -26,6 +26,7 @@ class MembershipsController < MyProfileController def new_community @community = Community.new(params[:community]) if request.post? + @community.environment = environment if @community.save @community.add_admin(profile) redirect_to :action => 'index' diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 0f1be4b..58a318a 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -41,11 +41,10 @@ class AccountController < PublicController begin @user = User.new(params[:user]) @user.terms_of_use = environment.terms_of_use + @user.environment = environment @terms_of_use = environment.terms_of_use if request.post? && params[self.icaptcha_field].blank? @user.save! - @user.person.environment = environment - @user.person.save! self.current_user = @user owner_role = Role.find_by_name('owner') @user.person.affiliate(@user.person, [owner_role]) if owner_role diff --git a/app/models/community.rb b/app/models/community.rb index 6928f8a..c6d099c 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -10,4 +10,8 @@ class Community < Organization self.identifier = value.to_slug end + def template + environment.community_template + end + end diff --git a/app/models/create_enterprise.rb b/app/models/create_enterprise.rb index 0e2f2c6..5b525c2 100644 --- a/app/models/create_enterprise.rb +++ b/app/models/create_enterprise.rb @@ -116,6 +116,8 @@ class CreateEnterprise < Task profile_fields.include?(key.to_s) end + enterprise.environment = environment + enterprise.user = self.requestor.user enterprise.update_attributes(organization_data) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 877a5f2..3cc387f 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -70,6 +70,10 @@ class Enterprise < Organization blocks end + def template + environment.enterprise_template + end + protected def default_homepage(attrs) diff --git a/app/models/environment.rb b/app/models/environment.rb index d5703b3..0f385a1 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -264,15 +264,15 @@ class Environment < ActiveRecord::Base end def enterprise_template - Enterprise.find settings[:enterprise_template_id] + Enterprise.find_by_id settings[:enterprise_template_id] end def community_template - Community.find settings[:community_template_id] + Community.find_by_id settings[:community_template_id] end def person_template - Person.find settings[:person_template_id] + Person.find_by_id settings[:person_template_id] end after_create do |env| @@ -280,8 +280,7 @@ class Environment < ActiveRecord::Base ent_id = Enterprise.create!(:name => 'Enterprise template', :identifier => pre + 'enterprise_template', :environment => env, :public_profile => false).id com_id = Community.create!(:name => 'Community template', :identifier => pre + 'community_template', :environment => env, :public_profile => false).id pass = Digest::MD5.hexdigest rand.to_s - user = User.create!(:login => 'person_template', :email => 'template@template.noo', :password => pass, :password_confirmation => pass).person - user.environment = env + user = User.create!(:login => (pre + 'person_template'), :email => (pre + 'template@template.noo'), :password => pass, :password_confirmation => pass, :environment => env).person user.public_profile = false user.save! usr_id = user.id diff --git a/app/models/environment_statistics_block.rb b/app/models/environment_statistics_block.rb index 033c1ab..5cf7332 100644 --- a/app/models/environment_statistics_block.rb +++ b/app/models/environment_statistics_block.rb @@ -9,9 +9,9 @@ class EnvironmentStatisticsBlock < Block end def content - users = owner.people.count - enterprises = owner.enterprises.count - communities = owner.communities.count + users = owner.people.find_all_by_public_profile(true).count + enterprises = owner.enterprises.find_all_by_public_profile(true).count + communities = owner.communities.find_all_by_public_profile(true).count info = [ n_('One user', '%{num} users', users) % { :num => users }, diff --git a/app/models/person.rb b/app/models/person.rb index 61a7da0..e83d416 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -101,4 +101,8 @@ class Person < Profile end end + def template + environment.person_template + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index 38aa5ed..0f4a25c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -178,14 +178,18 @@ class Profile < ActiveRecord::Base # overriden for each subclass to create a custom set of boxes for its # instances. def create_default_set_of_boxes - 3.times do - self.boxes << Box.new - end + if template + copy_blocks_from template + else + 3.times do + self.boxes << Box.new + end - if self.respond_to?(:default_set_of_blocks) - default_set_of_blocks.each_with_index do |blocks,i| - blocks.each do |block| - self.boxes[i].blocks << block.new + if self.respond_to?(:default_set_of_blocks) + default_set_of_blocks.each_with_index do |blocks,i| + blocks.each do |block| + self.boxes[i].blocks << block.new + end end end end @@ -193,6 +197,21 @@ class Profile < ActiveRecord::Base true end + def copy_blocks_from(profile) + self.boxes.destroy_all + profile.boxes.each do |box| + self.boxes << Box.new(:position => box.position) + box.blocks.each do |block| + self.boxes[-1].blocks << block.class.new(:title => block.title, :settings => block.settings, :position => block.position) + end + end + end + + # this method should be override to provide the correct template + def template + nil + end + xss_terminate :only => [ :name, :nickname, :address, :contact_phone ] # returns the contact email for this profile. By default returns the the diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 6600789..769b245 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -497,6 +497,24 @@ class AccountControllerTest < Test::Unit::TestCase assert_tag :tag => 'input', :attributes => { :type => 'text', :name => @controller.icaptcha_field } end + should 'use the current environment for the template of user' do + template = User.create!(:login => 'test_template', :email => 'test@bli.com', :password => 'pass', :password_confirmation => 'pass').person + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.save! + env = Environment.create!(:name => 'test_env') + env.settings[:person_template_id] = template.id + env.save! + + @controller.stubs(:environment).returns(env) + + create_user + + assert_equal 1, assigns(:user).person.boxes.size + assert_equal 1, assigns(:user).person.boxes[0].blocks.size + end + protected def create_user(options = {}, extra_options ={}) post :signup, { :user => { :login => 'quire', diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index 9a4b76b..b5c2d76 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -215,4 +215,22 @@ class MembershipsControllerTest < Test::Unit::TestCase assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" } end + should 'use the current environment for the template of user' do + template = Community.create!(:identifier => 'test_template', :name => 'test@bli.com') + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.save! + env = Environment.create!(:name => 'test_env') + env.settings[:community_template_id] = template.id + env.save! + + @controller.stubs(:environment).returns(env) + + post :new_community, :profile => profile.identifier, :community => { :name => 'test community', :description => 'a test community'} + + assert_equal 1, assigns(:community).boxes.size + assert_equal 1, assigns(:community).boxes[0].blocks.size + end + end diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index 69ed3f2..e5891ca 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -82,5 +82,24 @@ class CommunityTest < Test::Unit::TestCase assert !RoleAssignment.exists?(i.id) end end + + should 'copy set of boxes from community template' do + template = Community.create!(:name => 'test template', :identifier => 'test_template') + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.save! + + env = Environment.create!(:name => 'test_env') + env.settings[:community_template_id] = template.id + env.save! + + assert_equal template, env.community_template + + com = Community.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env) + + assert_equal 1, com.boxes.size + assert_equal 1, com.boxes[0].blocks.size + end end diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 8bbe0ce..5e8f552 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -126,6 +126,40 @@ class CreateEnterpriseTest < Test::Unit::TestCase assert_equal task.name, enterprise.name end + should 'actually create an enterprise when finishing the task and associate the task requestor as its owner through the "user" association even when environment is not default' do + + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain') + region = Region.create!(:name => 'My region', :environment_id => environment.id) + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) + region.validators << validator + person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + + task = CreateEnterprise.create!({ + :name => 'My new enterprise', + :identifier => 'mynewenterprise', + :address => 'satan street, 666', + :contact_phone => '1298372198', + :contact_person => 'random joe', + :legal_form => 'cooperative', + :economic_activity => 'free software', + :region_id => region.id, + :requestor_id => person.id, + :target_id => validator.id, + }) + + enterprise = Enterprise.new + Enterprise.expects(:new).returns(enterprise) + + task.finish + + assert !enterprise.new_record? + assert_equal person.user, enterprise.user + assert_equal environment, enterprise.environment + + # the data is not erased + assert_equal task.name, enterprise.name + end + should 'override message methods from Task' do generic = Task.new specific = CreateEnterprise.new @@ -190,5 +224,4 @@ class CreateEnterpriseTest < Test::Unit::TestCase t = CreateEnterprise.new assert_equal :validate_enterprise, t.permission end - end diff --git a/test/unit/domain_test.rb b/test/unit/domain_test.rb index f0dc58a..68681db 100644 --- a/test/unit/domain_test.rb +++ b/test/unit/domain_test.rb @@ -1,7 +1,7 @@ require File.dirname(__FILE__) + '/../test_helper' class DomainTest < Test::Unit::TestCase - fixtures :domains, :environments, :profiles + fixtures :domains, :environments, :profiles, :users # Replace this with your real tests. def test_domain_name_format diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index df5ac1b..5b05138 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -205,4 +205,23 @@ class EnterpriseTest < Test::Unit::TestCase assert_not_includes ent.blocks.map(&:class), ProductsBlock end + should 'copy set of boxes from enterprise template' do + template = Enterprise.create!(:name => 'test template', :identifier => 'test_template') + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.save! + + env = Environment.create!(:name => 'test_env') + env.settings[:enterprise_template_id] = template.id + env.save! + + assert_equal template, env.enterprise_template + + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env) + + assert_equal 1, ent.boxes.size + assert_equal 1, ent.boxes[0].blocks.size + end + end diff --git a/test/unit/environment_statistics_block_test.rb b/test/unit/environment_statistics_block_test.rb index c748eae..ccf9644 100644 --- a/test/unit/environment_statistics_block_test.rb +++ b/test/unit/environment_statistics_block_test.rb @@ -37,5 +37,28 @@ class EnvironmentStatisticsBlockTest < Test::Unit::TestCase assert_match /2 users/, content assert_match /One community/, content end + + should 'generate statistics but not for private profiles' do + env = Environment.create!(:name => "My test environment") + user1 = create_user('testuser1', :environment_id => env.id) + user2 = create_user('testuser2', :environment_id => env.id) + user3 = create_user('testuser3', :environment_id => env.id) + p = user3.person; p.public_profile = false; p.save! + + env.enterprises.build(:identifier => 'mytestenterprise', :name => 'My test enterprise').save! + env.enterprises.build(:identifier => 'mytestenterprise2', :name => 'My test enterprise 2', :public_profile => false).save! + + env.communities.build(:identifier => 'mytestcommunity', :name => 'mytestcommunity').save! + env.communities.build(:identifier => 'mytestcommunity2', :name => 'mytestcommunity 2', :public_profile => false).save! + + block = EnvironmentStatisticsBlock.new + env.boxes.first.blocks << block + + content = block.content + + assert_match /One enterprise/, content + assert_match /2 users/, content + assert_match /One community/, content + end end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 739519f..5b0ac75 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -278,5 +278,24 @@ class PersonTest < Test::Unit::TestCase assert person.display_info_to?(friend) end + + should 'copy set of boxes from person template' do + template = create_user('test_template').person + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.save! + + env = Environment.create!(:name => 'test_env') + env.settings[:person_template_id] = template.id + env.save! + + assert_equal template, env.person_template + + person = create_user('test_user', :environment => env).person + + assert_equal 1, person.boxes.size + assert_equal 1, person.boxes[0].blocks.size + end end -- libgit2 0.21.2