Module: Her::Model::ORM::SaveMethods

Extended by:
ActiveSupport::Concern
Included in:
Her::Model::ORM
Defined in:
lib/her/model/orm/save_methods.rb

Instance Method Summary collapse

Instance Method Details

#create_or_updateObject



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/her/model/orm/save_methods.rb', line 31

def create_or_update
  params = to_params
  resource = self

  if @data[:id]
    hooks = [:update, :save]
    method = :put
  else
    hooks = [:create, :save]
    method = :post
  end

  self.class.wrap_in_hooks(resource, *hooks) do |resource, klass|
    klass.request(params.merge(_method: method, _path: request_path)) do |parsed_data|
      self.data = self.class.parse(parsed_data[:data]) if parsed_data[:data].any?
      self. = parsed_data[:metadata]
      self.errors = parsed_data[:errors]

      return false if self.errors.any?
    end
  end

  self
end

#saveObject

Save a resource

Examples:

Save a resource after fetching it

@user = User.find(1)
# Fetched via GET "/users/1"
@user.fullname = "Tobias Fünke"
@user.save
# Called via PUT "/users/1"

Save a new resource by creating it

@user = User.new({ fullname: "Tobias Fünke" })
@user.save
# Called via POST "/users"


20
21
22
# File 'lib/her/model/orm/save_methods.rb', line 20

def save
  create_or_update
end

#save!Object

Raises:



24
25
26
27
28
29
# File 'lib/her/model/orm/save_methods.rb', line 24

def save!
  save
  raise RecordInvalid.new(self.errors) unless self.errors.empty?

  self
end