Skip to content

Commit

Permalink
188: setup act as paranoid to all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
edimossilva committed May 26, 2024
1 parent 04b1cad commit 005cf8e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ gem "paper_trail"
# A Rails engine that helps you put together a super-flexible admin dashboard.
gem "administrate"

# ActiveRecord plugin allowing you to hide and restore records without actually deleting them.
gem "acts_as_paranoid"

group :development, :test do
gem "bullet"

Expand Down
4 changes: 4 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ GEM
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
acts_as_paranoid (0.10.0)
activerecord (>= 6.1, < 7.2)
activesupport (>= 6.1, < 7.2)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
administrate (0.20.1)
Expand Down Expand Up @@ -568,6 +571,7 @@ PLATFORMS

DEPENDENCIES
active_model_serializers
acts_as_paranoid
administrate
bootsnap
brakeman
Expand Down
1 change: 1 addition & 0 deletions backend/app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
has_paper_trail
acts_as_paranoid
end
8 changes: 8 additions & 0 deletions backend/db/migrate/20240526124316_add_deleted_at_to_users.rb
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
4 changes: 3 additions & 1 deletion backend/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions backend/spec/models/acts_as_paranoid_factories_spec.rb
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
31 changes: 31 additions & 0 deletions backend/spec/support/acts_as_paranoid_examples.rb
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

0 comments on commit 005cf8e

Please sign in to comment.