aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-08-27 20:03:46 -0300
committerEmilio Tagua <miloops@gmail.com>2009-08-27 20:03:46 -0300
commit6b67df70ab1bc42d9a05571144cdf5614a7d4a6a (patch)
treedfc105d13590e6d5b50d6fc689e18f77d7a750c2
parentf2c0725d79e29b02e30e7a4827851acc4a766730 (diff)
downloadrails-6b67df70ab1bc42d9a05571144cdf5614a7d4a6a.tar.gz
rails-6b67df70ab1bc42d9a05571144cdf5614a7d4a6a.tar.bz2
rails-6b67df70ab1bc42d9a05571144cdf5614a7d4a6a.zip
Revert "Revert "Add readonly support for relations.""
This reverts commit f2c0725d79e29b02e30e7a4827851acc4a766730.
-rwxr-xr-xactiverecord/lib/active_record/base.rb7
-rw-r--r--activerecord/lib/active_record/relation.rb12
-rw-r--r--activerecord/test/cases/relations_test.rb6
3 files changed, 23 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index c11c049415..402d68c36e 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1722,7 +1722,7 @@ module ActiveRecord #:nodoc:
def construct_finder_arel(options = {}, scope = scope(:find))
# TODO add lock to Arel
- arel_table(options[:from]).
+ relation = arel_table(options[:from]).
joins(construct_join(options[:joins], scope)).
conditions(construct_conditions(options[:conditions], scope)).
select(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))).
@@ -1730,6 +1730,11 @@ module ActiveRecord #:nodoc:
order(construct_order(options[:order], scope)).
limit(construct_limit(options[:limit], scope)).
offset(construct_offset(options[:offset], scope))
+
+ relation = relation.readonly if options[:readonly]
+
+ relation
+
end
def construct_finder_sql(options, scope = scope(:find))
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 570ba3f80d..4b53857d36 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -5,10 +5,20 @@ module ActiveRecord
def initialize(klass, relation)
@klass, @relation = klass, relation
+ @readonly = false
+ end
+
+ def readonly
+ @readonly = true
+ self
end
def to_a
- @klass.find_by_sql(@relation.to_sql)
+ records = @klass.find_by_sql(@relation.to_sql)
+
+ records.each { |record| record.readonly! } if @readonly
+
+ records
end
def each(&block)
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 6a39aa6e40..655eb3314d 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -79,5 +79,11 @@ class RelationTest < ActiveRecord::TestCase
assert relation.respond_to?(method)
end
end
+
+ def test_find_with_readonly_option
+ Developer.all.each { |d| assert !d.readonly? }
+ Developer.all.readonly.each { |d| assert d.readonly? }
+ Developer.all(:readonly => true).each { |d| assert d.readonly? }
+ end
end