Module: Paranoia::Query

Defined in:
lib/paranoia.rb

Instance Method Summary collapse

Instance Method Details

#only_deletedObject Also known as: deleted

If you want to find only the deleted records



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/paranoia.rb', line 32

def only_deleted
  if paranoia_sentinel_value.nil?
    return with_deleted.where.not(paranoia_column => paranoia_sentinel_value)
  end
  # if paranoia_sentinel_value is not null, then it is possible that
  # some deleted rows will hold a null value in the paranoia column
  # these will not match != sentinel value because "NULL != value" is
  # NULL under the sql standard
  # Scoping with the table_name is mandatory to avoid ambiguous errors when joining tables.
  scoped_quoted_paranoia_column = "#{connection.quote_table_name(self.table_name)}.#{connection.quote_column_name(paranoia_column)}"
  with_deleted.where("#{scoped_quoted_paranoia_column} IS NULL OR #{scoped_quoted_paranoia_column} != ?", paranoia_sentinel_value)
end

#paranoia_destroy_attributesObject



58
59
60
61
62
# File 'lib/paranoia.rb', line 58

def paranoia_destroy_attributes
  {
    paranoia_column => current_time_from_proper_timezone
  }.merge(timestamp_attributes_with_current_time)
end

#paranoid?Boolean

Returns:

  • (Boolean)


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

def paranoid? ; true ; end

#restore(id_or_ids, opts = {}) ⇒ Object

If you want to restore a record



47
48
49
50
51
52
53
54
55
56
# File 'lib/paranoia.rb', line 47

def restore(id_or_ids, opts = {})
  ids = Array(id_or_ids).flatten
  any_object_instead_of_id = ids.any? { |id| ActiveRecord::Base === id }
  if any_object_instead_of_id
    ids.map! { |id| ActiveRecord::Base === id ? id.id : id }
    ActiveSupport::Deprecation.warn("You are passing an instance of ActiveRecord::Base to `restore`. " \
                                    "Please pass the id of the object by calling `.id`")
  end
  ids.map { |id| only_deleted.find(id).restore!(opts) }
end

#timestamp_attributes_with_current_timeObject



64
65
66
# File 'lib/paranoia.rb', line 64

def timestamp_attributes_with_current_time
  timestamp_attributes_for_update_in_model.each_with_object({}) { |attr,hash| hash[attr] = current_time_from_proper_timezone }
end

#with_deletedObject

If you want to find all records, even those which are deleted



24
25
26
27
28
29
# File 'lib/paranoia.rb', line 24

def with_deleted
  if ActiveRecord::VERSION::STRING >= "4.1"
    return unscope where: paranoia_column
  end
  all.tap { |x| x.default_scoped = false }
end