Class: XapixClient::Resource

Inherits:
JsonApiClient::Resource
  • Object
show all
Defined in:
lib/xapix_client/resource.rb

Direct Known Subclasses

InputEndpoint, Schema, SchemaRelationship

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transactional_create(attributes = {}, associated_records) ⇒ Object



9
10
11
12
13
# File 'lib/xapix_client/resource.rb', line 9

def transactional_create(attributes = {}, associated_records)
  new(attributes).tap do |resource|
    resource.transactional_save(associated_records)
  end      
end

Instance Method Details

#transactional_save(associated_records) ⇒ Boolean

Commit the current changes to the resource to the remote server. If the resource was previously loaded from the server, we will try to update the record. Otherwise if it’s a new record, then we will try to create it

Returns:

  • (Boolean)

    Whether or not the save succeeded



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/xapix_client/resource.rb', line 22

def transactional_save(associated_records)
  return false unless valid?
  return false unless associated_records.all?(&:valid?)

  self.last_result_set = if persisted?
    #TODO
    raise 'NOT YET IMPLEMENTED'
    self.class.requestor.update(self)
  else
    self.class.requestor.transactional_create([self] + associated_records)
  end

  if last_result_set.has_errors?
    last_result_set.errors.each do |error|
      if error.source_parameter
        errors.add(error.source_parameter, error.title)
      else
        errors.add(:base, error.title)
      end
    end
    false
  else
    self.errors.clear if self.errors
    mark_as_persisted!
    associated_records.each(&:mark_as_persisted!)
    if updated = last_result_set.first
      self.attributes = updated.attributes
      self.relationships.attributes = updated.relationships.attributes
      clear_changes_information
    end
    true
  end
end