Last active
August 14, 2016 17:11
-
-
Save theareba/b87d1b5d7bbd8ea52dafaa7cf82a1b00 to your computer and use it in GitHub Desktop.
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
class Comment < ApplicationRecord | |
include Trashable | |
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
class Post < ApplicationRecord | |
include Trashable | |
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
Comment.first(5).map(&:trash!) | |
Post.first(5).map(&:trash!) |
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
module Trashable | |
extend ActiveSupport::Concern | |
included do | |
default_scope -> { where(trashed_at: nil) } | |
scope :trashed, -> { unscoped.where("#{self.table_name}.trashed_at is not null") } | |
end | |
def trash! | |
update_attribute :trashed_at, Time.now | |
end | |
def trashed? | |
trashed_at.present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment