diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-10-15 00:19:56 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-10-15 00:19:56 +0000 |
commit | 64fcb752f2c2f337918f74cd0cc6049a4cafb7a6 (patch) | |
tree | cc274a00c8d4eda38feaf8b872b680d0b227b6cd /activerecord/lib/active_record | |
parent | 6a6df5f1e297a1b93233acbd851f123232ce1acf (diff) | |
download | rails-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/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 25 |
1 files changed, 22 insertions, 3 deletions
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 |