diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-12-29 15:07:51 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-12-29 15:24:30 +0530 |
commit | 61fa111a5502c767b4b2c376eb5f06eb3077f45f (patch) | |
tree | b709e5bc5b0ffd8d91e082ad8edc495bcab50448 /activerecord | |
parent | cf4978313bbf908f85ff23c7a484bc856bed5181 (diff) | |
download | rails-61fa111a5502c767b4b2c376eb5f06eb3077f45f.tar.gz rails-61fa111a5502c767b4b2c376eb5f06eb3077f45f.tar.bz2 rails-61fa111a5502c767b4b2c376eb5f06eb3077f45f.zip |
Refactor Relation#readonly using attr_writer
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index b82a7d44f7..249e6475e7 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -5,9 +5,11 @@ module ActiveRecord attr_reader :relation, :klass, :associations_to_preload, :eager_load_associations include RelationalCalculations - def initialize(klass, relation, readonly = false, preload = [], eager_load = []) + + attr_writer :readonly + + def initialize(klass, relation, preload = [], eager_load = []) @klass, @relation = klass, relation - @readonly = readonly @associations_to_preload = preload @eager_load_associations = eager_load @loaded = false @@ -31,21 +33,24 @@ module ActiveRecord alias :& :merge def preload(*associations) - create_new_relation(@relation, @readonly, @associations_to_preload + Array.wrap(associations)) + create_new_relation(@relation, @associations_to_preload + Array.wrap(associations)) end def eager_load(*associations) - create_new_relation(@relation, @readonly, @associations_to_preload, @eager_load_associations + Array.wrap(associations)) + create_new_relation(@relation, @associations_to_preload, @eager_load_associations + Array.wrap(associations)) end def readonly(status = true) - status.nil? ? create_new_relation : create_new_relation(@relation, status) + relation = create_new_relation + relation.readonly = status + relation end def select(selects) if selects.present? - frozen = @relation.joins(relation).present? ? false : @readonly - create_new_relation(@relation.project(selects), frozen) + relation = create_new_relation(@relation.project(selects)) + relation.readonly = @relation.joins(relation).present? ? false : @readonly + relation else create_new_relation end @@ -126,7 +131,9 @@ module ActiveRecord @relation.join(join, join_type) end - create_new_relation(join_relation, true) + relation = create_new_relation(join_relation) + relation.readonly = true + relation end def where(*args) @@ -352,8 +359,10 @@ module ActiveRecord end end - def create_new_relation(relation = @relation, readonly = @readonly, preload = @associations_to_preload, eager_load = @eager_load_associations) - self.class.new(@klass, relation, readonly, preload, eager_load) + def create_new_relation(relation = @relation, preload = @associations_to_preload, eager_load = @eager_load_associations) + relation = self.class.new(@klass, relation, preload, eager_load) + relation.readonly = @readonly + relation end def where_clause(join_string = "\n\tAND ") |