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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/paranoia.rb', line 156

def self.acts_as_paranoid(options={})
  alias :destroy! :destroy
  alias :delete! :delete
  def really_destroy!
    dependent_reflections = self.class.reflections.select do |name, reflection|
      reflection.options[:dependent] == :destroy
    end
    if dependent_reflections.any?
      dependent_reflections.each do |name, _|
        associated_records = self.send(name)
        # has_one association can return nil
        if associated_records && associated_records.respond_to?(:with_deleted)
          # Paranoid models will have this method, non-paranoid models will not
          associated_records.with_deleted.each(&:really_destroy!)
          self.send(name).reload
        elsif associated_records && !associated_records.respond_to?(:each) # single record
          associated_records.really_destroy!
        end
      end
    end
    touch_paranoia_column if ActiveRecord::VERSION::STRING >= "4.1"
    destroy!
  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(table_name => { 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.



200
201
202
203
204
205
206
207
# File 'lib/paranoia.rb', line 200

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



185
186
187
# File 'lib/paranoia.rb', line 185

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

.paranoid?Boolean

Returns:

  • (Boolean)


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

def self.paranoid? ; false ; end

Instance Method Details

#paranoid?Boolean

Returns:

  • (Boolean)


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

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

#persisted?Boolean

Override the persisted method to allow for the paranoia gem. If a paranoid record is selected, then we only want to check if it’s a new record, not if it is “destroyed”.

Returns:

  • (Boolean)


215
216
217
# File 'lib/paranoia.rb', line 215

def persisted?
  paranoid? ? !new_record? : super
end

#really_destroy!Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/paranoia.rb', line 159

def really_destroy!
  dependent_reflections = self.class.reflections.select do |name, reflection|
    reflection.options[:dependent] == :destroy
  end
  if dependent_reflections.any?
    dependent_reflections.each do |name, _|
      associated_records = self.send(name)
      # has_one association can return nil
      if associated_records && associated_records.respond_to?(:with_deleted)
        # Paranoid models will have this method, non-paranoid models will not
        associated_records.with_deleted.each(&:really_destroy!)
        self.send(name).reload
      elsif associated_records && !associated_records.respond_to?(:each) # single record
        associated_records.really_destroy!
      end
    end
  end
  touch_paranoia_column if ActiveRecord::VERSION::STRING >= "4.1"
  destroy!
end