aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/extensions/base.rb
blob: 8fe00146d27f327141e44c9a157e44b3e72a82dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class ActiveRecord::Base
  class << self
    def cache
      @identity_map ||= IdentityMap.new
    end
    
    def relation
      @relation ||= ActiveRelation::Relations::Table.new(table_name)
    end
  end
  
  class IdentityMap
    def initialize
      @map = {}
    end
    
    def get(record, &block)
      @map[record] ||= yield
    end
  end
end

# all of the below disables normal AR behavior. It's rather destructive and purely for demonstration purposes (see scratch_spec).

class ActiveRecord::Associations::BelongsToAssociation
  def instantiate(record, joins = [])
    @target = proxy_reflection.klass.instantiate(record, joins)
    loaded
  end
 
  # this basically disables belongs_to from loading themselves
  def reload
    @target = 'hack'
  end
end

class ActiveRecord::Associations::AssociationCollection
  def instantiate(record, joins = [])
    @target << proxy_reflection.klass.instantiate(record, joins)
    loaded # technically, this isn't true. doesn't matter though
  end
end

class ActiveRecord::Associations::HasManyThroughAssociation
  def instantiate(record, joins = [])
    @target << proxy_reflection.klass.instantiate(record, joins)
    loaded # again, not really true.
  end
end