2009-07-21 23:21:03 +00:00
|
|
|
module ActiveRecord
|
|
|
|
class Relation
|
2009-08-18 11:27:37 +00:00
|
|
|
delegate :to_sql, :to => :relation
|
2009-10-07 15:43:04 +00:00
|
|
|
delegate :length, :collect, :find, :map, :each, :to => :to_a
|
2009-07-21 23:21:03 +00:00
|
|
|
attr_reader :relation, :klass
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def initialize(klass, relation)
|
2009-08-18 17:22:38 +00:00
|
|
|
@klass, @relation = klass, relation
|
2009-08-27 23:03:46 +00:00
|
|
|
@readonly = false
|
2009-09-01 18:36:09 +00:00
|
|
|
@associations_to_preload = []
|
2009-10-05 18:24:08 +00:00
|
|
|
@eager_load_associations = []
|
2009-09-01 18:36:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def preload(association)
|
2009-10-05 18:24:08 +00:00
|
|
|
@associations_to_preload += association
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def eager_load(association)
|
|
|
|
@eager_load_associations += association
|
|
|
|
self
|
2009-08-27 23:03:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def readonly
|
|
|
|
@readonly = true
|
|
|
|
self
|
2009-07-21 23:21:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
2009-10-07 14:57:59 +00:00
|
|
|
records = if @eager_load_associations.any?
|
|
|
|
catch :invalid_query do
|
|
|
|
return @klass.send(:find_with_associations, {
|
2009-10-05 18:24:08 +00:00
|
|
|
:select => @relation.send(:select_clauses).join(', '),
|
|
|
|
:joins => @relation.joins(relation),
|
|
|
|
:group => @relation.send(:group_clauses).join(', '),
|
|
|
|
:order => @relation.send(:order_clauses).join(', '),
|
|
|
|
:conditions => @relation.send(:where_clauses).join("\n\tAND "),
|
2009-10-07 15:43:04 +00:00
|
|
|
:limit => @relation.taken,
|
|
|
|
:offset => @relation.skipped
|
2009-10-05 18:24:08 +00:00
|
|
|
},
|
|
|
|
ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
|
|
|
|
end
|
2009-10-07 14:57:59 +00:00
|
|
|
[]
|
2009-10-05 18:24:08 +00:00
|
|
|
else
|
2009-10-07 14:57:59 +00:00
|
|
|
@klass.find_by_sql(@relation.to_sql)
|
2009-10-05 18:24:08 +00:00
|
|
|
end
|
2009-08-27 23:03:46 +00:00
|
|
|
|
2009-10-07 14:57:59 +00:00
|
|
|
@klass.send(:preload_associations, records, @associations_to_preload) unless @associations_to_preload.empty?
|
|
|
|
records.each { |record| record.readonly! } if @readonly
|
|
|
|
|
2009-08-27 23:03:46 +00:00
|
|
|
records
|
2009-07-21 23:21:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def first
|
|
|
|
@relation = @relation.take(1)
|
|
|
|
to_a.first
|
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def select(selects)
|
2009-08-25 22:47:29 +00:00
|
|
|
selects.blank? ? self : Relation.new(@klass, @relation.project(selects))
|
2009-08-18 10:50:11 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def group(groups)
|
2009-08-25 22:47:29 +00:00
|
|
|
groups.blank? ? self : Relation.new(@klass, @relation.group(groups))
|
2009-08-18 11:27:37 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def order(orders)
|
2009-08-25 22:47:29 +00:00
|
|
|
orders.blank? ? self : Relation.new(@klass, @relation.order(orders))
|
2009-08-18 11:27:37 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def limit(limits)
|
2009-08-25 22:47:29 +00:00
|
|
|
limits.blank? ? self : Relation.new(@klass, @relation.take(limits))
|
2009-08-18 11:27:37 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def offset(offsets)
|
2009-08-25 22:47:29 +00:00
|
|
|
offsets.blank? ? self : Relation.new(@klass, @relation.skip(offsets))
|
2009-08-18 10:50:11 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def on(join)
|
2009-08-25 22:47:29 +00:00
|
|
|
join.blank? ? self : Relation.new(@klass, @relation.on(join))
|
2009-08-18 10:50:11 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def joins(join, join_type = nil)
|
|
|
|
if join.blank?
|
|
|
|
self
|
|
|
|
else
|
|
|
|
join = case join
|
|
|
|
when String
|
|
|
|
@relation.join(join)
|
|
|
|
when Hash, Array, Symbol
|
|
|
|
if @klass.send(:array_of_strings?, join)
|
|
|
|
@relation.join(join.join(' '))
|
|
|
|
else
|
|
|
|
@relation.join(@klass.send(:build_association_joins, join))
|
|
|
|
end
|
2009-08-18 11:18:24 +00:00
|
|
|
else
|
2009-08-18 17:10:03 +00:00
|
|
|
@relation.join(join, join_type)
|
2009-08-14 15:33:05 +00:00
|
|
|
end
|
2009-08-18 17:10:03 +00:00
|
|
|
Relation.new(@klass, join)
|
2009-08-14 15:33:05 +00:00
|
|
|
end
|
2009-07-31 19:08:22 +00:00
|
|
|
end
|
|
|
|
|
2009-08-18 17:10:03 +00:00
|
|
|
def conditions(conditions)
|
|
|
|
if conditions.blank?
|
|
|
|
self
|
|
|
|
else
|
2009-07-31 19:08:22 +00:00
|
|
|
conditions = @klass.send(:merge_conditions, conditions) if [String, Hash, Array].include?(conditions.class)
|
2009-08-18 17:10:03 +00:00
|
|
|
Relation.new(@klass, @relation.where(conditions))
|
2009-07-31 19:08:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-18 19:35:33 +00:00
|
|
|
def respond_to?(method)
|
|
|
|
if @relation.respond_to?(method) || Array.instance_methods.include?(method.to_s)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-21 23:21:03 +00:00
|
|
|
private
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
if @relation.respond_to?(method)
|
|
|
|
@relation.send(method, *args, &block)
|
|
|
|
elsif Array.instance_methods.include?(method.to_s)
|
|
|
|
to_a.send(method, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|