Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions core/app/models/concerns/spree/hard_deletable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module Spree
# Implements all of the methods that Discard implements, but without a deleted_at column.
# Deprecates all usages of Discard methods.
module HardDeletable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍 worth a DeadCode episode!

extend ActiveSupport::Concern

def discarded? = false
def undiscarded? = true
def kept? = true
def deleted_at = nil

included do
def self.kept
all
end

def self.discarded
none
end

def self.discard_all
destroy_all
end

def self.with_discarded
all
end
class << self
deprecate :kept, :discarded, :with_discarded, :discard_all, deprecator: Spree.deprecator
end

alias_method :discard, :destroy
alias_method :discard!, :destroy

deprecate :discard, :discard!, deprecator: Spree.deprecator
deprecate :discarded?, :undiscarded?, :kept?, :deleted_at, deprecator: Spree.deprecator
end
end
end
80 changes: 80 additions & 0 deletions core/spec/models/spree/hard_deletable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Spree::HardDeletable do
let(:hard_deletable_migration) do
Class.new(ActiveRecord::Migration[5.1]) do
def change
create_table(:hard_deletable_items)
end
end
end

let(:hard_deletable_item_class) do
Class.new(Spree::Base) do
include Spree::HardDeletable

def self.name
"HardDeletableItem"
end
end
end

let(:hard_deletable_item) { hard_deletable_item_class.new }

around do |example|
hard_deletable_migration.migrate(:up)
example.run
hard_deletable_migration.migrate(:down)
end

before do
expect(Spree.deprecator).to receive(:warn)
end

describe ".with_discarded" do
it "is deprecated and returns all" do
expect(hard_deletable_item_class.with_discarded.to_sql).to eq(hard_deletable_item_class.all.to_sql)
end
end

describe ".kept" do
it "is deprecated and returns all" do
expect(hard_deletable_item_class.kept.to_sql).to eq(hard_deletable_item_class.all.to_sql)
end
end

describe ".discarded" do
it "is deprecated and returns none" do
expect(hard_deletable_item_class.discarded.to_sql).to eq(hard_deletable_item_class.none.to_sql)
end
end

describe ".discard_all" do
it "is deprecated and calls #destroy_all" do
expect(hard_deletable_item_class).to receive(:destroy_all)
hard_deletable_item_class.discard_all
end
end

describe "#deleted_at" do
subject { hard_deletable_item.deleted_at }
it { is_expected.to be nil }
end

describe "#discarded?" do
subject { hard_deletable_item.discarded? }
it { is_expected.to be false }
end

describe "#undiscarded?" do
subject { hard_deletable_item.undiscarded? }
it { is_expected.to be true }
end

describe "#kept?" do
subject { hard_deletable_item.kept? }
it { is_expected.to be true }
end
end