Permalink
Browse files

r3618@sedna: jeremy | 2005-10-14 12:06:03 -0700

 Branch for :join tainting
 r3631@sedna:  jeremy | 2005-10-14 20:13:49 -0700
 Introduce read-only records, object.readonly\!, object.readonly?, Foo.find(:all, :readonly => true).  Foo.find(:all, :joins => '...') also implies :readonly => true.


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2594 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 6a6df5f commit 64fcb752f2c2f337918f74cd0cc6049a4cafb7a6 @jeremy jeremy committed Oct 15, 2005
Showing with 55 additions and 5 deletions.
  1. +2 −2 activerecord/CHANGELOG
  2. +22 −3 activerecord/lib/active_record/base.rb
  3. +31 −0 activerecord/test/readonly_test.rb
@@ -1,5 +1,7 @@
*SVN*
+* Introduce read-only records. If you call object.readonly! then it will mark the object as read-only and raise ReadOnlyRecord if you call object.save. object.readonly? reports whether the object is read-only. Passing :readonly => true to any finder method will mark returned records as read-only. The :joins option now implies :readonly, so if you use this option, saving the same record will now fail. Use find_by_sql to work around.
+
* Avoid memleak in dev mode when using fcgi
* Simplified .clear on active record associations by using the existing delete_records method. #1906 [Caleb <me@cpb.ca>]
@@ -10,8 +12,6 @@
* Add ActiveRecord::Base.schema_format setting which specifies how databases should be dumped [Sam Stephenson]
-* Optimize postgresql selects. [skaes@web.de]
-
* Update DB2 adapter. #2206. [contact@maik-schmidt.de]
* Corrections to SQLServer native data types. #2267. [rails.20.clarry@spamgourmet.com]
@@ -28,6 +28,8 @@ class StaleObjectError < ActiveRecordError #:nodoc:
end
class ConfigurationError < StandardError #:nodoc:
end
+ class ReadOnlyRecord < StandardError #:nodoc:
+ end
class AttributeAssignmentError < ActiveRecordError #:nodoc:
attr_reader :exception, :attribute
@@ -336,10 +338,13 @@ class << self # Class methods
# * <tt>:limit</tt>: An integer determining the limit on the number of rows that should be returned.
# * <tt>:offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
+ # The records will be returned read-only since they will have attributes that do not correspond to the table's columns.
+ # Use <tt>find_by_sql</tt> to circumvent this limitation.
# * <tt>:include</tt>: Names associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
# to already defined associations. See eager loading under Associations.
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
# include the joined columns.
+ # * <tt>:readonly</tt>: Mark the returned records read-only so they cannot be saved or updated.
#
# Examples for find by id:
# Person.find(1) # returns the object for ID = 1
@@ -361,11 +366,16 @@ class << self # Class methods
def find(*args)
options = extract_options_from_args!(args)
+ # :joins implies :readonly => true
+ options[:readonly] = true if options[:joins]
+
case args.first
when :first
find(:all, options.merge(options[:include] ? { } : { :limit => 1 })).first
when :all
- options[:include] ? find_with_associations(options) : find_by_sql(construct_finder_sql(options))
+ records = options[:include] ? find_with_associations(options) : find_by_sql(construct_finder_sql(options))
+ records.each { |record| record.readonly! } if options[:readonly]
+ records
else
return args.first if args.first.kind_of?(Array) && args.first.empty?
expects_array = args.first.kind_of?(Array)
@@ -1052,9 +1062,9 @@ def extract_options_from_args!(args)
validate_find_options(options)
options
end
-
+
def validate_find_options(options)
- options.assert_valid_keys [:conditions, :include, :joins, :limit, :offset, :order, :select]
+ options.assert_valid_keys [:conditions, :include, :joins, :limit, :offset, :order, :select, :readonly]
end
def encode_quoted_value(value)
@@ -1110,6 +1120,7 @@ def new_record?
# * No record exists: Creates a new record with values matching those of the object attributes.
# * A record does exist: Updates the record with values matching those of the object attributes.
def save
+ raise ActiveRecord::ReadOnlyRecord if readonly?
create_or_update
end
@@ -1285,6 +1296,14 @@ def frozen?
@attributes.frozen?
end
+ def readonly?
+ @readonly == true
+ end
+
+ def readonly!
+ @readonly = true
+ end
+
private
def create_or_update
if new_record? then create else update end
@@ -0,0 +1,31 @@
+require 'abstract_unit'
+require 'fixtures/topic'
+
+class ReadOnlyTest < Test::Unit::TestCase
+ fixtures :topics
+
+ def test_cant_save_readonly_record
+ topic = Topic.find(:first)
+ assert !topic.readonly?
+
+ topic.readonly!
+ assert topic.readonly?
+
+ assert_nothing_raised do
+ topic.content = 'Luscious forbidden fruit.'
+ end
+
+ assert_raise(ActiveRecord::ReadOnlyRecord) { topic.save }
+ assert_raise(ActiveRecord::ReadOnlyRecord) { topic.save! }
+ end
+
+ def test_find_with_readonly_option
+ Topic.find(:all).each { |t| assert !t.readonly? }
+ Topic.find(:all, :readonly => false).each { |t| assert !t.readonly? }
+ Topic.find(:all, :readonly => true).each { |t| assert t.readonly? }
+ end
+
+ def test_find_with_joins_option_implies_readonly
+ Topic.find(:all, :joins => '').each { |t| assert t.readonly? }
+ end
+end

0 comments on commit 64fcb75

Please sign in to comment.