Class: CapsuleCRM::Associations::BelongsToAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/capsule_crm/associations/belongs_to_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association_name, defined_on, options) ⇒ BelongsToAssociation

Public: Initialize a new CapsuleCRM::Associations::BelongsToAssociation

association_name - The Symbol name of the association defined_on - The String name of the class that this association is defined on options - The Hash of association options

foreign_key - The String foreign_key column name
class_name  - The String name of the parent class

Examples

CapsuleCRM::Associations::BelongsToAssociation.new(

:person, 'CapsuleCRM::Opportunity', class_name: 'CapsuleCRM::Person'

)

Returns a CapsuleCRM::Associations::BelongsToAssociation



22
23
24
25
26
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 22

def initialize(association_name, defined_on, options)
  @association_name = association_name
  @defined_on       = defined_on
  @options          = options
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



4
5
6
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 4

def association_name
  @association_name
end

#defined_onObject (readonly)

Returns the value of attribute defined_on.



4
5
6
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 4

def defined_on
  @defined_on
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 4

def options
  @options
end

Instance Method Details

#foreign_keyObject

Public: Build the foreign key column name. If a foreign key name was supplied in the options during initialization, then that is returned. Otherwise it is inferred from the association name

Returns a String foreign key name



41
42
43
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 41

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

#inverseObject



28
29
30
31
32
33
34
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 28

def inverse
  @inverse ||=
    target_klass.has_many_associations.find do |name, association|
    association.source == association_name &&
      association.target_klass == defined_on
  end.try(:last) if target_klass.respond_to?(:has_many_associations)
end

#macroObject

Public: The type of association. Just a convenience method

Returns a Symbol :belongs_to



66
67
68
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 66

def macro
  :belongs_to
end

#parent(object) ⇒ Object

Public: Find the parent object of the supplied object

object - The object to find the parent for

Examples

association = CapsuleCRM::Associations::BelongsToAssociation.new(

:person, 'CapsuleCRM::Opportunity', class_name: 'CapsuleCRM::Person'

) object = CapsuleCRM::Opportunity.first association.parent(object)

Returns an Object that is on the parent side of the belongs to association



59
60
61
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 59

def parent(object)
  target_klass.find(object.send(foreign_key))
end

#serializable_keyObject

Public: The key to use when serializing this association

Returns the String key



73
74
75
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 73

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

#serializeObject



77
78
79
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 77

def serialize
  @serialize ||= options[:serialize]
end