Module: NaranyaEcm::Rest::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/naranya_ecm/rest/persistence.rb

Defined Under Namespace

Modules: ClassMethods Classes: ReadOnlyResource, ResourceNotDestroyed, ResourceNotSaved

Instance Method Summary collapse

Instance Method Details

#assign_attributes(given_attributes) ⇒ Object



215
216
217
218
219
# File 'lib/naranya_ecm/rest/persistence.rb', line 215

def assign_attributes(given_attributes)
  given_attributes.each do |key, val|
    self.public_send "#{key}=", val
  end
end

#deleteObject

Deletes the resource at the server and freezes this instance to reflect that no changes should be made (since they can’t be persisted). Returns the frozen instance.

The row is simply removed with an SQL DELETE statement on the resource’s primary key, and no callbacks are executed.

To enforce the object’s before_destroy and after_destroy callbacks or any :dependent association options, use #destroy.



153
154
155
156
157
# File 'lib/naranya_ecm/rest/persistence.rb', line 153

def delete
  self.class.delete(id) if persisted?
  @destroyed = true
  freeze
end

#destroyObject

Deletes the resource at the server and freezes this instance to reflect that no changes should be made (since they can’t be persisted).

There’s a series of callbacks associated with destroy. If the before_destroy callback return false the action is cancelled and destroy returns false. See Activeresource::Callbacks for further details.



166
167
168
169
170
171
172
173
174
# File 'lib/naranya_ecm/rest/persistence.rb', line 166

def destroy
  run_callbacks :destroy do
    #raise ReadOnlyresource if readonly?
    #destroy_associations
    self.class.delete(self.id) if persisted?
    @destroyed = true
    freeze
  end
end

#destroy!Object

Deletes the resource at the server and freezes this instance to reflect that no changes should be made (since they can’t be persisted).

There’s a series of callbacks associated with destroy!. If the before_destroy callback return false the action is cancelled and destroy! raises Activeresource::resourceNotDestroyed. See Activeresource::Callbacks for further details.



183
184
185
# File 'lib/naranya_ecm/rest/persistence.rb', line 183

def destroy!
  destroy || raise(ResourceNotDestroyed, self)
end

#destroyed?Boolean

Returns true if this object has been destroyed, otherwise returns false.

Returns:

  • (Boolean)


87
88
89
# File 'lib/naranya_ecm/rest/persistence.rb', line 87

def destroyed?
  @destroyed
end

#new_resource?Boolean Also known as: new?, new_record?

Returns true if this object hasn’t been saved yet – that is, a resource for the object doesn’t exist at the server yet; otherwise, returns false.

Returns:

  • (Boolean)


79
80
81
# File 'lib/naranya_ecm/rest/persistence.rb', line 79

def new_resource?
  @new_resource
end

#persisted?Boolean

Returns true if the resource is persisted, i.e. it’s not a new resource and it was not destroyed, otherwise returns false.

Returns:

  • (Boolean)


93
94
95
# File 'lib/naranya_ecm/rest/persistence.rb', line 93

def persisted?
  !(new_resource? || destroyed?)
end

#saveObject

Saves the resource at the server.

If the model is new a resource gets created at the server, otherwise the existing resource gets updated.

By default, save always run validations. If any of them fail the action is cancelled and save returns false. However, if you supply validate: false, validations are bypassed altogether. See ActiveModel::Validations for more information.

There’s a series of callbacks associated with save. If any of the before_* callbacks return false the action is cancelled and save returns false. See ActiveModel::Callbacks for further details.

Attributes marked as readonly are silently ignored if the resource is being updated.



114
115
116
117
118
119
120
121
# File 'lib/naranya_ecm/rest/persistence.rb', line 114

def save(*)
  run_callbacks :save do
    @previously_changed = changes
    result = create_or_update
    @changed_attributes.clear
    result
  end
end

#save!Object

Saves the model.

If the model is new a resource gets created at the server, otherwise the existing resource gets updated.

With save! validations always run. If any of them fail ActiveModel::resourceInvalid gets raised. See ActiveModel::Validations for more information.

There’s a series of callbacks associated with save!. If any of the before_* callbacks return false the action is cancelled and save! raises ActiveModel::resourceNotSaved. See ActiveModel::Callbacks for further details.

Attributes marked as readonly are silently ignored if the resource is being updated.



139
140
141
# File 'lib/naranya_ecm/rest/persistence.rb', line 139

def save!(*)
  create_or_update || raise(resourceNotSaved.new(nil, self))
end

#touchObject



72
73
74
75
# File 'lib/naranya_ecm/rest/persistence.rb', line 72

def touch
  @is_touch = true
  self.update({})
end

#update(attributes) ⇒ Object Also known as: update_attributes

Updates the attributes of the model from the passed-in hash and saves the resource, all wrapped in a transaction. If the object is invalid, the saving will fail and false will be returned.



210
211
212
213
# File 'lib/naranya_ecm/rest/persistence.rb', line 210

def update(attributes)
  assign_attributes(attributes)
  save
end

#update!(attributes) ⇒ Object Also known as: update_attributes!

Updates its receiver just like update but calls save! instead of save, so an exception is raised if the resource is invalid.



225
226
227
228
229
230
231
232
# File 'lib/naranya_ecm/rest/persistence.rb', line 225

def update!(attributes)
  # The following transaction covers any possible server side-effects of the
  # attributes assignment. For example, setting the IDs of a child collection.
  with_transaction_returning_status do
    assign_attributes(attributes)
    save!
  end
end

#update_attribute(name, value) ⇒ Object

Updates a single attribute and saves the resource. This is especially useful for boolean flags on existing resources. Also note that

  • Validation is skipped.

  • Callbacks are invoked.

  • updated_at/updated_on attribute is updated if that attribute is available.

  • Updates all the attributes that are dirty in this object.

This method raises an Activeresource::ActiveresourceError if the attribute is marked as readonly.

See also update_column.



200
201
202
203
204
205
# File 'lib/naranya_ecm/rest/persistence.rb', line 200

def update_attribute(name, value)
  name = name.to_s
  #verify_readonly_attribute(name)
  send("#{name}=", value)
  save(validate: false)
end