Module: ActiveModelPersistence::Persistence::ClassMethods

Defined in:
lib/active_model_persistence/persistence.rb

Overview

When this module is included in another class, ActiveSupport::Concern will make these class methods on that class.

Instance Method Summary collapse

Instance Method Details

#allArray<Object>

Return all model objects that have been saved to the object store

Examples:

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2
ModelExample.all.map(&:id) #=> [1, 2]
ModelExample.all.map(&:name) #=> ['James', 'Frank']

Returns:

  • (Array<Object>)

    the model objects in the object store



160
161
162
# File 'lib/active_model_persistence/persistence.rb', line 160

def all
  object_array.each
end

#countInteger Also known as: size

The number of model objects saved in the object store

Examples:

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2

Returns:

  • (Integer)

    the number of model objects in the object store



176
177
178
# File 'lib/active_model_persistence/persistence.rb', line 176

def count
  object_array.size
end

#create(attributes = nil, &block) ⇒ Object+

Creates a new model object in to the object store and returns it

Create a new model object passing ‘attributes` and `block` to `.new` and then calls `#save`.

The new model object is returned even if it could not be saved to the object store.

Examples:

m = ModelExample.new(id: 1, name: 'James')
m.id #=> 1
m.name #=> 'James'

Multiple model objects can be created

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
objects = ModelExample.create(array_of_attributes)
objects.class #=> Array
objects.size #=> 2
objects.first.id #=> 1
objects.map(&:name) #=> ['James', 'Frank']

Parameters:

  • attributes (Hash, Array<Hash>) (defaults to: nil)

    attributes

    The attributes to set on the model object. These are passed to the model’s ‘.new` method.

    Multiple model objects can be created by passing an array of attribute Hashes.

  • block (Proc)

    options

    The block to pass to the model’s ‘.new` method.

Returns:

  • (Object, Array<Object>)

    the model object or array of model objects created



94
95
96
97
98
99
100
# File 'lib/active_model_persistence/persistence.rb', line 94

def create(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr, &block) }
  else
    new(attributes, &block).tap(&:save)
  end
end

#create!(attributes = nil, &block) ⇒ Object+

Creates a new model object in to the object store

Raises an error if the object could not be created.

Create a new model object passing ‘attributes` and `block` to `.new` and then calls `#save!`.

Examples:

m = ModelExample.new(id: 1, name: 'James')
m.id #=> 1
m.name #=> 'James'

Multiple model objects can be created

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
objects = ModelExample.create(array_of_attributes)
objects.class #=> Array
objects.size #=> 2
objects.first.id #=> 1
objects.map(&:name) #=> ['James', 'Frank']

Parameters:

  • attributes (Hash, Array<Hash>) (defaults to: nil)

    attributes

    The attributes to set on the model object. These are passed to the model’s ‘.new` method.

    Multiple model objects can be created by passing an array of attribute Hashes.

  • block (Proc)

    options

    The block to pass to the model’s ‘.new` method.

Returns:

  • (Object, Array<Object>)

    the model object or array of model objects created

Raises:

  • (ModelError)

    if the model object could not be created



138
139
140
141
142
143
144
# File 'lib/active_model_persistence/persistence.rb', line 138

def create!(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create!(attr, &block) }
  else
    new(attributes, &block).tap(&:save!)
  end
end

#delete_allvoid

This method returns an undefined value.

Removes all model objects from the object store

Each saved model object’s ‘#destroy` method is NOT called.

Examples:

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2
ModelExample.destroy_all
ModelExample.all.count #=> 0


218
219
220
221
222
# File 'lib/active_model_persistence/persistence.rb', line 218

def delete_all
  @object_array = []
  indexes.values.each(&:remove_all)
  nil
end

#destroy_allvoid

This method returns an undefined value.

Removes all model objects from the object store

Each saved model object’s ‘#destroy` method is called.

Examples:

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2
ModelExample.destroy_all
ModelExample.all.count #=> 0


198
199
200
# File 'lib/active_model_persistence/persistence.rb', line 198

def destroy_all
  object_array.first.destroy while object_array.size.positive?
end

#object_arrayArray<Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All saved model objects are stored in this array (this is the object store)

Returns:

  • (Array<Object>)

    the model objects in the object store



232
233
234
# File 'lib/active_model_persistence/persistence.rb', line 232

def object_array
  @object_array ||= []
end