2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/post'
|
|
|
|
require 'models/comment'
|
|
|
|
require 'models/developer'
|
|
|
|
require 'models/project'
|
|
|
|
require 'models/reader'
|
|
|
|
require 'models/person'
|
2005-10-14 20:19:56 -04:00
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
# Dummy class methods to test implicit association scoping.
|
2005-10-28 00:53:22 -04:00
|
|
|
def Comment.foo() find :first end
|
|
|
|
def Project.foo() find :first end
|
|
|
|
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ReadOnlyTest < ActiveRecord::TestCase
|
2005-10-28 00:53:22 -04:00
|
|
|
fixtures :posts, :comments, :developers, :projects, :developers_projects
|
2005-10-14 20:19:56 -04:00
|
|
|
|
|
|
|
def test_cant_save_readonly_record
|
2005-10-28 00:53:22 -04:00
|
|
|
dev = Developer.find(1)
|
2005-10-20 10:53:04 -04:00
|
|
|
assert !dev.readonly?
|
2005-10-14 20:19:56 -04:00
|
|
|
|
2005-10-20 10:53:04 -04:00
|
|
|
dev.readonly!
|
|
|
|
assert dev.readonly?
|
2005-10-14 20:19:56 -04:00
|
|
|
|
|
|
|
assert_nothing_raised do
|
2005-10-20 10:53:04 -04:00
|
|
|
dev.name = 'Luscious forbidden fruit.'
|
|
|
|
assert !dev.save
|
|
|
|
dev.name = 'Forbidden.'
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
2005-10-20 10:53:04 -04:00
|
|
|
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save }
|
|
|
|
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
|
|
|
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2005-10-14 20:19:56 -04:00
|
|
|
def test_find_with_readonly_option
|
2005-10-20 10:53:04 -04:00
|
|
|
Developer.find(:all).each { |d| assert !d.readonly? }
|
|
|
|
Developer.find(:all, :readonly => false).each { |d| assert !d.readonly? }
|
|
|
|
Developer.find(:all, :readonly => true).each { |d| assert d.readonly? }
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
|
|
|
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2005-10-14 20:19:56 -04:00
|
|
|
def test_find_with_joins_option_implies_readonly
|
2005-10-28 00:53:22 -04:00
|
|
|
# Blank joins don't count.
|
|
|
|
Developer.find(:all, :joins => ' ').each { |d| assert !d.readonly? }
|
|
|
|
Developer.find(:all, :joins => ' ', :readonly => false).each { |d| assert !d.readonly? }
|
|
|
|
|
|
|
|
# Others do.
|
|
|
|
Developer.find(:all, :joins => ', projects').each { |d| assert d.readonly? }
|
|
|
|
Developer.find(:all, :joins => ', projects', :readonly => false).each { |d| assert !d.readonly? }
|
2005-10-20 10:53:04 -04:00
|
|
|
end
|
|
|
|
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2005-10-20 10:53:04 -04:00
|
|
|
def test_habtm_find_readonly
|
2005-10-28 00:53:22 -04:00
|
|
|
dev = Developer.find(1)
|
|
|
|
assert !dev.projects.empty?
|
2006-03-30 17:27:32 -05:00
|
|
|
assert dev.projects.all?(&:readonly?)
|
|
|
|
assert dev.projects.find(:all).all?(&:readonly?)
|
|
|
|
assert dev.projects.find(:all, :readonly => true).all?(&:readonly?)
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
|
|
|
|
def test_has_many_find_readonly
|
|
|
|
post = Post.find(1)
|
|
|
|
assert !post.comments.empty?
|
2006-03-30 17:27:32 -05:00
|
|
|
assert !post.comments.any?(&:readonly?)
|
|
|
|
assert !post.comments.find(:all).any?(&:readonly?)
|
|
|
|
assert post.comments.find(:all, :readonly => true).all?(&:readonly?)
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2006-03-30 17:27:32 -05:00
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly
|
|
|
|
assert people = Post.find(1).people
|
|
|
|
assert !people.any?(&:readonly?)
|
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_readonly_scoping
|
2008-01-18 02:30:42 -05:00
|
|
|
Post.with_scope(:find => { :conditions => '1=1' }) do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert !Post.find(1).readonly?
|
|
|
|
assert Post.find(1, :readonly => true).readonly?
|
|
|
|
assert !Post.find(1, :readonly => false).readonly?
|
|
|
|
end
|
|
|
|
|
2008-01-18 02:30:42 -05:00
|
|
|
Post.with_scope(:find => { :joins => ' ' }) do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert !Post.find(1).readonly?
|
|
|
|
assert Post.find(1, :readonly => true).readonly?
|
|
|
|
assert !Post.find(1, :readonly => false).readonly?
|
|
|
|
end
|
|
|
|
|
2005-10-29 14:40:49 -04:00
|
|
|
# Oracle barfs on this because the join includes unqualified and
|
|
|
|
# conflicting column names
|
2006-03-01 11:01:53 -05:00
|
|
|
unless current_adapter?(:OracleAdapter)
|
2008-01-18 02:30:42 -05:00
|
|
|
Post.with_scope(:find => { :joins => ', developers' }) do
|
2005-10-29 14:40:49 -04:00
|
|
|
assert Post.find(1).readonly?
|
|
|
|
assert Post.find(1, :readonly => true).readonly?
|
|
|
|
assert !Post.find(1, :readonly => false).readonly?
|
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
Post.with_scope(:find => { :readonly => true }) do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert Post.find(1).readonly?
|
|
|
|
assert Post.find(1, :readonly => true).readonly?
|
|
|
|
assert !Post.find(1, :readonly => false).readonly?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_association_collection_method_missing_scoping_not_readonly
|
2005-10-28 00:53:22 -04:00
|
|
|
assert !Developer.find(1).projects.foo.readonly?
|
|
|
|
assert !Post.find(1).comments.foo.readonly?
|
|
|
|
end
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|