3 require 'models/comment'
4 require 'models/developer'
5 require 'models/project'
6 require 'models/reader'
7 require 'models/person'
9 # Dummy class methods to test implicit association scoping.
10 def Comment
.foo() find
:first end
11 def Project
.foo() find
:first end
14 class ReadOnlyTest
< ActiveRecord
::TestCase
15 fixtures
:posts, :comments, :developers, :projects, :developers_projects
17 def test_cant_save_readonly_record
18 dev
= Developer
.find(1)
24 assert_nothing_raised
do
25 dev
.name
= 'Luscious forbidden fruit.'
27 dev
.name
= 'Forbidden.'
29 assert_raise(ActiveRecord
::ReadOnlyRecord) { dev
.save
}
30 assert_raise(ActiveRecord
::ReadOnlyRecord) { dev
.save
! }
34 def test_find_with_readonly_option
35 Developer
.find(:all).each
{ |d
| assert
!d
.readonly
? }
36 Developer
.find(:all, :readonly => false).each
{ |d
| assert
!d
.readonly
? }
37 Developer
.find(:all, :readonly => true).each
{ |d
| assert d
.readonly
? }
41 def test_find_with_joins_option_implies_readonly
42 # Blank joins don't count.
43 Developer
.find(:all, :joins => ' ').each
{ |d
| assert
!d
.readonly
? }
44 Developer
.find(:all, :joins => ' ', :readonly => false).each
{ |d
| assert
!d
.readonly
? }
47 Developer
.find(:all, :joins => ', projects').each
{ |d
| assert d
.readonly
? }
48 Developer
.find(:all, :joins => ', projects', :readonly => false).each
{ |d
| assert
!d
.readonly
? }
52 def test_habtm_find_readonly
53 dev
= Developer
.find(1)
54 assert
!dev
.projects
.empty
?
55 assert dev
.projects
.all
?(&:readonly?)
56 assert dev
.projects
.find(:all).all
?(&:readonly?)
57 assert dev
.projects
.find(:all, :readonly => true).all
?(&:readonly?)
60 def test_has_many_find_readonly
62 assert
!post
.comments
.empty
?
63 assert
!post
.comments
.any
?(&:readonly?)
64 assert
!post
.comments
.find(:all).any
?(&:readonly?)
65 assert post
.comments
.find(:all, :readonly => true).all
?(&:readonly?)
68 def test_has_many_with_through_is_not_implicitly_marked_readonly
69 assert people
= Post
.find(1).people
70 assert
!people
.any
?(&:readonly?)
73 def test_readonly_scoping
74 Post
.with_scope(:find => { :conditions => '1=1' }) do
75 assert
!Post
.find(1).readonly
?
76 assert Post
.find(1, :readonly => true).readonly
?
77 assert
!Post
.find(1, :readonly => false).readonly
?
80 Post
.with_scope(:find => { :joins => ' ' }) do
81 assert
!Post
.find(1).readonly
?
82 assert Post
.find(1, :readonly => true).readonly
?
83 assert
!Post
.find(1, :readonly => false).readonly
?
86 # Oracle barfs on this because the join includes unqualified and
87 # conflicting column names
88 unless current_adapter
?(:OracleAdapter)
89 Post
.with_scope(:find => { :joins => ', developers' }) do
90 assert Post
.find(1).readonly
?
91 assert Post
.find(1, :readonly => true).readonly
?
92 assert
!Post
.find(1, :readonly => false).readonly
?
96 Post
.with_scope(:find => { :readonly => true }) do
97 assert Post
.find(1).readonly
?
98 assert Post
.find(1, :readonly => true).readonly
?
99 assert
!Post
.find(1, :readonly => false).readonly
?
103 def test_association_collection_method_missing_scoping_not_readonly
104 assert
!Developer
.find(1).projects
.foo
.readonly
?
105 assert
!Post
.find(1).comments
.foo
.readonly
?