Module: Immortal::ClassMethods
- Defined in:
- lib/immortal.rb
Instance Method Summary collapse
- #count_only_deleted(*args) ⇒ Object
- #count_with_deleted(*args) ⇒ Object
- #delete_all!(*args) ⇒ Object
- #deleted_clause_sql ⇒ Object
- #exists?(id = false) ⇒ Boolean
- #find_only_deleted(*args) ⇒ Object
- #find_with_deleted(*args) ⇒ Object
- #immortal? ⇒ Boolean
- #immortal_delete_all(conditions = nil) ⇒ Object
- #undeleted_clause_sql ⇒ Object
- #without_default_scope ⇒ Object
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_sql ⇒ Object
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
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
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_sql ⇒ Object
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_scope ⇒ Object
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 |