Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/paranoia.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.acts_as_paranoid(options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/paranoia.rb', line 184

def self.acts_as_paranoid(options={})
  alias :really_destroyed? :destroyed?
  alias :really_delete :delete

  alias :destroy_without_paranoia :destroy
  def really_destroy!
    transaction do
      run_callbacks(:real_destroy) do
        dependent_reflections = self.class.reflections.select do |name, reflection|
          reflection.options[:dependent] == :destroy
        end
        if dependent_reflections.any?
          dependent_reflections.each do |name, reflection|
            association_data = self.send(name)
            # has_one association can return nil
            # .paranoid? will work for both instances and classes
            next unless association_data && association_data.paranoid?
            if reflection.collection?
              next association_data.with_deleted.each(&:really_destroy!)
            end
            association_data.really_destroy!
          end
        end
        write_attribute(paranoia_column, current_time_from_proper_timezone)
        destroy_without_paranoia
      end
    end
  end

  include Paranoia
  class_attribute :paranoia_column, :paranoia_sentinel_value

  self.paranoia_column = (options[:column] || :deleted_at).to_s
  self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value }
  def self.paranoia_scope
    where(paranoia_column => paranoia_sentinel_value)
  end
  default_scope { paranoia_scope }

  before_restore {
    self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers)
  }
  after_restore {
    self.class.notify_observers(:after_restore, self) if self.class.respond_to?(:notify_observers)
  }
end

.I_AM_THE_DESTROYER!Object

Please do not use this method in production. Pretty please.



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

def self.I_AM_THE_DESTROYER!
  # TODO: actually implement spelling error fixes
  puts %Q{
    Sharon: "There should be a method called I_AM_THE_DESTROYER!"
    Ryan:   "What should this method do?"
    Sharon: "It should fix all the spelling errors on the page!"
}
end

.paranoia_scopeObject



218
219
220
# File 'lib/paranoia.rb', line 218

def self.paranoia_scope
  where(paranoia_column => paranoia_sentinel_value)
end

.paranoid?Boolean

Returns:

  • (Boolean)


242
# File 'lib/paranoia.rb', line 242

def self.paranoid? ; false ; end

Instance Method Details

#paranoid?Boolean

Returns:

  • (Boolean)


243
# File 'lib/paranoia.rb', line 243

def paranoid? ; self.class.paranoid? ; end

#really_destroy!Object



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

def really_destroy!
  transaction do
    run_callbacks(:real_destroy) do
      dependent_reflections = self.class.reflections.select do |name, reflection|
        reflection.options[:dependent] == :destroy
      end
      if dependent_reflections.any?
        dependent_reflections.each do |name, reflection|
          association_data = self.send(name)
          # has_one association can return nil
          # .paranoid? will work for both instances and classes
          next unless association_data && association_data.paranoid?
          if reflection.collection?
            next association_data.with_deleted.each(&:really_destroy!)
          end
          association_data.really_destroy!
        end
      end
      write_attribute(paranoia_column, current_time_from_proper_timezone)
      destroy_without_paranoia
    end
  end
end