mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Revert "Revert "Add readonly support for relations.""
This reverts commit f2c0725d79
.
This commit is contained in:
parent
f2c0725d79
commit
6b67df70ab
3 changed files with 23 additions and 2 deletions
|
@ -1722,7 +1722,7 @@ module ActiveRecord #:nodoc:
|
||||||
|
|
||||||
def construct_finder_arel(options = {}, scope = scope(:find))
|
def construct_finder_arel(options = {}, scope = scope(:find))
|
||||||
# TODO add lock to Arel
|
# TODO add lock to Arel
|
||||||
arel_table(options[:from]).
|
relation = arel_table(options[:from]).
|
||||||
joins(construct_join(options[:joins], scope)).
|
joins(construct_join(options[:joins], scope)).
|
||||||
conditions(construct_conditions(options[:conditions], scope)).
|
conditions(construct_conditions(options[:conditions], scope)).
|
||||||
select(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))).
|
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)).
|
order(construct_order(options[:order], scope)).
|
||||||
limit(construct_limit(options[:limit], scope)).
|
limit(construct_limit(options[:limit], scope)).
|
||||||
offset(construct_offset(options[:offset], scope))
|
offset(construct_offset(options[:offset], scope))
|
||||||
|
|
||||||
|
relation = relation.readonly if options[:readonly]
|
||||||
|
|
||||||
|
relation
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def construct_finder_sql(options, scope = scope(:find))
|
def construct_finder_sql(options, scope = scope(:find))
|
||||||
|
|
|
@ -5,10 +5,20 @@ module ActiveRecord
|
||||||
|
|
||||||
def initialize(klass, relation)
|
def initialize(klass, relation)
|
||||||
@klass, @relation = klass, relation
|
@klass, @relation = klass, relation
|
||||||
|
@readonly = false
|
||||||
|
end
|
||||||
|
|
||||||
|
def readonly
|
||||||
|
@readonly = true
|
||||||
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
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
|
end
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
|
|
|
@ -79,5 +79,11 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
assert relation.respond_to?(method)
|
assert relation.respond_to?(method)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue