Module: ActsAsParanoid::Core

Defined in:
lib/acts_as_paranoid/core.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/acts_as_paranoid/core.rb', line 5

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#deleteObject

Straight from ActiveRecord 5.1!



139
140
141
142
143
# File 'lib/acts_as_paranoid/core.rb', line 139

def delete
  self.class.delete(id) if persisted?
  stale_paranoid_value
  freeze
end

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
234
# File 'lib/acts_as_paranoid/core.rb', line 224

def deleted?
  return true if @destroyed

  if self.class.string_type_with_deleted_value?
    paranoid_value == paranoid_configuration[:deleted_value]
  elsif self.class.boolean_type_not_nullable?
    paranoid_value == true
  else
    !paranoid_value.nil?
  end
end

#deleted_fully?Boolean Also known as: destroyed_fully?

Returns:

  • (Boolean)


238
239
240
# File 'lib/acts_as_paranoid/core.rb', line 238

def deleted_fully?
  @destroyed
end

#destroyObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/acts_as_paranoid/core.rb', line 170

def destroy
  if !deleted?
    with_transaction_returning_status do
      run_callbacks :destroy do
        if persisted?
          # Handle composite keys, otherwise we would just use
          # `self.class.primary_key.to_sym => self.id`.
          self.class
            .delete_all([Array(self.class.primary_key), Array(id)].transpose.to_h)
          decrement_counters_on_associations
        end

        @_trigger_destroy_callback = true

        stale_paranoid_value
        self
      end
    end
  elsif paranoid_configuration[:double_tap_destroys_fully]
    destroy_fully!
  end
end

#destroy!Object



164
165
166
167
168
# File 'lib/acts_as_paranoid/core.rb', line 164

def destroy!
  destroy || raise(
    ActiveRecord::RecordNotDestroyed.new("Failed to destroy the record", self)
  )
end

#destroy_fully!Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/acts_as_paranoid/core.rb', line 145

def destroy_fully!
  with_transaction_returning_status do
    run_callbacks :destroy do
      destroy_dependent_associations!

      if persisted?
        # Handle composite keys, otherwise we would just use
        # `self.class.primary_key.to_sym => self.id`.
        self.class
          .delete_all!([Array(self.class.primary_key), Array(id)].transpose.to_h)
        decrement_counters_on_associations
      end

      @destroyed = true
      freeze
    end
  end
end

#paranoid_valueObject



134
135
136
# File 'lib/acts_as_paranoid/core.rb', line 134

def paranoid_value
  send(self.class.paranoid_column)
end

#persisted?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/acts_as_paranoid/core.rb', line 130

def persisted?
  !(new_record? || @destroyed)
end

#recover(options = {}) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/acts_as_paranoid/core.rb', line 193

def recover(options = {})
  return if !deleted?

  options = {
    recursive: self.class.paranoid_configuration[:recover_dependent_associations],
    recovery_window: self.class.paranoid_configuration[:dependent_recovery_window],
    raise_error: false
  }.merge(options)

  self.class.transaction do
    run_callbacks :recover do
      increment_counters_on_associations
      deleted_value = paranoid_value
      self.paranoid_value = self.class.recovery_value
      result = if options[:raise_error]
                 save!
               else
                 save
               end
      recover_dependent_associations(deleted_value, options) if options[:recursive]
      result
    end
  end
end

#recover!(options = {}) ⇒ Object



218
219
220
221
222
# File 'lib/acts_as_paranoid/core.rb', line 218

def recover!(options = {})
  options[:raise_error] = true

  recover(options)
end