aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-10-15 00:19:56 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-10-15 00:19:56 +0000
commit64fcb752f2c2f337918f74cd0cc6049a4cafb7a6 (patch)
treecc274a00c8d4eda38feaf8b872b680d0b227b6cd /activerecord
parent6a6df5f1e297a1b93233acbd851f123232ce1acf (diff)
downloadrails-64fcb752f2c2f337918f74cd0cc6049a4cafb7a6.tar.gz
rails-64fcb752f2c2f337918f74cd0cc6049a4cafb7a6.tar.bz2
rails-64fcb752f2c2f337918f74cd0cc6049a4cafb7a6.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG4
-rwxr-xr-xactiverecord/lib/active_record/base.rb25
-rwxr-xr-xactiverecord/test/readonly_test.rb31
3 files changed, 55 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 227ed7d488..9e5d24d9c6 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -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]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 8bd2a4530b..6d98cd16db 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -28,6 +28,8 @@ module ActiveRecord #:nodoc:
end
class ConfigurationError < StandardError #:nodoc:
end
+ class ReadOnlyRecord < StandardError #:nodoc:
+ end
class AttributeAssignmentError < ActiveRecordError #:nodoc:
attr_reader :exception, :attribute
@@ -336,10 +338,13 @@ module ActiveRecord #:nodoc:
# * <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 @@ module ActiveRecord #:nodoc:
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 @@ module ActiveRecord #:nodoc:
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 @@ module ActiveRecord #:nodoc:
# * 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 @@ module ActiveRecord #:nodoc:
@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
diff --git a/activerecord/test/readonly_test.rb b/activerecord/test/readonly_test.rb
new file mode 100755
index 0000000000..cbab35b49a
--- /dev/null
+++ b/activerecord/test/readonly_test.rb
@@ -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