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
-
#all ⇒ Array<Object>
Return all model objects that have been saved to the object store.
-
#count ⇒ Integer
(also: #size)
The number of model objects saved in the object store.
-
#create(attributes = nil, &block) ⇒ Object+
Creates a new model object in to the object store and returns it.
-
#create!(attributes = nil, &block) ⇒ Object+
Creates a new model object in to the object store.
-
#destroy_all ⇒ void
(also: #delete_all)
Removes all model objects from the object store.
Instance Method Details
#all ⇒ Array<Object>
Return all model objects that have been saved to the object store
160 161 162 |
# File 'lib/active_model_persistence/persistence.rb', line 160 def all object_array.select { |object| object.is_a?(self) }.each end |
#count ⇒ Integer Also known as: size
The number of model objects saved in the object store
176 177 178 |
# File 'lib/active_model_persistence/persistence.rb', line 176 def count object_array.select { |object| object.is_a?(self) }.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.
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!`.
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 |
#destroy_all ⇒ void Also known as: delete_all
This method returns an undefined value.
Removes all model objects from the object store
Each saved model object's `#destroy` method is called.
198 199 200 201 |
# File 'lib/active_model_persistence/persistence.rb', line 198 def destroy_all objects_to_destroy = object_array.select { |object| object.is_a?(self) } objects_to_destroy.each(&:destroy) end |