Class: ActiveRecord::Associations::AssociationProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/associations/association_proxy.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ AssociationProxy

Returns a new instance of AssociationProxy.



10
11
12
13
14
# File 'lib/active_record/associations/association_proxy.rb', line 10

def initialize(owner, reflection)
  @owner, @reflection = owner, reflection
  Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



121
122
123
124
125
# File 'lib/active_record/associations/association_proxy.rb', line 121

def method_missing(method, *args, &block)
  if load_target        
    @target.send(method, *args, &block)
  end
end

Instance Attribute Details

#reflectionObject (readonly)

Returns the value of attribute reflection.



4
5
6
# File 'lib/active_record/associations/association_proxy.rb', line 4

def reflection
  @reflection
end

Instance Method Details

#===(other) ⇒ Object

Explicitly proxy === because the instance method removal above doesn’t catch it.



34
35
36
37
# File 'lib/active_record/associations/association_proxy.rb', line 34

def ===(other)
  load_target
  other === @target
end

#aliased_table_nameObject



39
40
41
# File 'lib/active_record/associations/association_proxy.rb', line 39

def aliased_table_name
  @reflection.klass.table_name
end

#conditionsObject Also known as: sql_conditions



43
44
45
# File 'lib/active_record/associations/association_proxy.rb', line 43

def conditions
  @conditions ||= interpolate_sql(sanitize_sql(@reflection.options[:conditions])) if @reflection.options[:conditions]
end

#loadedObject



62
63
64
# File 'lib/active_record/associations/association_proxy.rb', line 62

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/active_record/associations/association_proxy.rb', line 58

def loaded?
  @loaded
end

#proxy_ownerObject



16
17
18
# File 'lib/active_record/associations/association_proxy.rb', line 16

def proxy_owner
  @owner
end

#proxy_reflectionObject



20
21
22
# File 'lib/active_record/associations/association_proxy.rb', line 20

def proxy_reflection
  @reflection
end

#proxy_respond_to?Object



5
# File 'lib/active_record/associations/association_proxy.rb', line 5

alias_method :proxy_respond_to?, :respond_to?

#proxy_targetObject



24
25
26
# File 'lib/active_record/associations/association_proxy.rb', line 24

def proxy_target
  @target
end

#reloadObject



53
54
55
56
# File 'lib/active_record/associations/association_proxy.rb', line 53

def reload
  reset
  load_target
end

#resetObject



48
49
50
51
# File 'lib/active_record/associations/association_proxy.rb', line 48

def reset
  @target = nil
  @loaded = false
end

#respond_to?(symbol, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_record/associations/association_proxy.rb', line 28

def respond_to?(symbol, include_priv = false)
  proxy_respond_to?(symbol, include_priv) || (load_target && @target.respond_to?(symbol, include_priv))
end

#targetObject



66
67
68
# File 'lib/active_record/associations/association_proxy.rb', line 66

def target
  @target
end

#target=(target) ⇒ Object



70
71
72
73
# File 'lib/active_record/associations/association_proxy.rb', line 70

def target=(target)
  @target = target
  loaded
end