From e9d696527e0d86a554ec4916cc1ad19dcaa89904 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Mon, 28 Jan 2008 15:12:40 +0000 Subject: [PATCH] ActionItem153: putting Person in the same environment as User --- app/models/user.rb | 9 ++++++++- db/migrate/006_create_users.rb | 2 ++ test/unit/user_test.rb | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 57dc8ba..80a0e74 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,11 +7,18 @@ class User < ActiveRecord::Base N_('User|Password') N_('User|Password confirmation') + before_create do |user| + if user.environment.nil? + user.environment = Environment.default + end + end + after_create do |user| - Person.create!(:identifier => user.login, :name => user.login, :user_id => user.id) + Person.create!(:identifier => user.login, :name => user.login, :user_id => user.id, :environment_id => user.environment_id) end has_one :person, :dependent => :destroy + belongs_to :environment # Virtual attribute for the unencrypted password attr_accessor :password diff --git a/db/migrate/006_create_users.rb b/db/migrate/006_create_users.rb index aeefe83..1eebccb 100644 --- a/db/migrate/006_create_users.rb +++ b/db/migrate/006_create_users.rb @@ -12,6 +12,8 @@ class CreateUsers < ActiveRecord::Migration t.column :terms_of_use, :text t.column :terms_accepted, :string, :limit => 1 + + t.column :environment_id, :integer end end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index fc2f184..a56f607 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -148,6 +148,13 @@ class UserTest < Test::Unit::TestCase assert Person.find_by_identifier('lalala') end + should 'set the same environment for user and person objects' do + env = Environment.create!(:name => 'my test environment') + user = create_user(:environment_id => env.id) + assert_equal env, user.environment + assert_equal env, user.person.environment + end + def test_should_destroy_person_when_destroying_user user = create_user(:login => 'lalala', :email => 'lalala@example.com') assert Person.find_by_identifier('lalala') -- libgit2 0.21.2