Permalink
Browse files

merge from upstream/rails3.2

  • Loading branch information...
2 parents 8dd7f6a + aa2f9c4 commit 7df8e3c7e26834f2e046b877edf6b62d76c45b6b @mvj3 committed Apr 9, 2013
View
@@ -186,29 +186,32 @@ Paranoiac.pretty.only_deleted.count #=> 1
Associations are also supported. From the simplest behaviors you'd expect to more nifty things like the ones mentioned previously or the usage of the `:with_deleted` option with `belongs_to`
```ruby
-class ParanoiacParent < ActiveRecord::Base
+class Parent < ActiveRecord::Base
has_many :children, :class_name => "ParanoiacChild"
end
class ParanoiacChild < ActiveRecord::Base
- belongs_to :parent, :class_name => "ParanoiacParent"
- belongs_to :parent_with_deleted, :class_name => "ParanoiacParent", :with_deleted => true
+ belongs_to :parent
+ belongs_to :parent_including_deleted, :class_name => "Parent", :with_deleted => true
+ # You cannot name association *_with_deleted
end
-parent = ParanoiacParent.first
+parent = Parent.first
child = parent.children.create
parent.destroy
child.parent #=> nil
-child.parent_with_deleted #=> ParanoiacParent (it works!)
+child.parent_including_deleted #=> Parent (it works!)
```
## Caveats
Watch out for these caveats:
+
- You cannot use scopes named `with_deleted` and `only_deleted`
- You cannot use scopes named `deleted_inside_time_window`, `deleted_before_time`, `deleted_after_time` **if** your paranoid column's type is `time`
+- You cannot name association `*_with_deleted`
- `unscoped` will return all records, deleted or not
# Support
View
@@ -13,7 +13,7 @@ gemspec = eval(File.read(Dir["*.gemspec"].first))
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the rails3_acts_as_paranoid plugin.'
+desc 'Test the acts_as_paranoid plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
@@ -25,7 +25,7 @@ Rake::TestTask.new(:test) do |t|
t.verbose = true
end
-desc 'Generate documentation for the rails3_acts_as_paranoid plugin.'
+desc 'Generate documentation for the acts_as_paranoid plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ActsAsParanoid'
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "acts_as_paranoid"
- s.version = "0.0.0"
+ s.version = "0.4.1"
s.platform = Gem::Platform::RUBY
s.authors = ["Goncalo Silva", "Charles G.", "Rick Olson"]
s.email = ["goncalossilva@gmail.com"]
View
@@ -1 +1 @@
-require 'rails3_acts_as_paranoid'
+require 'acts_as_paranoid'
@@ -37,6 +37,9 @@ def acts_as_paranoid(options = {})
# Magic!
default_scope { where(paranoid_default_scope_sql) }
+ # The paranoid column should not be mass-assignable
+ attr_protected paranoid_configuration[:column]
+
if paranoid_configuration[:column_type] == 'time'
scope :deleted_inside_time_window, lambda {|time, window|
deleted_after_time((time - window)).deleted_before_time((time + window))
@@ -96,7 +96,7 @@ def destroy!
run_callbacks :destroy do
destroy_dependent_associations!
# Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
- self.class.delete_all!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
+ self.class.delete_all!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose]) if persisted?
self.paranoid_value = self.class.delete_now_value
freeze
end
@@ -108,7 +108,7 @@ def destroy
with_transaction_returning_status do
run_callbacks :destroy do
# Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
- self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
+ self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose]) if persisted?
self.paranoid_value = self.class.delete_now_value
self
end
View
@@ -11,6 +11,11 @@ def test_paranoid?
assert ParanoidTime.paranoid?
end
+ def test_attr_protected_column
+ pt = ParanoidTime.new(:deleted_at => Time.now)
+ assert_nil pt.deleted_at
+ end
+
def test_scope_inclusion_with_time_column_type
assert ParanoidTime.respond_to?(:deleted_inside_time_window)
assert ParanoidTime.respond_to?(:deleted_before_time)
@@ -63,6 +68,20 @@ def test_real_removal
assert_empty ParanoidTime.with_deleted.all
end
+ def test_non_persisted_destroy
+ pt = ParanoidTime.new
+ assert_nil pt.paranoid_value
+ pt.destroy
+ assert_not_nil pt.paranoid_value
+ end
+
+ def test_non_persisted_destroy!
+ pt = ParanoidTime.new
+ assert_nil pt.paranoid_value
+ pt.destroy!
+ assert_not_nil pt.paranoid_value
+ end
+
def test_recovery
assert_equal 3, ParanoidBoolean.count
ParanoidBoolean.first.destroy

0 comments on commit 7df8e3c

Please sign in to comment.