From 7cce95b25ace33e04526d4490e487a080c1f9b96 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Thu, 27 Aug 2009 18:52:14 -0300 Subject: Add readonly support for relations. --- activerecord/lib/active_record/base.rb | 7 ++++++- activerecord/lib/active_record/relation.rb | 12 +++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') 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) -- cgit v1.2.3