Skip to content
Browse files

Add readonly support for relations.

  • Loading branch information...
1 parent d56e987 commit 7cce95b25ace33e04526d4490e487a080c1f9b96 @miloops miloops committed Aug 27, 2009
View
7 activerecord/lib/active_record/base.rb
@@ -1722,14 +1722,19 @@ def default_select(qualified)
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]))).
group(construct_group(options[:group], options[:having], scope)).
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))
View
12 activerecord/lib/active_record/relation.rb
@@ -5,10 +5,20 @@ class Relation
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)
View
6 activerecord/test/cases/relations_test.rb
@@ -79,5 +79,11 @@ def test_relation_responds_to_delegated_methods
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

0 comments on commit 7cce95b

Please sign in to comment.
Something went wrong with that request. Please try again.