module Immortal::InstanceMethods

Public Class Methods

included(base) click to toggle source
# File lib/immortal.rb, line 80
def self.included(base)
  unless base.table_exists? && base.columns_hash[COLUMN_NAME] && !base.columns_hash[COLUMN_NAME].null
    Kernel.warn(
      "[Immortal] The '#{COLUMN_NAME}' column in #{base} is nullable, " \
      'change the column to not accept NULL values'
    )
  end

  base.class_eval do
    scope(:mortal, -> { where(COLUMN_NAME => false) })
    scope(:immortal, -> { where(COLUMN_NAME => true) })

    default_scope { -> { mortal } } if arel_table[COLUMN_NAME]

    alias_method :mortal_destroy, :destroy
    alias_method :destroy, :immortal_destroy
  end
end

Public Instance Methods

destroy!() click to toggle source
# File lib/immortal.rb, line 107
def destroy!
  mortal_destroy
end
destroy_without_callbacks() click to toggle source
# File lib/immortal.rb, line 111
def destroy_without_callbacks
  scoped_record.update_all(
    COLUMN_NAME => true,
    updated_at: current_time_from_proper_timezone
  )

  @destroyed = true
  reload
  freeze
end
immortal_destroy() click to toggle source
# File lib/immortal.rb, line 99
def immortal_destroy
  with_transaction_returning_status do
    run_callbacks :destroy do
      destroy_without_callbacks
    end
  end
end
recover!() click to toggle source
# File lib/immortal.rb, line 122
def recover!
  scoped_record.update_all(
    COLUMN_NAME => false,
    updated_at: current_time_from_proper_timezone
  )

  @destroyed = false
  reload
end

Private Instance Methods

current_time_from_proper_timezone() click to toggle source
# File lib/immortal.rb, line 139
def current_time_from_proper_timezone
  ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.current
end
scoped_record() click to toggle source

@return [ActiveRecord::Relation]

# File lib/immortal.rb, line 135
def scoped_record
  self.class.unscoped.where(id: id)
end