Class: Transactionable::RemoteCustomer

Inherits:
RemoteEntity
  • Object
show all
Defined in:
app/models/transactionable/remote_customer.rb

Instance Method Summary collapse

Methods inherited from RemoteEntity

#destroy_remote, #fetch, #remote_entity_type, #service_name, #synced?

Instance Method Details

#build_or_update_remoteObject



5
6
7
8
# File 'app/models/transactionable/remote_customer.rb', line 5

def build_or_update_remote
  build_remote unless synced?
  update_remote
end

#build_remoteObject



10
11
12
13
14
# File 'app/models/transactionable/remote_customer.rb', line 10

def build_remote
  balanced_customer = Balanced::Customer.new.save
  self.uri = balanced_customer.uri
  self.synced_at = Time.now
end

#update_remoteObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/transactionable/remote_customer.rb', line 16

def update_remote
  balanced_customer = fetch
  if self.local_entity.respond_to?(:sync_attributes)
    balanced_customer_attributes = local_entity.sync_attributes
    non_address_keys = [:name, :email, :phone]
    new_customer_attributes = balanced_customer_attributes.reject { |k,v| v.blank? }
    address = new_customer_attributes.slice!(*non_address_keys)
    new_customer_attributes.merge!(address: address)
    new_customer_attributes.each { |k,v| balanced_customer.send("#{k}=", v) }
    balanced_customer.save
  else
    balanced_customer = fetch
    balanced_customer.name = "#{self.local_entity_type} #{self.local_entity_id}"
    balanced_customer.meta = {
                                entity: self.local_entity_type,
                                entity_id: self.local_entity_id
                             }
    balanced_customer.save
  end
  update_attribute(:synced_at, Time.now) unless new_record?
  balanced_customer
end