Module: Immortal::ClassMethods

Defined in:
lib/immortal.rb

Instance Method Summary collapse

Instance Method Details

#count_only_deleted(*args) ⇒ Object



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

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

#count_with_deleted(*args) ⇒ Object



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

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

#delete_all!(*args) ⇒ Object



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

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

#deleted_clause_sqlObject



98
99
100
# File 'lib/immortal.rb', line 98

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

#exists?(id = false) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#find_only_deleted(*args) ⇒ Object



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

def find_only_deleted(*args)
  without_default_scope do
    where(:deleted => true).find(*args)
  end
end

#find_with_deleted(*args) ⇒ Object



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

def find_with_deleted(*args)
  without_default_scope do
    find(*args)
  end
end

#immortal?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/immortal.rb', line 32

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

#immortal_delete_all(conditions = nil) ⇒ Object



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

def immortal_delete_all(conditions = nil)
  unscoped.update_all({:deleted => 1}, conditions)
end

#undeleted_clause_sqlObject



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

def undeleted_clause_sql
  unscoped.where(arel_table[:deleted].eq(nil).or(arel_table[:deleted].eq(false))).constraints.first.to_sql
end

#without_default_scopeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/immortal.rb', line 36

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

  non_immortal_constraints = our_scope.arel.constraints.select do |constraint|
    !constraint.to_sql.include?('deleted')
  end

  non_immortal_constraints_sql = non_immortal_constraints.to_a.map do |constraint|
    constraint.to_sql
  end.join(' AND ')

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

  unscoped do
    with_scope(new_scope) do
      yield
    end
  end
end