better comments

This commit is contained in:
Sam 2013-05-07 14:46:46 +10:00
parent e9fc272db7
commit 066c2bed42

View File

@ -3,28 +3,36 @@ module Trashable
included do included do
default_scope where(with_deleted_scope_sql) default_scope where(with_deleted_scope_sql)
# scope unscoped does not work
end end
module ClassMethods module ClassMethods
def with_deleted def with_deleted
# lifted from acts_as_paranoid, works around http://stackoverflow.com/questions/8734669/rails-3-1-3-unscoped-scope # lifted from acts_as_paranoid, works around https://github.com/rails/rails/issues/4306
#
# with this in place Post.limit(10).with_deleted, will work as expected
#
scope = self.scoped.with_default_scope scope = self.scoped.with_default_scope
scope.where_values.delete(with_deleted_scope_sql) scope.where_values.delete(with_deleted_scope_sql)
scope scope
end end
def with_deleted_scope_sql def with_deleted_scope_sql
self.scoped.table[:deleted_at].eq(nil).to_sql scoped.table[:deleted_at].eq(nil).to_sql
end end
end end
def trash! def trash!
self.update_column(:deleted_at, DateTime.now) update_column(:deleted_at, DateTime.now)
end end
def recover! def recover!
# see: https://github.com/rails/rails/issues/8436 # see: https://github.com/rails/rails/issues/8436
#
# Fixed in Rails 4
#
self.class.unscoped.update_all({deleted_at: nil}, id: self.id) self.class.unscoped.update_all({deleted_at: nil}, id: self.id)
raw_write_attribute :deleted_at, nil raw_write_attribute :deleted_at, nil
end end