Module: ActsAsParanoid::Core::ClassMethods

Defined in:
lib/acts_as_paranoid/core.rb

Constant Summary collapse

DESTROYING_ASSOCIATION_DEPENDENCY_TYPES =
[:destroy, :delete_all].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



10
11
12
# File 'lib/acts_as_paranoid/core.rb', line 10

def self.extended(base)
  base.define_callbacks :recover
end

Instance Method Details

#after_recover(method) ⇒ Object



18
19
20
# File 'lib/acts_as_paranoid/core.rb', line 18

def after_recover(method)
  set_callback :recover, :after, method
end

#before_recover(method) ⇒ Object



14
15
16
# File 'lib/acts_as_paranoid/core.rb', line 14

def before_recover(method)
  set_callback :recover, :before, method
end

#boolean_type_not_nullable?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/acts_as_paranoid/core.rb', line 61

def boolean_type_not_nullable?
  paranoid_column_type == :boolean && !paranoid_configuration[:allow_nulls]
end

#delete_all(conditions = nil) ⇒ Object



41
42
43
44
# File 'lib/acts_as_paranoid/core.rb', line 41

def delete_all(conditions = nil)
  where(conditions)
    .update_all(["#{paranoid_configuration[:column]} = ?", delete_now_value])
end

#delete_all!(conditions = nil) ⇒ Object



37
38
39
# File 'lib/acts_as_paranoid/core.rb', line 37

def delete_all!(conditions = nil)
  without_paranoid_default_scope.delete_all!(conditions)
end

#delete_now_valueObject



85
86
87
88
89
90
91
# File 'lib/acts_as_paranoid/core.rb', line 85

def delete_now_value
  case paranoid_configuration[:column_type]
  when "time" then Time.now
  when "boolean" then true
  when "string" then paranoid_configuration[:deleted_value]
  end
end

#dependent_associationsObject



79
80
81
82
83
# File 'lib/acts_as_paranoid/core.rb', line 79

def dependent_associations
  reflect_on_all_associations.select do |a|
    DESTROYING_ASSOCIATION_DEPENDENCY_TYPES.include?(a.options[:dependent])
  end
end

#only_deletedObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/acts_as_paranoid/core.rb', line 26

def only_deleted
  if string_type_with_deleted_value?
    without_paranoid_default_scope
      .where(paranoid_column_reference => paranoid_configuration[:deleted_value])
  elsif boolean_type_not_nullable?
    without_paranoid_default_scope.where(paranoid_column_reference => true)
  else
    without_paranoid_default_scope.where.not(paranoid_column_reference => nil)
  end
end

#paranoid_columnObject



65
66
67
# File 'lib/acts_as_paranoid/core.rb', line 65

def paranoid_column
  paranoid_configuration[:column].to_sym
end

#paranoid_column_referenceObject



73
74
75
# File 'lib/acts_as_paranoid/core.rb', line 73

def paranoid_column_reference
  "#{table_name}.#{paranoid_column}"
end

#paranoid_column_typeObject



69
70
71
# File 'lib/acts_as_paranoid/core.rb', line 69

def paranoid_column_type
  paranoid_configuration[:column_type].to_sym
end

#paranoid_default_scopeObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/acts_as_paranoid/core.rb', line 46

def paranoid_default_scope
  if string_type_with_deleted_value?
    all.table[paranoid_column].eq(nil)
      .or(all.table[paranoid_column].not_eq(paranoid_configuration[:deleted_value]))
  elsif boolean_type_not_nullable?
    all.table[paranoid_column].eq(false)
  else
    all.table[paranoid_column].eq(nil)
  end
end

#recovery_valueObject



93
94
95
96
97
98
99
# File 'lib/acts_as_paranoid/core.rb', line 93

def recovery_value
  if boolean_type_not_nullable?
    false
  else
    nil
  end
end

#string_type_with_deleted_value?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/acts_as_paranoid/core.rb', line 57

def string_type_with_deleted_value?
  paranoid_column_type == :string && !paranoid_configuration[:deleted_value].nil?
end

#with_deletedObject



22
23
24
# File 'lib/acts_as_paranoid/core.rb', line 22

def with_deleted
  without_paranoid_default_scope
end