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



3
4
5
# File 'lib/acts_as_paranoid/core.rb', line 3

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

Instance Method Details

#deleteObject

Straight from ActiveRecord 5.1!



109
110
111
112
113
# File 'lib/acts_as_paranoid/core.rb', line 109

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

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


233
234
235
236
237
238
239
240
241
# File 'lib/acts_as_paranoid/core.rb', line 233

def deleted?
  @destroyed || !if self.class.string_type_with_deleted_value?
    paranoid_value != self.class.delete_now_value || paranoid_value.nil?
  elsif self.class.boolean_type_not_nullable?
    paranoid_value == false
  else
    paranoid_value.nil?
  end
end

#deleted_fully?Boolean Also known as: destroyed_fully?

Returns:

  • (Boolean)


245
246
247
# File 'lib/acts_as_paranoid/core.rb', line 245

def deleted_fully?
  @destroyed
end

#destroy!Object Also known as: destroy



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/acts_as_paranoid/core.rb', line 134

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(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])

          decrement_counters_on_associations
        end

        @_trigger_destroy_callback = true

        stale_paranoid_value
        self
      end
    end
  else
    if paranoid_configuration[:double_tap_destroys_fully]
      destroy_fully!
    end
  end
end

#destroy_dependent_associations!Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/acts_as_paranoid/core.rb', line 214

def destroy_dependent_associations!
  self.class.dependent_associations.each do |reflection|
    next unless (klass = get_reflection_class(reflection)).paranoid?

    scope = klass.only_deleted

    # Merge in the association's scope
    scope = if ActiveRecord::VERSION::MAJOR >= 6
      scope.merge(ActiveRecord::Associations::AssociationScope.scope(association(reflection.name)))
    else
      scope.merge(association(reflection.name).association_scope)
    end

    scope.each do |object|
      object.destroy!
    end
  end
end

#destroy_fully!Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/acts_as_paranoid/core.rb', line 115

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!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])

        decrement_counters_on_associations
      end

      stale_paranoid_value
      @destroyed = true
      freeze
    end
  end
end

#paranoid_valueObject



104
105
106
# File 'lib/acts_as_paranoid/core.rb', line 104

def paranoid_value
  self.send(self.class.paranoid_column)
end

#persisted?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/acts_as_paranoid/core.rb', line 100

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

#recover(options = {}) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/acts_as_paranoid/core.rb', line 161

def recover(options={})
  return if !self.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
      recover_dependent_associations(options[:recovery_window], options) if options[:recursive]
      increment_counters_on_associations
      self.paranoid_value = self.class.paranoid_configuration[:recovery_value]
      if options[:raise_error]
        self.save!
      else
        self.save
      end
    end
  end
end

#recover!(options = {}) ⇒ Object



183
184
185
186
187
# File 'lib/acts_as_paranoid/core.rb', line 183

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

  recover(options)
end

#recover_dependent_associations(window, options) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/acts_as_paranoid/core.rb', line 189

def recover_dependent_associations(window, options)
  self.class.dependent_associations.each do |reflection|
    next unless (klass = get_reflection_class(reflection)).paranoid?

    scope = klass.only_deleted

    # Merge in the association's scope
    scope = if ActiveRecord::VERSION::MAJOR >= 6
      scope.merge(ActiveRecord::Associations::AssociationScope.scope(association(reflection.name)))
    else
      scope.merge(association(reflection.name).association_scope)
    end

    # We can only recover by window if both parent and dependant have a
    # paranoid column type of :time.
    if self.class.paranoid_column_type == :time && klass.paranoid_column_type == :time
      scope = scope.deleted_inside_time_window(paranoid_value, window)
    end

    scope.each do |object|
      object.recover(options)
    end
  end
end