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
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ReadOnlyTest < ActiveRecord::TestCase
|
2010-08-26 00:22:27 -04:00
|
|
|
fixtures :posts, :comments, :developers, :projects, :developers_projects, :people, :readers
|
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? }
|
2009-12-28 05:46:21 -05:00
|
|
|
Developer.readonly(false).each { |d| assert !d.readonly? }
|
|
|
|
Developer.readonly(true).each { |d| assert d.readonly? }
|
|
|
|
Developer.readonly.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.
|
2009-12-28 05:46:21 -05:00
|
|
|
Developer.joins(' ').each { |d| assert !d.readonly? }
|
|
|
|
Developer.joins(' ').readonly(false).each { |d| assert !d.readonly? }
|
2005-10-28 00:53:22 -04:00
|
|
|
|
|
|
|
# Others do.
|
2009-12-28 05:46:21 -05:00
|
|
|
Developer.joins(', projects').each { |d| assert d.readonly? }
|
|
|
|
Developer.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
|
|
|
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?)
|
2009-12-28 05:46:21 -05:00
|
|
|
assert post.comments.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
|
|
|
|
2010-08-26 00:22:27 -04:00
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_by_id
|
|
|
|
assert !posts(:welcome).people.find(1).readonly?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_first
|
|
|
|
assert !posts(:welcome).people.first.readonly?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_last
|
|
|
|
assert !posts(:welcome).people.last.readonly?
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_readonly_scoping
|
2009-12-28 17:07:23 -05:00
|
|
|
Post.send(:with_scope, :find => { :conditions => '1=1' }) do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert !Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly(true).find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
Post.send(:with_scope, :find => { :joins => ' ' }) do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert !Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly.find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
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)
|
2009-12-28 17:07:23 -05:00
|
|
|
Post.send(:with_scope, :find => { :joins => ', developers' }) do
|
2005-10-29 14:40:49 -04:00
|
|
|
assert Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly.find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-29 14:40:49 -04:00
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
Post.send(:with_scope, :find => { :readonly => true }) do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly.find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_association_collection_method_missing_scoping_not_readonly
|
2011-01-02 15:33:18 -05:00
|
|
|
developer = Developer.find(1)
|
|
|
|
project = Post.find(1)
|
|
|
|
|
|
|
|
assert !developer.projects.all_as_method.first.readonly?
|
|
|
|
assert !developer.projects.all_as_scope.first.readonly?
|
|
|
|
|
|
|
|
assert !project.comments.all_as_method.first.readonly?
|
|
|
|
assert !project.comments.all_as_scope.first.readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|