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.



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

def initialize(owner, reflection)
  @owner, @reflection = owner, reflection
  proxy_extend(reflection.options[:extend]) if reflection.options[:extend]
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



108
109
110
111
# File 'lib/active_record/associations/association_proxy.rb', line 108

def method_missing(method, *args, &block)
  load_target
  @target.send(method, *args, &block)
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.



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

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

#aliased_table_nameObject



26
27
28
# File 'lib/active_record/associations/association_proxy.rb', line 26

def aliased_table_name
  @reflection.klass.table_name
end

#conditionsObject Also known as: sql_conditions



30
31
32
# File 'lib/active_record/associations/association_proxy.rb', line 30

def conditions
  @conditions ||= eval("%(#{@reflection.active_record.send :sanitize_sql, @reflection.options[:conditions]})") if @reflection.options[:conditions]
end

#loadedObject



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

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/active_record/associations/association_proxy.rb', line 45

def loaded?
  @loaded
end

#proxy_respond_to?Object



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

alias_method :proxy_respond_to?, :respond_to?

#reloadObject



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

def reload
  reset
  load_target
end

#resetObject



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

def reset
  @target = nil
  @loaded = false
end

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

Returns:

  • (Boolean)


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

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

#targetObject



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

def target
  @target
end

#target=(target) ⇒ Object



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

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