Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Persistence.empty? #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/active_model_persistence/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ def count

alias size count

# Returns true if there are no model objects saved in the object store
#
# @example
# array_of_attributes = [
# ]
# ModelExample.create(array_of_attributes)
# ModelExample.size #=> 0
# ModelExample.empty? #=> true
#
# @return [Integer] the number of model objects in the object store
#
def empty?
object_array.size.zero?
end

# Removes all model objects from the object store
#
# Each saved model object's `#destroy` method is called.
Expand Down
2 changes: 2 additions & 0 deletions spec/active_model_persistence/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def self.name

it 'should have three object in the object store' do
expect(model_class.size).to eq(3)
expect(model_class.empty?).to eq(false)
end

context 'after calling .destroy_all' do
Expand All @@ -396,6 +397,7 @@ def self.name

it 'should remove all objects from the obejct store' do
expect(model_class.size).to be_zero
expect(model_class.empty?).to eq(true)
end
end
end
Expand Down