lib/purgatory/purgatory.rb in purgatory-2.3.0 vs lib/purgatory/purgatory.rb in purgatory-2.4.0
- old
+ new
@@ -3,10 +3,11 @@
class Purgatory < ActiveRecord::Base
belongs_to :soul, polymorphic: true, autosave: false
belongs_to :requester, class_name: PurgatoryModule.configuration.user_class_name
belongs_to :approver, class_name: PurgatoryModule.configuration.user_class_name
before_create :store_changes
+ before_create :destroy_pending_with_same_soul
validates :soul_type, presence: true
serialize :requested_changes
@@ -40,11 +41,19 @@
return true
end
false
end
+ def self.pending_with_matching_soul(soul)
+ pending.where("soul_id IS NOT NULL AND soul_id = ? AND soul_type = ?", soul.id, soul.class.name)
+ end
+
private
def store_changes
self.requested_changes = soul.changes
+ end
+
+ def destroy_pending_with_same_soul
+ Purgatory.pending_with_matching_soul(soul).destroy_all
end
end