diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-02-13 06:32:50 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-02-13 06:32:50 +0000 |
commit | dfa786631bb8476c8cae555ad8de5e83811dfe9d (patch) | |
tree | f1e9a04a9966a91ac4a283e9453134cbb95755de /activerecord/test | |
parent | 08a4c9979da44654f057494dc76498ed6a8506bc (diff) | |
download | rails-dfa786631bb8476c8cae555ad8de5e83811dfe9d.tar.gz rails-dfa786631bb8476c8cae555ad8de5e83811dfe9d.tar.bz2 rails-dfa786631bb8476c8cae555ad8de5e83811dfe9d.zip |
Introduce the :readonly option to all associations. Records from the association cannot be saved. Closes #11084.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8864 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/cases/associations_test.rb | 20 | ||||
-rw-r--r-- | activerecord/test/cases/reflection_test.rb | 6 | ||||
-rwxr-xr-x | activerecord/test/models/company.rb | 3 | ||||
-rw-r--r-- | activerecord/test/models/project.rb | 1 |
4 files changed, 27 insertions, 3 deletions
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index cfc58ffa10..89b978ad06 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -469,6 +469,11 @@ class HasOneAssociationsTest < ActiveRecord::TestCase end end + def test_cant_save_readonly_association + assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_firm).readonly_account.save! } + assert companies(:first_firm).readonly_account.readonly? + end + end @@ -544,6 +549,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length end + def test_dynamic_find_all_should_respect_readonly_access + companies(:first_firm).readonly_clients.find(:all).each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } } + companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? } + end + def test_triple_equality assert !(Array === Firm.find(:first).clients) assert Firm.find(:first).clients === Array @@ -1581,6 +1591,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal post.author_id, author2.id end + def test_cant_save_readonly_association + assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! } + assert companies(:first_client).readonly_firm.readonly? + end + end @@ -1987,6 +2002,11 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, projects(:active_record).limited_developers.find_all_by_name('Jamis', :limit => 9_000).length end + def test_dynamic_find_all_should_respect_readonly_access + projects(:active_record).readonly_developers.each { |d| assert_raise(ActiveRecord::ReadOnlyRecord) { d.save! } if d.valid?} + projects(:active_record).readonly_developers.each { |d| d.readonly? } + end + def test_new_with_values_in_collection jamis = DeveloperForProjectWithAfterCreateHook.find_by_name('Jamis') david = DeveloperForProjectWithAfterCreateHook.find_by_name('David') diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index c2dedec3ef..c8ee40ea09 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -159,9 +159,9 @@ class ReflectionTest < ActiveRecord::TestCase end def test_reflection_of_all_associations - assert_equal 17, Firm.reflect_on_all_associations.size - assert_equal 15, Firm.reflect_on_all_associations(:has_many).size - assert_equal 2, Firm.reflect_on_all_associations(:has_one).size + assert_equal 19, Firm.reflect_on_all_associations.size + assert_equal 16, Firm.reflect_on_all_associations(:has_many).size + assert_equal 3, Firm.reflect_on_all_associations(:has_one).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size end diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index c33f188a02..3d76dfd398 100755 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -40,8 +40,10 @@ class Firm < Company :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000' has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1' has_many :plain_clients, :class_name => 'Client' + has_many :readonly_clients, :class_name => 'Client', :readonly => true has_one :account, :foreign_key => "firm_id", :dependent => :destroy + has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true end class DependentFirm < Company @@ -60,6 +62,7 @@ class Client < Company belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id" belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of" belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1] + belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true # Record destruction so we can test whether firm.clients.clear has # is calling client.destroy, deleting from the database, or setting diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index b90d2c6fbc..e1ab89eca5 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -1,5 +1,6 @@ class Project < ActiveRecord::Base has_and_belongs_to_many :developers, :uniq => true, :order => 'developers.name desc, developers.id desc' + has_and_belongs_to_many :readonly_developers, :class_name => "Developer", :readonly => true has_and_belongs_to_many :selected_developers, :class_name => "Developer", :select => "developers.*", :uniq => true has_and_belongs_to_many :non_unique_developers, :order => 'developers.name desc, developers.id desc', :class_name => 'Developer' has_and_belongs_to_many :limited_developers, :class_name => "Developer", :limit => 1 |