Module: ActsAsParanoid::InstanceMethods

Defined in:
lib/rails3_acts_as_paranoid.rb

Instance Method Summary collapse

Instance Method Details

#act_on_dependent_destroy_associationsObject



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

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})").each do |object|
        object.destroy!
      end
    end
  end
end

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


179
180
181
# File 'lib/rails3_acts_as_paranoid.rb', line 179

def deleted?
  !paranoid_value.nil?
end

#destroyObject



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

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

#destroy!Object



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

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

#paranoid_valueObject



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

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

#recover(options = {}) ⇒ Object



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

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.update_attributes(self.class.paranoid_column.to_sym => nil)
    end
  end
end

#recover_dependent_associations(window, options) ⇒ Object



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

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