2009-07-21 19:21:03 -04:00
|
|
|
module ActiveRecord
|
|
|
|
class Relation
|
2009-08-18 07:27:37 -04:00
|
|
|
delegate :to_sql, :to => :relation
|
2009-10-07 11:43:04 -04:00
|
|
|
delegate :length, :collect, :find, :map, :each, :to => :to_a
|
2009-07-21 19:21:03 -04:00
|
|
|
attr_reader :relation, :klass
|
|
|
|
|
2009-12-25 16:23:10 -05:00
|
|
|
def initialize(klass, relation, readonly = false, preload = [], eager_load = [])
|
2009-08-18 13:22:38 -04:00
|
|
|
@klass, @relation = klass, relation
|
2009-12-25 16:23:10 -05:00
|
|
|
@readonly = readonly
|
|
|
|
@associations_to_preload = preload
|
|
|
|
@eager_load_associations = eager_load
|
2009-12-26 04:37:00 -05:00
|
|
|
@loaded = false
|
2009-09-01 14:36:09 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 04:10:45 -05:00
|
|
|
def preload(*associations)
|
2009-12-26 03:56:02 -05:00
|
|
|
create_new_relation(@relation, @readonly, @associations_to_preload + Array.wrap(associations))
|
2009-10-05 14:24:08 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 04:10:45 -05:00
|
|
|
def eager_load(*associations)
|
2009-12-26 03:56:02 -05:00
|
|
|
create_new_relation(@relation, @readonly, @associations_to_preload, @eager_load_associations + Array.wrap(associations))
|
2009-08-27 19:03:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def readonly
|
2009-12-26 03:56:02 -05:00
|
|
|
create_new_relation(@relation, true)
|
2009-07-21 19:21:03 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def select(selects)
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.project(selects))
|
2009-08-18 06:50:11 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def group(groups)
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.group(groups))
|
2009-08-18 07:27:37 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def order(orders)
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.order(orders))
|
2009-08-18 07:27:37 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def limit(limits)
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.take(limits))
|
2009-08-18 07:27:37 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def offset(offsets)
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.skip(offsets))
|
2009-08-18 06:50:11 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def on(join)
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.on(join))
|
2009-08-18 06:50:11 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def joins(join, join_type = nil)
|
2009-12-25 17:37:50 -05:00
|
|
|
join = case join
|
|
|
|
when String
|
|
|
|
@relation.join(join)
|
|
|
|
when Hash, Array, Symbol
|
|
|
|
if @klass.send(:array_of_strings?, join)
|
|
|
|
@relation.join(join.join(' '))
|
2009-08-18 07:18:24 -04:00
|
|
|
else
|
2009-12-25 17:37:50 -05:00
|
|
|
@relation.join(@klass.send(:build_association_joins, join))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@relation.join(join, join_type)
|
2009-08-14 11:33:05 -04:00
|
|
|
end
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(join)
|
2009-07-31 15:08:22 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 02:27:34 -05:00
|
|
|
def where(*args)
|
|
|
|
if [String, Hash, Array].include?(args.first.class)
|
|
|
|
conditions = @klass.send(:merge_conditions, args.size > 1 ? Array.wrap(args) : args.first)
|
|
|
|
else
|
|
|
|
conditions = args.first
|
|
|
|
end
|
|
|
|
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.where(conditions))
|
2009-07-31 15:08:22 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 15:35:33 -04:00
|
|
|
def respond_to?(method)
|
2009-11-10 14:00:50 -05:00
|
|
|
@relation.respond_to?(method) || Array.method_defined?(method) || super
|
2009-08-18 15:35:33 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 04:37:00 -05:00
|
|
|
def to_a
|
|
|
|
return @records if loaded?
|
|
|
|
|
|
|
|
@records = if @eager_load_associations.any?
|
|
|
|
catch :invalid_query do
|
|
|
|
return @klass.send(:find_with_associations, {
|
|
|
|
: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 "),
|
|
|
|
:limit => @relation.taken,
|
|
|
|
:offset => @relation.skipped
|
|
|
|
},
|
|
|
|
ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
|
|
|
|
end
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
@klass.find_by_sql(@relation.to_sql)
|
|
|
|
end
|
|
|
|
|
|
|
|
@associations_to_preload.each {|associations| @klass.send(:preload_associations, @records, associations) }
|
|
|
|
@records.each { |record| record.readonly! } if @readonly
|
|
|
|
|
|
|
|
@loaded = true
|
|
|
|
@records
|
|
|
|
end
|
|
|
|
|
|
|
|
alias all to_a
|
|
|
|
|
|
|
|
def first
|
|
|
|
if loaded?
|
|
|
|
@records.first
|
|
|
|
else
|
|
|
|
@first ||= limit(1).to_a[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def loaded?
|
|
|
|
@loaded
|
|
|
|
end
|
|
|
|
|
2009-07-21 19:21:03 -04:00
|
|
|
private
|
2009-12-25 16:23:10 -05:00
|
|
|
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
if @relation.respond_to?(method)
|
|
|
|
@relation.send(method, *args, &block)
|
|
|
|
elsif Array.method_defined?(method)
|
|
|
|
to_a.send(method, *args, &block)
|
|
|
|
else
|
|
|
|
super
|
2009-07-21 19:21:03 -04:00
|
|
|
end
|
2009-12-25 16:23:10 -05:00
|
|
|
end
|
|
|
|
|
2009-12-26 03:56:02 -05:00
|
|
|
def create_new_relation(relation, readonly = @readonly, preload = @associations_to_preload, eager_load = @eager_load_associations)
|
|
|
|
Relation.new(@klass, relation, readonly, preload, eager_load)
|
2009-12-25 16:23:10 -05:00
|
|
|
end
|
|
|
|
|
2009-07-21 19:21:03 -04:00
|
|
|
end
|
|
|
|
end
|