Module: ActsAsParanoid::InstanceMethods

Defined in:
lib/rails3_acts_as_paranoid.rb

Instance Method Summary collapse

Instance Method Details

#act_on_dependent_destroy_associationsObject



171
172
173
174
175
176
177
178
179
# File 'lib/rails3_acts_as_paranoid.rb', line 171

def act_on_dependent_destroy_associations
  self.class.dependent_associations.each do |association|
    if association.collection? && self.send(association.name).paranoid?
      association.klass.with_deleted.instance_eval("find_all_by_#{association.foreign_key}(#{self.id.to_json})").each do |object|
        object.destroy!
      end
    end
  end
end

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


181
182
183
# File 'lib/rails3_acts_as_paranoid.rb', line 181

def deleted?
  !paranoid_value.nil?
end

#destroyObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rails3_acts_as_paranoid.rb', line 118

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/rails3_acts_as_paranoid.rb', line 107

def destroy!
  with_transaction_returning_status do
    run_callbacks :destroy do
      act_on_dependent_destroy_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

#paranoid_valueObject



103
104
105
# File 'lib/rails3_acts_as_paranoid.rb', line 103

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

#recover(options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rails3_acts_as_paranoid.rb', line 132

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rails3_acts_as_paranoid.rb', line 148

def recover_dependent_associations(window, options)
  self.class.dependent_associations.each do |association|
    if association.collection? && self.send(association.name).paranoid?
      self.send(association.name).unscoped do
        self.send(association.name).paranoid_deleted_around_time(paranoid_value, window).each do |object|
          object.recover(options) if object.respond_to?(:recover)
        end
      end
    elsif association.macro == :has_one && association.klass.paranoid?
      association.klass.unscoped do
        object = association.klass.paranoid_deleted_around_time(paranoid_value, window).send('find_by_'+association.foreign_key, self.id)
        object.recover(options) if object && object.respond_to?(:recover)
      end
    elsif association.klass.paranoid?
      association.klass.unscoped do
        id = self.send(association.foreign_key)
        object = association.klass.paranoid_deleted_around_time(paranoid_value, window).find_by_id(id)
        object.recover(options) if object && object.respond_to?(:recover)
      end
    end
  end
end