-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
188: setup act as paranoid to all classes (#192)
- Loading branch information
1 parent
04b1cad
commit bb774ed
Showing
7 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
class ApplicationRecord < ActiveRecord::Base | ||
primary_abstract_class | ||
has_paper_trail | ||
acts_as_paranoid | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddDeletedAtToUsers < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :users, :deleted_at, :datetime | ||
add_index :users, :deleted_at | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
# spec/models/acts_as_paranoid_factories_spec.rb | ||
require "rails_helper" | ||
|
||
RSpec.describe "ApplicationRecord" do | ||
FactoryBot.factories.each do |factory| | ||
model_class = factory.build_class | ||
|
||
it_behaves_like "acts_as_paranoid", model_class | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
# spec/support/acts_as_paranoid_examples.rb | ||
RSpec.shared_examples "acts_as_paranoid" do |model_class| | ||
let!(:record) { create(model_class.name.underscore.to_sym) } | ||
|
||
describe "acts_as_paranoid" do | ||
it "soft deletes the record" do | ||
record.destroy | ||
expect(record.reload).to be_deleted | ||
expect(model_class.with_deleted.find(record.id)).to eq(record) | ||
end | ||
|
||
it "does not include soft deleted records in default scope" do | ||
record.destroy | ||
expect(model_class.all).not_to include(record) | ||
end | ||
|
||
it "includes soft deleted records with with_deleted scope" do | ||
record.destroy | ||
expect(model_class.with_deleted).to include(record) | ||
end | ||
|
||
it "restores a soft deleted record" do | ||
record.destroy | ||
record.recover | ||
expect(model_class.all).to include(record) | ||
expect(record.reload.deleted_at).to be_nil | ||
end | ||
end | ||
end |