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
- #after_recover(method) ⇒ Object
- #before_recover(method) ⇒ Object
- #boolean_type_not_nullable? ⇒ Boolean
- #delete_all(conditions = nil) ⇒ Object
- #delete_all!(conditions = nil) ⇒ Object
- #delete_now_value ⇒ Object
- #dependent_associations ⇒ Object
- #only_deleted ⇒ Object
- #paranoid_column ⇒ Object
- #paranoid_column_reference ⇒ Object
- #paranoid_column_type ⇒ Object
- #paranoid_default_scope ⇒ Object
- #recovery_value ⇒ Object
- #string_type_with_deleted_value? ⇒ Boolean
- #with_deleted ⇒ Object
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
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_value ⇒ Object
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_associations ⇒ Object
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.[:dependent]) end end |
#only_deleted ⇒ Object
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_column ⇒ Object
65 66 67 |
# File 'lib/acts_as_paranoid/core.rb', line 65 def paranoid_column paranoid_configuration[:column].to_sym end |
#paranoid_column_reference ⇒ Object
73 74 75 |
# File 'lib/acts_as_paranoid/core.rb', line 73 def paranoid_column_reference "#{table_name}.#{paranoid_column}" end |
#paranoid_column_type ⇒ Object
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_scope ⇒ Object
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_value ⇒ Object
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
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_deleted ⇒ Object
22 23 24 |
# File 'lib/acts_as_paranoid/core.rb', line 22 def with_deleted without_paranoid_default_scope end |