Commit d5b832ece8857d381a158a17525f72efe3c2f32a
1 parent
f7932a0d
Exists in
master
and in
23 other branches
ActionItem153: fixing the association of new users to profiles
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1275 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
5 changed files
with
35 additions
and
11 deletions
Show diff stats
app/models/person.rb
app/models/profile.rb
| @@ -73,12 +73,12 @@ class Profile < ActiveRecord::Base | @@ -73,12 +73,12 @@ class Profile < ActiveRecord::Base | ||
| 73 | validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS | 73 | validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS |
| 74 | validates_uniqueness_of :identifier | 74 | validates_uniqueness_of :identifier |
| 75 | 75 | ||
| 76 | - # creates a new Profile. By default, it is attached to the default | ||
| 77 | - # Environment (see Environment#default), unless you tell it | ||
| 78 | - # otherwise | ||
| 79 | - def initialize(*args) | ||
| 80 | - super(*args) | ||
| 81 | - self.environment ||= Environment.default | 76 | + before_create :set_default_environment |
| 77 | + def set_default_environment | ||
| 78 | + if self.environment.nil? | ||
| 79 | + self.environment = Environment.default | ||
| 80 | + end | ||
| 81 | + true | ||
| 82 | end | 82 | end |
| 83 | 83 | ||
| 84 | # registar callback for creating boxes after the object is created. | 84 | # registar callback for creating boxes after the object is created. |
test/test_helper.rb
| @@ -28,6 +28,8 @@ class Test::Unit::TestCase | @@ -28,6 +28,8 @@ class Test::Unit::TestCase | ||
| 28 | # Add more helper methods to be used by all tests here... | 28 | # Add more helper methods to be used by all tests here... |
| 29 | 29 | ||
| 30 | include AuthenticatedTestHelper | 30 | include AuthenticatedTestHelper |
| 31 | + | ||
| 32 | + fixtures :environments | ||
| 31 | 33 | ||
| 32 | def self.all_fixtures | 34 | def self.all_fixtures |
| 33 | Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |item| | 35 | Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |item| |
test/unit/enterprise_test.rb
| @@ -35,8 +35,7 @@ class EnterpriseTest < Test::Unit::TestCase | @@ -35,8 +35,7 @@ class EnterpriseTest < Test::Unit::TestCase | ||
| 35 | end | 35 | end |
| 36 | 36 | ||
| 37 | def test_belongs_to_environment_and_has_default | 37 | def test_belongs_to_environment_and_has_default |
| 38 | - p = Enterprise.new | ||
| 39 | - assert_kind_of Environment, p.environment | 38 | + assert_equal Environment.default, Enterprise.create!(:name => 'my test environment', :identifier => 'mytestenvironment').environment |
| 40 | end | 39 | end |
| 41 | 40 | ||
| 42 | def test_cannot_rename | 41 | def test_cannot_rename |
test/unit/profile_test.rb
| @@ -34,9 +34,29 @@ class ProfileTest < Test::Unit::TestCase | @@ -34,9 +34,29 @@ class ProfileTest < Test::Unit::TestCase | ||
| 34 | assert_kind_of Array, p.domains | 34 | assert_kind_of Array, p.domains |
| 35 | end | 35 | end |
| 36 | 36 | ||
| 37 | - def test_belongs_to_environment_and_has_default | ||
| 38 | - p = Profile.new | ||
| 39 | - assert_kind_of Environment, p.environment | 37 | + should 'be assigned to default environment if no environment is informed' do |
| 38 | + assert_equal Environment.default, Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile').environment | ||
| 39 | + end | ||
| 40 | + | ||
| 41 | + should 'not override environment informed before creation' do | ||
| 42 | + env = Environment.create!(:name => 'My test environment') | ||
| 43 | + p = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment_id => env.id) | ||
| 44 | + | ||
| 45 | + assert_equal env, p.environment | ||
| 46 | + end | ||
| 47 | + | ||
| 48 | + should 'be able to set environment after instantiation and before creating' do | ||
| 49 | + env = Environment.create!(:name => 'My test environment') | ||
| 50 | + p = Profile.new(:identifier => 'mytestprofile', :name => 'My test profile') | ||
| 51 | + p.environment = env | ||
| 52 | + p.save! | ||
| 53 | + | ||
| 54 | + p.reload | ||
| 55 | + assert_equal env, p.environment | ||
| 56 | + end | ||
| 57 | + | ||
| 58 | + should 'set default environment for users created' do | ||
| 59 | + assert_equal Environment.default, create_user('mytestuser').person.environment | ||
| 40 | end | 60 | end |
| 41 | 61 | ||
| 42 | def test_cannot_rename | 62 | def test_cannot_rename |