Commit 621630aeb02b7d8f42643682667ca712f006bb0a

Authored by MoisesMachado
1 parent 8a5c98ff

ActionItem591: made the templates be used to create new profiles

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2413 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/memberships_controller.rb
... ... @@ -26,6 +26,7 @@ class MembershipsController < MyProfileController
26 26 def new_community
27 27 @community = Community.new(params[:community])
28 28 if request.post?
  29 + @community.environment = environment
29 30 if @community.save
30 31 @community.add_admin(profile)
31 32 redirect_to :action => 'index'
... ...
app/controllers/public/account_controller.rb
... ... @@ -41,11 +41,10 @@ class AccountController < PublicController
41 41 begin
42 42 @user = User.new(params[:user])
43 43 @user.terms_of_use = environment.terms_of_use
  44 + @user.environment = environment
44 45 @terms_of_use = environment.terms_of_use
45 46 if request.post? && params[self.icaptcha_field].blank?
46 47 @user.save!
47   - @user.person.environment = environment
48   - @user.person.save!
49 48 self.current_user = @user
50 49 owner_role = Role.find_by_name('owner')
51 50 @user.person.affiliate(@user.person, [owner_role]) if owner_role
... ...
app/models/community.rb
... ... @@ -10,4 +10,8 @@ class Community < Organization
10 10 self.identifier = value.to_slug
11 11 end
12 12  
  13 + def template
  14 + environment.community_template
  15 + end
  16 +
13 17 end
... ...
app/models/create_enterprise.rb
... ... @@ -116,6 +116,8 @@ class CreateEnterprise < Task
116 116 profile_fields.include?(key.to_s)
117 117 end
118 118  
  119 + enterprise.environment = environment
  120 +
119 121 enterprise.user = self.requestor.user
120 122  
121 123 enterprise.update_attributes(organization_data)
... ...
app/models/enterprise.rb
... ... @@ -70,6 +70,10 @@ class Enterprise < Organization
70 70 blocks
71 71 end
72 72  
  73 + def template
  74 + environment.enterprise_template
  75 + end
  76 +
73 77 protected
74 78  
75 79 def default_homepage(attrs)
... ...
app/models/environment.rb
... ... @@ -264,15 +264,15 @@ class Environment < ActiveRecord::Base
264 264 end
265 265  
266 266 def enterprise_template
267   - Enterprise.find settings[:enterprise_template_id]
  267 + Enterprise.find_by_id settings[:enterprise_template_id]
268 268 end
269 269  
270 270 def community_template
271   - Community.find settings[:community_template_id]
  271 + Community.find_by_id settings[:community_template_id]
272 272 end
273 273  
274 274 def person_template
275   - Person.find settings[:person_template_id]
  275 + Person.find_by_id settings[:person_template_id]
276 276 end
277 277  
278 278 after_create do |env|
... ... @@ -280,8 +280,7 @@ class Environment < ActiveRecord::Base
280 280 ent_id = Enterprise.create!(:name => 'Enterprise template', :identifier => pre + 'enterprise_template', :environment => env, :public_profile => false).id
281 281 com_id = Community.create!(:name => 'Community template', :identifier => pre + 'community_template', :environment => env, :public_profile => false).id
282 282 pass = Digest::MD5.hexdigest rand.to_s
283   - user = User.create!(:login => 'person_template', :email => 'template@template.noo', :password => pass, :password_confirmation => pass).person
284   - user.environment = env
  283 + user = User.create!(:login => (pre + 'person_template'), :email => (pre + 'template@template.noo'), :password => pass, :password_confirmation => pass, :environment => env).person
285 284 user.public_profile = false
286 285 user.save!
287 286 usr_id = user.id
... ...
app/models/environment_statistics_block.rb
... ... @@ -9,9 +9,9 @@ class EnvironmentStatisticsBlock < Block
9 9 end
10 10  
11 11 def content
12   - users = owner.people.count
13   - enterprises = owner.enterprises.count
14   - communities = owner.communities.count
  12 + users = owner.people.find_all_by_public_profile(true).count
  13 + enterprises = owner.enterprises.find_all_by_public_profile(true).count
  14 + communities = owner.communities.find_all_by_public_profile(true).count
