Module: Immortal::InstanceMethods

Defined in:
lib/immortal.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/immortal.rb', line 100

def self.included(base)
  unless base.table_exists? && base.columns_hash["deleted"] && !base.columns_hash["deleted"].null
    Kernel.warn "[Immortal] The 'deleted' column in #{base.to_s} is nullable, change the column to not accept NULL values"
  end

  base.class_eval do
    default_scope { ->{ where(deleted: false) } } if arel_table[:deleted]

    alias :mortal_destroy :destroy
    alias :destroy :immortal_destroy
  end
end

Instance Method Details

#destroy!Object



121
122
123
# File 'lib/immortal.rb', line 121

def destroy!
  mortal_destroy
end

#destroy_without_callbacksObject



125
126
127
128
129
130
# File 'lib/immortal.rb', line 125

def destroy_without_callbacks
  self.class.unscoped.where(id: id).update_all(deleted: true, updated_at: current_time_from_proper_timezone)
  @destroyed = true
  reload
  freeze
end

#immortal_destroyObject



113
114
115
116
117
118
119
# File 'lib/immortal.rb', line 113

def immortal_destroy
  with_transaction_returning_status do
    run_callbacks :destroy do
      destroy_without_callbacks
    end
  end
end

#recover!Object



132
133
134
135
136
# File 'lib/immortal.rb', line 132

def recover!
  self.class.unscoped.where(id: id).update_all(deleted: false, updated_at: current_time_from_proper_timezone)
  @destroyed = false
  reload
end