Module: NaranyaEcm::Rest::Persistence::ClassMethods

Defined in:
lib/naranya_ecm/rest/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = {}) ⇒ Object



46
47
48
49
50
# File 'lib/naranya_ecm/rest/persistence.rb', line 46

def create(attributes={})
  object_to_create = self.new attributes
  object_to_create.save
  object_to_create
end

#create!(attributes = {}) ⇒ Object



52
53
54
55
56
# File 'lib/naranya_ecm/rest/persistence.rb', line 52

def create!(attributes={})
  created_object = create(attributes)
  raise "ValidationError" unless created_object.persisted?
  created_object
end

#delete(id, options = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/naranya_ecm/rest/persistence.rb', line 58

def delete(id, options={})
  ActiveSupport::Notifications.instrument('ncontent_sdk.destroy') do |notification_payload|
    response = NContent::SDK::RESTClient.delete("#{self.path}/#{id}.json")
    notification_payload.merge!({ id: id }.merge(self.as_notification_payload))
  end
  true
end

#update(id, attributes) ⇒ Object

Updates an object (or multiple objects) and saves it to the server, if validations pass.

The resulting object is returned whether the object was saved successfully to the server or not.

Parameters

  • id - This should be the id or an array of ids to be updated.

  • attributes - This should be a hash of attributes or an array of hashes.

Examples

# Updates one resource
Person.update(15, user_name: 'Samuel', group: 'expert')

# Updates multiple resources
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)


36
37
38
39
40
41
42
43
44
# File 'lib/naranya_ecm/rest/persistence.rb', line 36

def update(id, attributes)
  if id.is_a?(Array)
    id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) }
  else
    object = find(id)
    object.update(attributes)
    object
  end
end