Module: ApiRecord::Saveable

Extended by:
ActiveSupport::Concern
Includes:
Base, Findable
Included in:
All
Defined in:
lib/api_record/saveable.rb

Instance Method Summary collapse

Methods included from Findable

#new_record?

Methods included from Base

#has_error?, #inspect, #validate!

Instance Method Details

#perform_save_requestObject



70
71
72
73
74
75
76
77
78
# File 'lib/api_record/saveable.rb', line 70

def perform_save_request
  params = self.class.request_params(attributes)
  params.delete("id")
  if new_record?
    request(:post, "#{self.class.api_path}", params)
  else
    request(:put, "#{self.class.api_path}/#{id}", params)
  end
end

#perform_save_request!Object



60
61
62
63
64
65
66
67
68
# File 'lib/api_record/saveable.rb', line 60

def perform_save_request!
  params = self.class.request_params(attributes)
  params.delete("id")
  if new_record?
    request!(:post, "#{self.class.api_path}", params)
  else
    request!(:put, "#{self.class.api_path}/#{id}", params)
  end
end

#saveObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/api_record/saveable.rb', line 35

def save
  return false unless valid?

  result = perform_save_request
  if result
    if response.body.present?
      body = response.body.deep_transform_keys!(&:underscore)
      self.attributes = body
    end
  end
  result
end

#save!Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/api_record/saveable.rb', line 48

def save!
  validate!

  perform_save_request!

  if response.body.present?
    body = response.body.deep_transform_keys!(&:underscore)
    self.attributes = body
  end
  self
end

#update(params) ⇒ Object



30
31
32
33
# File 'lib/api_record/saveable.rb', line 30

def update(params)
  self.attributes = params
  save
end

#update!(params) ⇒ Object



25
26
27
28
# File 'lib/api_record/saveable.rb', line 25

def update!(params)
  self.attributes = params
  save!
end