Created
December 30, 2018 23:17
-
-
Save kwent/b5e1494dc24afa5fa01f2d8c8d473ea3 to your computer and use it in GitHub Desktop.
Cancelable Rails concern
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 Cancelable | |
extend ActiveSupport::Concern | |
included do | |
scope :with_cancelled, -> { where.not(cancelled_at: nil) } | |
scope :not_cancelled, -> { where(cancelled_at: nil) } | |
def cancel | |
# Use update_attributes and not touch to trigger after_save | |
self.update_attributes(cancelled_at: DateTime.now.in_time_zone) | |
end | |
def cancel! | |
# Use update_attributes and not touch to trigger after_save | |
self.update_attributes!(cancelled_at: DateTime.now.in_time_zone) | |
end | |
def cancelled? | |
self.cancelled_at? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment