Class: SugarCRM::AssociationCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcrm/associations/association_collection.rb

Overview

A class for handling association collections. Basically just an extension of Array doesn’t actually load the records from Sugar until you invoke one of the public methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, association, preload = false) ⇒ AssociationCollection

creates a new instance of an AssociationCollection Owner is the parent object, and association is the target



10
11
12
13
14
15
16
# File 'lib/sugarcrm/associations/association_collection.rb', line 10

def initialize(owner, association, preload=false)
  @loaded     = false
  @owner      = owner
  @association= association
  load if preload
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

delegate undefined methods to the @collection array E.g. contact.cases should behave like an array and allow ‘length`, `size`, `each`, etc.



79
80
81
82
# File 'lib/sugarcrm/associations/association_collection.rb', line 79

def method_missing(method_name, *args, &block)
  load
  @collection.send(method_name.to_sym, *args, &block)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/sugarcrm/associations/association_collection.rb', line 6

def collection
  @collection
end

Instance Method Details

#<<(record) ⇒ Object Also known as: add

Add records to this association, saving any unsaved records before adding them.

Returns self so method calls may be chained. Be sure to call save on the association to commit any association changes



66
67
68
69
70
71
72
73
74
# File 'lib/sugarcrm/associations/association_collection.rb', line 66

def <<(record)
  load
  record.save! if record.new?
  result = true
  result = false if include?(record)
  @owner.update_association_cache_for(@association, record, :add)
  record.update_association_cache_for(record.associations.find!(@owner).link_field, @owner, :add)
  result && self
end

#addedObject

return any added elements



38
39
40
41
# File 'lib/sugarcrm/associations/association_collection.rb', line 38

def added
  load
  @collection - @original
end

#changed?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/sugarcrm/associations/association_collection.rb', line 18

def changed?
  return false unless loaded?
  return true if added.length > 0
  return true if removed.length > 0
  false
end

#delete(record) ⇒ Object Also known as: remove

Removes a record from the collection, uses the id of the record as a test for inclusion.

Raises:



50
51
52
53
54
# File 'lib/sugarcrm/associations/association_collection.rb', line 50

def delete(record)
  load
  raise InvalidRecord, "#{record.class} does not have a valid :id!" if record.id.empty?
  @collection.delete record
end

#include?(record) ⇒ Boolean

Checks if a record is included in the current collection. Uses id’s as comparison

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/sugarcrm/associations/association_collection.rb', line 58

def include?(record)
  load
  @collection.include? record
end

#loadObject



29
30
31
# File 'lib/sugarcrm/associations/association_collection.rb', line 29

def load
  load_associated_records unless loaded?
end

#loaded?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/sugarcrm/associations/association_collection.rb', line 25

def loaded?
  @loaded
end

#reloadObject



33
34
35
# File 'lib/sugarcrm/associations/association_collection.rb', line 33

def reload
  load_associated_records
end

#removedObject

return any removed elements



44
45
46
47
# File 'lib/sugarcrm/associations/association_collection.rb', line 44

def removed
  load
  @original - @collection
end

#respond_to?(method_name) ⇒ Boolean

respond correctly for delegated methods

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/sugarcrm/associations/association_collection.rb', line 85

def respond_to?(method_name)
  load
  return true if @collection.respond_to? method_name
  super
end

#saveObject



91
92
93
94
95
96
97
# File 'lib/sugarcrm/associations/association_collection.rb', line 91

def save
  begin
    save!
  rescue
    return false
  end
end

#save!Object

Pushes collection changes to SugarCRM, and updates the state of the collection



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sugarcrm/associations/association_collection.rb', line 100

def save!
  load
  added.each do |record|
    associate!(record)
  end
  removed.each do |record|
    disassociate!(record)
  end
  reload
  true
end