Browse files

Refactor Relation#readonly using attr_writer

  • Loading branch information...
1 parent cf49783 commit 61fa111a5502c767b4b2c376eb5f06eb3077f45f @lifo lifo committed Dec 29, 2009
Showing with 19 additions and 10 deletions.
  1. +19 −10 activerecord/lib/active_record/relation.rb
View
29 activerecord/lib/active_record/relation.rb
@@ -5,9 +5,11 @@ class Relation
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 @@ def merge(r)
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 @@ def joins(join, join_type = nil)
@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 @@ def find_some(ids)
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 ")

0 comments on commit 61fa111

Please sign in to comment.