Permalink
Browse files

support boolean with default set to false, rather than NULL; and pass…

…ed all tests
  • Loading branch information...
1 parent 9088943 commit 8dd7f6a60b4964df40b6d45bd4ab64afe19ca45f @mvj3 committed Apr 9, 2013
Showing with 68 additions and 50 deletions.
  1. +1 −0 Gemfile
  2. +1 −1 Rakefile
  3. +11 −8 lib/acts_as_paranoid/core.rb
  4. +8 −8 lib/rails3_acts_as_paranoid.rb
  5. +18 −18 test/test_core.rb
  6. +24 −10 test/test_helper.rb
  7. +5 −5 test/test_validations.rb
View
@@ -18,4 +18,5 @@ group :test do
gem "minitest"
gem "ZenTest"
gem "autotest-growl"
+ # gem 'pry-debugger'
end
View
@@ -5,7 +5,7 @@ rescue LoadError
$stderr.puts "You need to have Bundler installed to be able build this gem."
end
require "rake/testtask"
-require "rake/rdoctask"
+require "rdoc/task"
gemspec = eval(File.read(Dir["*.gemspec"].first))
@@ -22,11 +22,8 @@ def with_deleted
end
def only_deleted
- if string_type_with_deleted_value?
- without_paranoid_default_scope.where("#{paranoid_column_reference} IS ?", paranoid_configuration[:deleted_value])
- else
- without_paranoid_default_scope.where("#{paranoid_column_reference} IS NOT ?", nil)
- end
+ _is_not = string_type_with_deleted_value? ? "" : " NOT "
+ without_paranoid_default_scope.where("#{paranoid_column_reference} IS #{_is_not} ?", paranoid_configuration[:deleted_value])
end
def delete_all!(conditions = nil)
@@ -38,10 +35,15 @@ def delete_all(conditions = nil)
end
def paranoid_default_scope_sql
+ # lazy load self.columns
+ @@set_paranoid_configuration_deleted_value ||= begin
+ if (paranoid_configuration[:column_type] == "boolean") && (self.columns.detect {|column| column.name == self.paranoid_configuration[:column] }.default === false)
+ self.paranoid_configuration.merge!({:deleted_value => true})
+ end
+ end
+
if string_type_with_deleted_value?
- self.scoped.table[paranoid_column].eq(nil).
- or(self.scoped.table[paranoid_column].not_eq(paranoid_configuration[:deleted_value])).
- to_sql
+ self.scoped.table[paranoid_column].eq(nil).or(self.scoped.table[paranoid_column].not_eq(paranoid_configuration[:deleted_value])).to_sql
else
self.scoped.table[paranoid_column].eq(nil).to_sql
end
@@ -180,5 +182,6 @@ def deleted?
def paranoid_value=(value)
self.send("#{self.class.paranoid_column}=", value)
end
+
end
end
@@ -8,32 +8,32 @@
module ActsAsParanoid
-
+
def paranoid?
self.included_modules.include?(ActsAsParanoid::Core)
end
-
+
def validates_as_paranoid
include ActsAsParanoid::Validations
end
-
+
def acts_as_paranoid(options = {})
raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
-
+
class_attribute :paranoid_configuration, :paranoid_column_reference
-
+
self.paranoid_configuration = { :column => "deleted_at", :column_type => "time", :recover_dependent_associations => true, :dependent_recovery_window => 2.minutes }
self.paranoid_configuration.merge!({ :deleted_value => "deleted" }) if options[:column_type] == "string"
self.paranoid_configuration.merge!(options) # user options
raise ArgumentError, "'time', 'boolean' or 'string' expected for :column_type option, got #{paranoid_configuration[:column_type]}" unless ['time', 'boolean', 'string'].include? paranoid_configuration[:column_type]
self.paranoid_column_reference = "#{self.table_name}.#{paranoid_configuration[:column]}"
-
+
return if paranoid?
-
+
include ActsAsParanoid::Core
-
+
# Magic!
default_scope { where(paranoid_default_scope_sql) }
View
@@ -6,7 +6,7 @@ def test_paranoid?
assert_raise(NoMethodError) { NotParanoid.delete_all! }
assert_raise(NoMethodError) { NotParanoid.first.destroy! }
assert_raise(NoMethodError) { NotParanoid.with_deleted }
- assert_raise(NoMethodError) { NotParanoid.only_deleted }
+ assert_raise(NoMethodError) { NotParanoid.only_deleted }
assert ParanoidTime.paranoid?
end
@@ -79,7 +79,7 @@ def test_recovery
def setup_recursive_tests
@paranoid_time_object = ParanoidTime.first
-
+
# Create one extra ParanoidHasManyDependant record so that we can validate
# the correct dependants are recovered.
ParanoidTime.where('id <> ?', @paranoid_time_object.id).first.paranoid_has_many_dependants.create(:name => "should not be recovered").destroy
@@ -208,25 +208,25 @@ def test_deleted?
ParanoidString.first.destroy
assert ParanoidString.with_deleted.first.deleted?
end
-
+
def test_paranoid_destroy_callbacks
@paranoid_with_callback = ParanoidWithCallback.first
ParanoidWithCallback.transaction do
@paranoid_with_callback.destroy
end
-
+
assert @paranoid_with_callback.called_before_destroy
assert @paranoid_with_callback.called_after_destroy
assert @paranoid_with_callback.called_after_commit_on_destroy
end
-
+
def test_hard_destroy_callbacks
@paranoid_with_callback = ParanoidWithCallback.first
-
+
ParanoidWithCallback.transaction do
@paranoid_with_callback.destroy!
end
-
+
assert @paranoid_with_callback.called_before_destroy
assert @paranoid_with_callback.called_after_destroy
assert @paranoid_with_callback.called_after_commit_on_destroy
@@ -252,45 +252,45 @@ def test_delete_by_multiple_id_is_paranoid
model_a = ParanoidBelongsDependant.create
model_b = ParanoidBelongsDependant.create
ParanoidBelongsDependant.delete([model_a.id, model_b.id])
-
+
assert_paranoid_deletion(model_a)
assert_paranoid_deletion(model_b)
end
-
+
def test_destroy_by_multiple_id_is_paranoid
model_a = ParanoidBelongsDependant.create
model_b = ParanoidBelongsDependant.create
ParanoidBelongsDependant.destroy([model_a.id, model_b.id])
-
+
assert_paranoid_deletion(model_a)
assert_paranoid_deletion(model_b)
end
-
+
def test_delete_by_single_id_is_paranoid
model = ParanoidBelongsDependant.create
ParanoidBelongsDependant.delete(model.id)
-
+
assert_paranoid_deletion(model)
end
-
+
def test_destroy_by_single_id_is_paranoid
model = ParanoidBelongsDependant.create
ParanoidBelongsDependant.destroy(model.id)
-
+
assert_paranoid_deletion(model)
end
-
+
def test_instance_delete_is_paranoid
model = ParanoidBelongsDependant.create
model.delete
-
+
assert_paranoid_deletion(model)
end
-
+
def test_instance_destroy_is_paranoid
model = ParanoidBelongsDependant.create
model.destroy
-
+
assert_paranoid_deletion(model)
end
View
@@ -31,6 +31,16 @@ def setup_db
t.timestamps
end
+ create_table :paranoid_boolean_with_delete_falses do |t|
+ t.string :name
+ t.boolean :is_deleted
+ t.integer :paranoid_time_id
+
+ t.timestamps
+ end
+
+
+
create_table :paranoid_strings do |t|
t.string :name
t.string :deleted
@@ -198,6 +208,10 @@ class ParanoidBoolean < ActiveRecord::Base
has_one :paranoid_has_one_dependant, :dependent => :destroy
end
+class ParanoidBooleanWithDeleteFalse < ActiveRecord::Base
+ acts_as_paranoid :column_type => "boolean", :column => "is_deleted"
+end
+
class ParanoidString < ActiveRecord::Base
acts_as_paranoid :column_type => "string", :column => "deleted", :deleted_value => "dead"
end
@@ -237,31 +251,31 @@ class ParanoidHasOneDependant < ActiveRecord::Base
class ParanoidWithCallback < ActiveRecord::Base
acts_as_paranoid
-
+
attr_accessor :called_before_destroy, :called_after_destroy, :called_after_commit_on_destroy
attr_accessor :called_before_recover, :called_after_recover
-
+
before_destroy :call_me_before_destroy
after_destroy :call_me_after_destroy
-
+
after_commit :call_me_after_commit_on_destroy, :on => :destroy
before_recover :call_me_before_recover
after_recover :call_me_after_recover
-
+
def initialize(*attrs)
@called_before_destroy = @called_after_destroy = @called_after_commit_on_destroy = false
super(*attrs)
end
-
+
def call_me_before_destroy
@called_before_destroy = true
end
-
+
def call_me_after_destroy
@called_after_destroy = true
end
-
+
def call_me_after_commit_on_destroy
@called_after_commit_on_destroy = true
end
@@ -369,17 +383,17 @@ def setup
def teardown
teardown_db
end
-
+
def assert_empty(collection)
assert(collection.respond_to?(:empty?) && collection.empty?)
end
-
+
def assert_paranoid_deletion(model)
row = find_row(model)
assert_not_nil row, "#{model.class} entirely deleted"
assert_not_nil row["deleted_at"], "Deleted at not set"
end
-
+
def assert_non_paranoid_deletion(model)
row = find_row(model)
assert_nil row, "#{model.class} still exists"
View
@@ -23,19 +23,19 @@ def test_should_validate_without_deleted
def test_models_with_scoped_validations_can_be_multiply_deleted
model_a = ParanoidWithScopedValidation.create(:name => "Model A", :category => "Category A")
model_b = ParanoidWithScopedValidation.create(:name => "Model B", :category => "Category B")
-
+
ParanoidWithScopedValidation.delete([model_a.id, model_b.id])
-
+
assert_paranoid_deletion(model_a)
assert_paranoid_deletion(model_b)
end
-
+
def test_models_with_scoped_validations_can_be_multiply_destroyed
model_a = ParanoidWithScopedValidation.create(:name => "Model A", :category => "Category A")
model_b = ParanoidWithScopedValidation.create(:name => "Model B", :category => "Category B")
-
+
ParanoidWithScopedValidation.destroy([model_a.id, model_b.id])
-
+
assert_paranoid_deletion(model_a)
assert_paranoid_deletion(model_b)
end

0 comments on commit 8dd7f6a

Please sign in to comment.