Module: Service::Persistence

Included in:
Record
Defined in:
lib/service/persistence.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/service/persistence.rb', line 4

def self.included(base)
  base.send :include, Callback
end

Instance Method Details

#destroyObject

Delete QuickBook record, require Id attribute This will delete or make record Inactive based on object class. For Invoice it will delete record from server else make record Active false.

Raises:

  • (RecordDeleteError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/service/persistence.rb', line 38

def destroy
  before_destroy
  
  entities_for_delete = ["INVOICE", "PAYMENT", "SALESRECEIPT", "REFUNDRECEIPT"]
  
  raise RecordDeleteError, "Can't delete record without ID" if self["Id"].to_i == 0
  if(entities_for_delete.index(qb_entity.upcase))
    raise RecordDeleteError, "Can't delete new record" if self.is_new_record 
    quickbook_gateway.post(qb_entity.downcase, {:operation => :delete} , {"Id" => self["Id"], "SyncToken" => self["SyncToken"]}.to_json())
    self.attributes = {}
    self.is_new_record = true
  else
    self.Active = "false"
    self.save!
  end
  
  after_destroy
end

#save!Object

Create or Update current instance to QuickBook



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/service/persistence.rb', line 18

def save!
  new_record = self.is_new_record
  
  before_save
  before_create if new_record
  before_update unless new_record
  
  params = {}
  params[:operation] = :update unless(self.is_new_record)
  
  self.attributes = quickbook_gateway.post(qb_entity, params , self.attributes.to_json())[entity]
  
  after_update unless new_record
  after_create if new_record
  after_save
end