Module: ActsAsParanoid::Core

Defined in:
lib/acts_as_paranoid/core.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/acts_as_paranoid/core.rb', line 3

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


151
152
153
# File 'lib/acts_as_paranoid/core.rb', line 151

def deleted?
  !paranoid_value.nil?
end

#destroyObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/acts_as_paranoid/core.rb', line 85

def destroy
  if paranoid_value.nil?
    with_transaction_returning_status do
      run_callbacks :destroy do
        self.class.delete_all(self.class.primary_key.to_sym => self.id)
        self.paranoid_value = self.class.delete_now_value
        self
      end
    end
  else
    destroy!
  end
end

#destroy!Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/acts_as_paranoid/core.rb', line 74

def destroy!
  with_transaction_returning_status do
    run_callbacks :destroy do
      destroy_dependent_associations!
      self.class.delete_all!(self.class.primary_key.to_sym => self.id)
      self.paranoid_value = self.class.delete_now_value
      freeze
    end
  end
end

#destroy_dependent_associations!Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/acts_as_paranoid/core.rb', line 136

def destroy_dependent_associations!
  self.class.dependent_associations.each do |reflection|
    next unless reflection.klass.paranoid?

    scope = reflection.klass.only_deleted
   
    # Merge in the association's scope
    scope = scope.merge(association(reflection.name).association_scope)

    scope.each do |object|
      object.destroy!
    end
  end
end

#paranoid_valueObject



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

def paranoid_value
  self.send(self.class.paranoid_column)
end

#recover(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/acts_as_paranoid/core.rb', line 99

def recover(options={})
  options = {
    :recursive => self.class.paranoid_configuration[:recover_dependent_associations],
    :recovery_window => self.class.paranoid_configuration[:dependent_recovery_window]
  }.merge(options)

  self.class.transaction do
    run_callbacks :recover do
      recover_dependent_associations(options[:recovery_window], options) if options[:recursive]

      self.paranoid_value = nil
      self.save
    end
  end
end

#recover_dependent_associations(window, options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/acts_as_paranoid/core.rb', line 115

def recover_dependent_associations(window, options)
  self.class.dependent_associations.each do |reflection|
    next unless reflection.klass.paranoid?

    scope = reflection.klass.only_deleted
    
    # Merge in the association's scope
    scope = scope.merge(association(reflection.name).association_scope)

    # We can only recover by window if both parent and dependant have a
    # paranoid column type of :time.
    if self.class.paranoid_column_type == :time && reflection.klass.paranoid_column_type == :time
      scope = scope.merge(reflection.klass.deleted_inside_time_window(paranoid_value, window))
    end

    scope.each do |object|
      object.recover(options)
    end
  end
end