Skip to content

Instantly share code, notes, and snippets.

@kwent
Created December 30, 2018 23:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwent/b5e1494dc24afa5fa01f2d8c8d473ea3 to your computer and use it in GitHub Desktop.
Save kwent/b5e1494dc24afa5fa01f2d8c8d473ea3 to your computer and use it in GitHub Desktop.
Cancelable Rails concern
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