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!



142
143
144
145
146
# File 'lib/acts_as_paranoid/core.rb', line 142

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

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


227
228
229
230
231
232
233
234
235
236
237
# File 'lib/acts_as_paranoid/core.rb', line 227

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)


241
242
243
# File 'lib/acts_as_paranoid/core.rb', line 241

def deleted_fully?
  @destroyed
end

#destroyObject



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

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



167
168
169
170
171
# File 'lib/acts_as_paranoid/core.rb', line 167

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

#destroy_fully!Object



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

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



137
138
139
# File 'lib/acts_as_paranoid/core.rb', line 137

def paranoid_value
  send(self.class.paranoid_column)
end

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#recover(options = {}) ⇒ Object



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

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



221
222
223
224
225
# File 'lib/acts_as_paranoid/core.rb', line 221

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

  recover(options)
end