15 15  
16 16 info = [
17 17 n_('One user', '%{num} users', users) % { :num => users },
... ...
app/models/person.rb
... ... @@ -101,4 +101,8 @@ class Person < Profile
101 101 end
102 102 end
103 103  
  104 + def template
  105 + environment.person_template
  106 + end
  107 +
104 108 end
... ...
app/models/profile.rb
... ... @@ -178,14 +178,18 @@ class Profile < ActiveRecord::Base
178 178 # overriden for each subclass to create a custom set of boxes for its
179 179 # instances.
180 180 def create_default_set_of_boxes
181   - 3.times do
182   - self.boxes << Box.new
183   - end
  181 + if template
  182 + copy_blocks_from template
  183 + else
  184 + 3.times do
  185 + self.boxes << Box.new
  186 + end
184 187  
185   - if self.respond_to?(:default_set_of_blocks)
186   - default_set_of_blocks.each_with_index do |blocks,i|
187   - blocks.each do |block|
188   - self.boxes[i].blocks << block.new
  188 + if self.respond_to?(:default_set_of_blocks)
  189 + default_set_of_blocks.each_with_index do |blocks,i|
  190 + blocks.each do |block|
  191 + self.boxes[i].blocks << block.new
  192 + end
189 193 end
190 194 end
191 195 end
... ... @@ -193,6 +197,21 @@ class Profile &lt; ActiveRecord::Base
193 197 true
194 198 end
195 199  
  200 + def copy_blocks_from(profile)
  201 + self.boxes.destroy_all
  202 + profile.boxes.each do |box|
  203 + self.boxes << Box.new(:position => box.position)
  204 + box.blocks.each do |block|
  205 + self.boxes[-1].blocks << block.class.new(:title => block.title, :settings => block.settings, :position => block.position)
  206 + end
  207 + end
  208 + end
  209 +
  210 + # this method should be override to provide the correct template
  211 + def template
  212 + nil
  213 + end
  214 +
196 215 xss_terminate :only => [ :name, :nickname, :address, :contact_phone ]
197 216  
198 217 # returns the contact email for this profile. By default returns the the
... ...
test/functional/account_controller_test.rb
... ... @@ -497,6 +497,24 @@ class AccountControllerTest &lt; Test::Unit::TestCase
497 497 assert_tag :tag => 'input', :attributes => { :type => 'text', :name => @controller.icaptcha_field }
498 498 end
499 499  
  500 + should 'use the current environment for the template of user' do
  501 + template = User.create!(:login => 'test_template', :email => 'test@bli.com', :password => 'pass', :password_confirmation => 'pass').person
  502 + template.boxes.destroy_all
  503 + template.boxes << Box.new
  504 + template.boxes[0].blocks << Block.new
  505 + template.save!
  506 + env = Environment.create!(:name => 'test_env')
  507 + env.settings[:person_template_id] = template.id
  508 + env.save!
  509 +
  510 + @controller.stubs(:environment).returns(env)
  511 +
  512 + create_user
  513 +
  514 + assert_equal 1, assigns(:user).person.boxes.size
  515 + assert_equal 1, assigns(:user).person.boxes[0].blocks.size
  516 + end
  517 +
500 518 protected
501 519 def create_user(options = {}, extra_options ={})
502 520 post :signup, { :user => { :login => 'quire',
... ...
test/functional/memberships_controller_test.rb
... ... @@ -215,4 +215,22 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase
215 215 assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" }
216 216 end
217 217  
  218 + should 'use the current environment for the template of user' do
  219 + template = Community.create!(:identifier => 'test_template', :name => 'test@bli.com')
  220 + template.boxes.destroy_all
  221 + template.boxes << Box.new
  222 + template.boxes[0].blocks << Block.new
  223 + template.save!
  224 + env = Environment.create!(:name => 'test_env')
  225 + env.settings[:community_template_id] = template.id
  226 + env.save!
  227 +
  228 + @controller.stubs(:environment).returns(env)
  229 +
  230 + post :new_community, :profile => profile.identifier, :community => { :name => 'test community', :description => 'a test community'}
  231 +
  232 + assert_equal 1, assigns(:community).boxes.size
  233 + assert_equal 1, assigns(:community).boxes[0].blocks.size
  234 + end
  235 +
218 236 end
... ...
test/unit/community_test.rb
... ... @@ -82,5 +82,24 @@ class CommunityTest &lt; Test::Unit::TestCase
82 82 assert !RoleAssignment.exists?(i.id)
83 83 end
84 84 end
  85 +
  86 + should 'copy set of boxes from community template' do
  87 + template = Community.create!(:name => 'test template', :identifier => 'test_template')
  88 + template.boxes.destroy_all
  89 + template.boxes << Box.new
  90 + template.boxes[0].blocks << Block.new
  91 + template.save!
  92 +
  93 + env = Environment.create!(:name => 'test_env')
  94 + env.settings[:community_template_id] = template.id
  95 + env.save!
  96 +
  97 + assert_equal template, env.community_template
  98 +
  99 + com = Community.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env)
  100 +
  101 + assert_equal 1, com.boxes.size
  102 + assert_equal 1, com.boxes[0].blocks.size
  103 + end
85 104  
86 105 end
... ...
test/unit/create_enterprise_test.rb
... ... @@ -126,6 +126,40 @@ class CreateEnterpriseTest &lt; Test::Unit::TestCase
126 126 assert_equal task.name, enterprise.name
127 127 end
128 128  
  129 + 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
  130 +
  131 + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain')
  132 + region = Region.create!(:name => 'My region', :environment_id => environment.id)
  133 + validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
  134 + region.validators << validator
  135 + person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person
  136 +
  137 + task = CreateEnterprise.create!({
  138 + :name => 'My new enterprise',
  139 + :identifier => 'mynewenterprise',
  140 + :address => 'satan street, 666',
  141 + :contact_phone => '1298372198',
  142 + :contact_person => 'random joe',
  143 + :legal_form => 'cooperative',
  144 + :economic_activity => 'free software',
  145 + :region_id => region.id,
  146 + :requestor_id => person.id,
  147 + :target_id => validator.id,
  148 + })
  149 +
  150 + enterprise = Enterprise.new
  151 + Enterprise.expects(:new).returns(enterprise)
  152 +
  153 + task.finish
  154 +
  155 + assert !enterprise.new_record?
  156 + assert_equal person.user, enterprise.user
  157 + assert_equal environment, enterprise.environment
  158 +
  159 + # the data is not erased
  160 + assert_equal task.name, enterprise.name
  161 + end
  162 +
129 163 should 'override message methods from Task' do
130 164 generic = Task.new
131 165 specific = CreateEnterprise.new
... ... @@ -190,5 +224,4 @@ class CreateEnterpriseTest &lt; Test::Unit::TestCase
190 224 t = CreateEnterprise.new
191 225 assert_equal :validate_enterprise, t.permission
192 226 end
193   -
194 227 end
... ...
test/unit/domain_test.rb
1 1 require File.dirname(__FILE__) + '/../test_helper'
2 2  
3 3 class DomainTest < Test::Unit::TestCase
4   - fixtures :domains, :environments, :profiles
  4 + fixtures :domains, :environments, :profiles, :users
5 5  
6 6 # Replace this with your real tests.
7 7 def test_domain_name_format
... ...
test/unit/enterprise_test.rb
... ... @@ -205,4 +205,23 @@ class EnterpriseTest &lt; Test::Unit::TestCase
205 205 assert_not_includes ent.blocks.map(&:class), ProductsBlock
206 206 end
207 207  
  208 + should 'copy set of boxes from enterprise template' do
  209 + template = Enterprise.create!(:name => 'test template', :identifier => 'test_template')
  210 + template.boxes.destroy_all
  211 + template.boxes << Box.new
  212 + template.boxes[0].blocks << Block.new
  213 + template.save!
  214 +
  215 + env = Environment.create!(:name => 'test_env')
  216 + env.settings[:enterprise_template_id] = template.id
  217 + env.save!
  218 +
  219 + assert_equal template, env.enterprise_template
  220 +
  221 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env)
  222 +
  223 + assert_equal 1, ent.boxes.size
  224 + assert_equal 1, ent.boxes[0].blocks.size
  225 + end
  226 +
208 227 end
... ...
test/unit/environment_statistics_block_test.rb
... ... @@ -37,5 +37,28 @@ class EnvironmentStatisticsBlockTest &lt; Test::Unit::TestCase
37 37 assert_match /2 users/, content
38 38 assert_match /One community/, content
39 39 end
  40 +
  41 + should 'generate statistics but not for private profiles' do
  42 + env = Environment.create!(:name => "My test environment")
  43 + user1 = create_user('testuser1', :environment_id => env.id)
  44 + user2 = create_user('testuser2', :environment_id => env.id)
  45 + user3 = create_user('testuser3', :environment_id => env.id)
  46 + p = user3.person; p.public_profile = false; p.save!
  47 +
  48 + env.enterprises.build(:identifier => 'mytestenterprise', :name => 'My test enterprise').save!
  49 + env.enterprises.build(:identifier => 'mytestenterprise2', :name => 'My test enterprise 2', :public_profile => false).save!
  50 +
  51 + env.communities.build(:identifier => 'mytestcommunity', :name => 'mytestcommunity').save!
  52 + env.communities.build(:identifier => 'mytestcommunity2', :name => 'mytestcommunity 2', :public_profile => false).save!
  53 +
  54 + block = EnvironmentStatisticsBlock.new
  55 + env.boxes.first.blocks << block
  56 +
  57 + content = block.content
  58 +
  59 + assert_match /One enterprise/, content
  60 + assert_match /2 users/, content
  61 + assert_match /One community/, content
  62 + end
40 63  
41 64 end
... ...
test/unit/person_test.rb
... ... @@ -278,5 +278,24 @@ class PersonTest &lt; Test::Unit::TestCase
278 278  
279 279 assert person.display_info_to?(friend)
280 280 end
  281 +
  282 + should 'copy set of boxes from person template' do
  283 + template = create_user('test_template').person
  284 + template.boxes.destroy_all
  285 + template.boxes << Box.new
  286 + template.boxes[0].blocks << Block.new
  287 + template.save!
  288 +
  289 + env = Environment.create!(:name => 'test_env')
  290 + env.settings[:person_template_id] = template.id
  291 + env.save!
  292 +
  293 + assert_equal template, env.person_template
  294 +
  295 + person = create_user('test_user', :environment => env).person
  296 +
  297 + assert_equal 1, person.boxes.size
  298 + assert_equal 1, person.boxes[0].blocks.size
  299 + end
281 300  
282 301 end
... ...