Skip to content

Commit

Permalink
renamed deleted_around scope to paranoid_deleted_around_time (less co…
Browse files Browse the repository at this point in the history
…nflict probability?)
  • Loading branch information
goncalossilva committed Apr 2, 2011
1 parent 6c7956f commit 784f469
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
27 changes: 14 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ PATH
GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.0)
activesupport (= 3.0.0)
activemodel (3.0.5)
activesupport (= 3.0.5)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
arel (~> 1.0.0)
i18n (~> 0.4)
activerecord (3.0.5)
activemodel (= 3.0.5)
activesupport (= 3.0.5)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activesupport (3.0.0)
arel (1.0.1)
activesupport (~> 3.0.0)
activesupport (3.0.5)
arel (2.0.9)
builder (2.1.2)
i18n (0.4.2)
i18n (0.5.0)
rake (0.8.7)
sqlite3-ruby (1.3.2)
tzinfo (0.3.25)
sqlite3 (1.3.3)
sqlite3-ruby (1.3.3)
sqlite3 (>= 1.3.3)
tzinfo (0.3.26)

PLATFORMS
ruby
Expand Down
19 changes: 9 additions & 10 deletions lib/rails3_acts_as_paranoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class << self
alias_method :destroy!, :destroy
end

default_scope where("#{paranoid_column_reference} IS ?", nil)
default_scope where("#{paranoid_column_reference} IS ?", nil) # Magic!

scope :deleted_around, lambda {|value, window|
scope :paranoid_deleted_around_time, lambda {|value, window|
if self.class.respond_to?(:paranoid?) && self.class.paranoid?
if self.class.paranoid_column_type == 'time' && ![true, false].include?(value)
self.where("#{self.class.paranoid_column} > ? AND #{self.class.paranoid_column} < ?", (value - window), (value + window))
Expand Down Expand Up @@ -83,7 +83,6 @@ def delete_now_value
when "boolean" then true
end
end

end

module InstanceMethods
Expand All @@ -95,7 +94,7 @@ def paranoid_value
def destroy!
with_transaction_returning_status do
run_callbacks :destroy do
self.class.delete_all!(:id => self)
self.class.delete_all!(:id => self.id)
self.paranoid_value = self.class.delete_now_value
freeze
end
Expand All @@ -105,7 +104,7 @@ def destroy!
def destroy
with_transaction_returning_status do
run_callbacks :destroy do
if paranoid_value == nil
if paranoid_value.nil?
self.class.delete_all(:id => self.id)
else
self.class.delete_all!(:id => self.id)
Expand All @@ -114,8 +113,8 @@ def destroy
end
end
end

def recover(options = {})
def recover(options={})
options = {
:recursive => self.class.paranoid_configuration[:recover_dependent_associations],
:recovery_window => self.class.paranoid_configuration[:dependent_recovery_window]
Expand All @@ -132,19 +131,19 @@ 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).deleted_around(paranoid_value, window).each do |object|
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.deleted_around(paranoid_value, window).send('find_by_'+association.primary_key_name, self.id)
object = association.klass.paranoid_deleted_around_time(paranoid_value, window).send('find_by_'+association.primary_key_name, self.id)
object.recover(options) if object && object.respond_to?(:recover)
end
elsif association.klass.paranoid?
association.klass.unscoped do
id = self.send(association.primary_key_name)
object = association.klass.deleted_around(paranoid_value, window).find_by_id(id)
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
Expand Down

0 comments on commit 784f469

Please sign in to comment.