Module: Immortal::ClassMethods

Defined in:
lib/immortal.rb

Instance Method Summary collapse

Instance Method Details

#count_only_deleted(*args) ⇒ Object



49
50
51
52
53
# File 'lib/immortal.rb', line 49

def count_only_deleted(*args)
  without_default_scope do
    where(deleted: true).count(*args)
  end
end

#count_with_deleted(*args) ⇒ Object



43
44
45
46
47
# File 'lib/immortal.rb', line 43

def count_with_deleted(*args)
  without_default_scope do
    count(*args)
  end
end

#delete_all!(*args) ⇒ Object



85
86
87
# File 'lib/immortal.rb', line 85

def delete_all!(*args)
  unscoped.mortal_delete_all(*args)
end

#deleted_clause_sqlObject



93
94
95
# File 'lib/immortal.rb', line 93

def deleted_clause_sql
  unscoped.where(arel_table[:deleted].eq(true)).constraints.first.to_sql
end

#exists?(id = false) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/immortal.rb', line 39

def exists?(id = false)
  where(deleted: false).exists?(id)
end

#find_only_deleted(*args) ⇒ Object



74
75
76
77
78
79
# File 'lib/immortal.rb', line 74

def find_only_deleted(*args)
  ActiveSupport::Deprecation.warn('[immortal] we are deprecating #find_only_deleted use where_only_deleted instead')
  without_default_scope do
    where(deleted: true).find(*args)
  end
end

#find_with_deleted(*args) ⇒ Object



61
62
63
64
65
66
# File 'lib/immortal.rb', line 61

def find_with_deleted(*args)
  ActiveSupport::Deprecation.warn('[immortal] we are deprecating #find_with_deleted use where_with_deleted instead')
  without_default_scope do
    find(*args)
  end
end

#immortal?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/immortal.rb', line 19

def immortal?
  self.included_modules.include?(::Immortal::InstanceMethods)
end

#immortal_delete_all(conditions = nil) ⇒ Object



81
82
83
# File 'lib/immortal.rb', line 81

def immortal_delete_all(conditions = nil)
  unscoped.where(conditions).update_all(deleted: 1)
end

#undeleted_clause_sqlObject



89
90
91
# File 'lib/immortal.rb', line 89

def undeleted_clause_sql
  unscoped.where(deleted: false).constraints.first.to_sql
end

#where_only_deleted(*args) ⇒ Object



68
69
70
71
72
# File 'lib/immortal.rb', line 68

def where_only_deleted(*args)
  without_default_scope do
    where(deleted: true).where(args)
  end
end

#where_with_deleted(*args) ⇒ Object



55
56
57
58
59
# File 'lib/immortal.rb', line 55

def where_with_deleted(*args)
  without_default_scope do
    where(*args)
  end
end

#without_default_scopeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/immortal.rb', line 23

def without_default_scope
  new_scope = self.unscoped
  our_scope = self.current_scope || self.unscoped

  non_immortal_constraints_sql = our_scope.arel.constraints.to_a.map do |constraint|
    constraint.to_sql.split('AND').reject{|clause| clause.include?('deleted')}
  end.flatten.join(' AND ')

  new_scope = new_scope.merge(our_scope.except(:where))
  new_scope = new_scope.where(non_immortal_constraints_sql)

  unscoped.merge(new_scope).scoping do
    yield
  end
end