Class: SmoothOperator::Associations::AssociationReflection

Inherits:
Reflection
  • Object
show all
Defined in:
lib/smooth_operator/associations/association_reflection.rb

Instance Attribute Summary collapse

Attributes inherited from Reflection

#klass, #name, #options

Instance Method Summary collapse

Methods inherited from Reflection

#plural_name, #single_name

Constructor Details

#initialize(association, related_reflection, options) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



9
10
11
12
13
# File 'lib/smooth_operator/associations/association_reflection.rb', line 9

def initialize(association, related_reflection, options)
  super(association, options)

  @related_reflection, @macro = related_reflection, options[:macro]
end

Instance Attribute Details

#macroObject (readonly)

Returns the value of attribute macro.



7
8
9
# File 'lib/smooth_operator/associations/association_reflection.rb', line 7

def macro
  @macro
end

Returns the value of attribute related_reflection.



7
8
9
# File 'lib/smooth_operator/associations/association_reflection.rb', line 7

def related_reflection
  @related_reflection
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/smooth_operator/associations/association_reflection.rb', line 55

def belongs_to?
  macro == :belongs_to
end

#collection?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/smooth_operator/associations/association_reflection.rb', line 59

def collection?
  has_many?
end

#foreign_keyObject



19
20
21
# File 'lib/smooth_operator/associations/association_reflection.rb', line 19

def foreign_key
  @foreign_key ||= options[:foreign_key] || foreign_key_default
end

#has_many?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/smooth_operator/associations/association_reflection.rb', line 47

def has_many?
  macro == :has_many
end

#has_one?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/smooth_operator/associations/association_reflection.rb', line 51

def has_one?
  macro == :has_one
end

#primary_keyObject



15
16
17
# File 'lib/smooth_operator/associations/association_reflection.rb', line 15

def primary_key
  @primary_key ||= options[:primary_key] || :id
end

#primary_key_of(object) ⇒ Object



43
44
45
# File 'lib/smooth_operator/associations/association_reflection.rb', line 43

def primary_key_of(object)
  object.send(primary_key)
end

#rails_serialization?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/smooth_operator/associations/association_reflection.rb', line 63

def rails_serialization?
  options[:rails_serialization] == true
end

#set_foreign_key(object, id) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/smooth_operator/associations/association_reflection.rb', line 33

def set_foreign_key(object, id)
  setter = "#{foreign_key}="

  if object.respond_to?(setter)
    object.send(setter, id)
  elsif object.respond_to?("send_to_representative")
    object.send_to_representative(setter, id)
  end
end

#set_relational_keys(origin, destination) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/smooth_operator/associations/association_reflection.rb', line 23

def set_relational_keys(origin, destination)
  return nil if options[:standalone] == true

  if has_many? || has_one?
    set_foreign_key(destination, primary_key_of(origin))
  elsif belongs_to?
    set_foreign_key(origin, primary_key_of(destination))
  end
end