Skip to content

Instantly share code, notes, and snippets.

@goromlagche
Created October 3, 2017 08:26
Show Gist options
  • Select an option

  • Save goromlagche/455a3c63a4149eb53c726284491ee0da to your computer and use it in GitHub Desktop.

Select an option

Save goromlagche/455a3c63a4149eb53c726284491ee0da to your computer and use it in GitHub Desktop.
module Archivable
extend ActiveSupport::Concern
included do
default_scope -> { where(is_archived: false) }
scope :with_archived, -> { all.unscope(where: :is_archived) }
scope :only_archived, -> { with_archived.where(is_archived: true) }
end
class_methods do
def archive_all
# rubocop:disable Rails/SkipsModelValidations
update_all(archived_at: Time.current, is_archived: true)
# rubocop:enable Rails/SkipsModelValidations
end
def destroy_all(conditions = nil)
Rails.logger.fatal('FE: destroy_all called while archive is supported')
super
end
def destroy(id_or_array)
Rails.logger.fatal('FE: destroy called while archive is supported')
super
end
def delete_all(conditions = nil)
Rails.logger.fatal('FE: delete_all called while archive is supported')
super
end
def delete(id_or_array)
Rails.logger.fatal('FE: delete called while archive is supported')
super
end
end
def archive
update_attributes(archived_at: Time.current, is_archived: true)
end
def archive!
update_attributes!(archived_at: Time.current, is_archived: true)
end
def delete
Rails.logger.fatal('FE: delete called while archive is supported')
super
end
def destroy
Rails.logger.fatal('FE: destroy called while archive is supported')
super
end
def destroy!
Rails.logger.fatal('FE: destroy! called while archive is supported')
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment