Module: Immortal::InstanceMethods

Defined in:
lib/immortal.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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

Instance Method Details

#destroy!Object



107
108
109
# File 'lib/immortal.rb', line 107

def destroy!
  mortal_destroy
end

#destroy_without_callbacksObject



111
112
113
114
115
116
117
118
119
120
# 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_destroyObject



99
100
101
102
103
104
105
# 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!Object



122
123
124
125
126
127
128
129
130
# 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