Module: Immortal::ClassMethods

Defined in:
lib/immortal.rb

Instance Method Summary collapse

Instance Method Details

#count_only_deleted(*args) ⇒ Object



52
53
54
55
56
# File 'lib/immortal.rb', line 52

def count_only_deleted(*args)
  without_default_scope do
    immortal.count(*args)
  end
end

#count_with_deleted(*args) ⇒ Object



46
47
48
49
50
# File 'lib/immortal.rb', line 46

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

#delete_all!(*args) ⇒ Object



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

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

#deleted_clause_sqlObject



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

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

#exists?(id = false) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/immortal.rb', line 42

def exists?(id = false)
  mortal.exists?(id)
end

#immortal?Boolean

Returns whether the model supports immortal or not.

Returns:

  • (Boolean)

    whether the model supports immortal or not



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

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

#immortal_delete_all(conditions = nil) ⇒ Object



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

def immortal_delete_all(conditions = nil)
  unscoped.where(conditions).update_all(COLUMN_NAME => 1)
end

#undeleted_clause_sqlObject



78
79
80
# File 'lib/immortal.rb', line 78

def undeleted_clause_sql
  unscoped.mortal.constraints.first.to_sql
end

#where_only_deleted(conditions) ⇒ Object



64
65
66
67
68
# File 'lib/immortal.rb', line 64

def where_only_deleted(conditions)
  without_default_scope do
    immortal.where(conditions)
  end
end

#where_with_deleted(conditions) ⇒ Object



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

def where_with_deleted(conditions)
  without_default_scope do
    where(conditions)
  end
end

#without_default_scopeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/immortal.rb', line 26

def without_default_scope
  new_scope = unscoped
  our_scope = current_scope || unscoped

  non_immortal_constraints_sql = our_scope.arel.constraints.to_a.map do |constraint|
    constraint.to_sql.split('AND').reject { |clause| clause.include?(COLUMN_NAME) }
  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