Class: CapsuleCRM::Associations::HasManyAssociation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association_name, defined_on, options) ⇒ HasManyAssociation

Public: Initialize a new CapsuleCRM::Associations::HasManyAssociation

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

:class_name - The String name of the belongs to
class
:source - The Symbol name of the accessor method on
the belongs to class

Examples

CapsuleCRM::Associations::HasManyAssociation.new(

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

)

Returns a CapsuleCRM::Associations::HasManyAssociation



25
26
27
28
29
# File 'lib/capsule_crm/associations/has_many_association.rb', line 25

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

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



4
5
6
# File 'lib/capsule_crm/associations/has_many_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/has_many_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/has_many_association.rb', line 4

def options
  @options
end

Instance Method Details

#embeddedObject



54
55
56
# File 'lib/capsule_crm/associations/has_many_association.rb', line 54

def embedded
  @embedded ||= !!options[:embedded]
end

#macroObject

Public: The type of association. Just a convenience method

Return a Symbol :has_many



50
51
52
# File 'lib/capsule_crm/associations/has_many_association.rb', line 50

def macro
  :has_many
end

#proxy(parent, collection = nil) ⇒ Object

Public: Build the HasManyProxy object

parent - The instance of the class that the has many assocation is defined on collection - An optional Array, Hash or Object to use as the target for the proxy. An Object will be coerced into an Array, a Hash will be turned in to an Object and then coerced into an Array.

Returns a CapsuleCRM::Associations::HasManyProxy



40
41
42
43
44
45
# File 'lib/capsule_crm/associations/has_many_association.rb', line 40

def proxy(parent, collection = nil)
  CapsuleCRM::Associations::HasManyProxy.new(
    parent, target_klass, build_target(parent, collection), source,
    embedded
  )
end

#sourceObject



58
59
60
# File 'lib/capsule_crm/associations/has_many_association.rb', line 58

def source
  @source ||= options[:source] || infer_source
end

#target_klassObject



62
63
64
65
# File 'lib/capsule_crm/associations/has_many_association.rb', line 62

def target_klass
  @target_klass ||=
    (options[:class_name] || infer_target_klass).constantize
end