Commit e9d696527e0d86a554ec4916cc1ad19dcaa89904

Authored by AntonioTerceiro
1 parent d5b832ec

ActionItem153: putting Person in the same environment as User


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1276 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/user.rb
... ... @@ -7,11 +7,18 @@ class User < ActiveRecord::Base
7 7 N_('User|Password')
8 8 N_('User|Password confirmation')
9 9  
  10 + before_create do |user|
  11 + if user.environment.nil?
  12 + user.environment = Environment.default
  13 + end
  14 + end
  15 +
10 16 after_create do |user|
11   - Person.create!(:identifier => user.login, :name => user.login, :user_id => user.id)
  17 + Person.create!(:identifier => user.login, :name => user.login, :user_id => user.id, :environment_id => user.environment_id)
12 18 end
13 19  
14 20 has_one :person, :dependent => :destroy
  21 + belongs_to :environment
15 22  
16 23 # Virtual attribute for the unencrypted password
17 24 attr_accessor :password
... ...
db/migrate/006_create_users.rb
... ... @@ -12,6 +12,8 @@ class CreateUsers < ActiveRecord::Migration
12 12  
13 13 t.column :terms_of_use, :text
14 14 t.column :terms_accepted, :string, :limit => 1
  15 +
  16 + t.column :environment_id, :integer
15 17 end
16 18 end
17 19  
... ...
test/unit/user_test.rb
... ... @@ -148,6 +148,13 @@ class UserTest < Test::Unit::TestCase
148 148 assert Person.find_by_identifier('lalala')
149 149 end
150 150  
  151 + should 'set the same environment for user and person objects' do
  152 + env = Environment.create!(:name => 'my test environment')
  153 + user = create_user(:environment_id => env.id)
  154 + assert_equal env, user.environment
  155 + assert_equal env, user.person.environment
  156 + end
  157 +
151 158 def test_should_destroy_person_when_destroying_user
152 159 user = create_user(:login => 'lalala', :email => 'lalala@example.com')
153 160 assert Person.find_by_identifier('lalala')
... ...