test/paranoia_test.rb in paranoia-2.2.0 vs test/paranoia_test.rb in paranoia-2.2.1

- old
+ new

@@ -38,10 +38,12 @@ 'namespaced_paranoid_has_ones' => 'deleted_at DATETIME, paranoid_belongs_tos_id INTEGER', 'namespaced_paranoid_belongs_tos' => 'deleted_at DATETIME, paranoid_has_one_id INTEGER', 'unparanoid_unique_models' => 'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER', 'active_column_models' => 'deleted_at DATETIME, active BOOLEAN', 'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN', + 'paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN, active_column_model_with_has_many_relationship_id INTEGER', + 'active_column_model_with_has_many_relationships' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN', 'without_default_scope_models' => 'deleted_at DATETIME' }.each do |table_name, columns_as_sql_string| ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})" end end @@ -180,20 +182,34 @@ parent2 = ParentModel.create p1 = ParanoidModel.create(:parent_model => parent1) p2 = ParanoidModel.create(:parent_model => parent2) p1.destroy p2.destroy + assert_equal 0, parent1.paranoid_models.count assert_equal 1, parent1.paranoid_models.only_deleted.count + + assert_equal 2, ParanoidModel.only_deleted.joins(:parent_model).count assert_equal 1, parent1.paranoid_models.deleted.count assert_equal 0, parent1.paranoid_models.without_deleted.count p3 = ParanoidModel.create(:parent_model => parent1) assert_equal 2, parent1.paranoid_models.with_deleted.count assert_equal 1, parent1.paranoid_models.without_deleted.count assert_equal [p1,p3], parent1.paranoid_models.with_deleted end + def test_only_deleted_with_joins + c1 = ActiveColumnModelWithHasManyRelationship.create(name: 'Jacky') + c2 = ActiveColumnModelWithHasManyRelationship.create(name: 'Thomas') + p1 = ParanoidModelWithBelongsToActiveColumnModelWithHasManyRelationship.create(name: 'Hello', active_column_model_with_has_many_relationship: c1) + + c1.destroy + assert_equal 1, ActiveColumnModelWithHasManyRelationship.count + assert_equal 1, ActiveColumnModelWithHasManyRelationship.only_deleted.count + assert_equal 1, ActiveColumnModelWithHasManyRelationship.only_deleted.joins(:paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships).count + end + def test_destroy_behavior_for_custom_column_models model = CustomColumnModel.new assert_equal 0, model.class.count model.save! assert_nil model.destroyed_at @@ -772,10 +788,17 @@ a = Employer.create!(name: "A") b = Employer.new(name: "A") refute b.valid? end + def test_updated_at_modification_on_destroy + paranoid_model = ParanoidModelWithTimestamp.create(:parent_model => ParentModel.create, :updated_at => 1.day.ago) + assert paranoid_model.updated_at < 10.minutes.ago + paranoid_model.destroy + assert paranoid_model.updated_at > 10.minutes.ago + end + def test_updated_at_modification_on_restore parent1 = ParentModel.create pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1) ParanoidModelWithTimestamp.record_timestamps = false pt1.update_columns(created_at: 20.years.ago, updated_at: 10.years.ago, deleted_at: 10.years.ago) @@ -1086,9 +1109,48 @@ end end class ActiveColumnModelWithUniquenessValidation < ActiveRecord::Base validates :name, :uniqueness => true + acts_as_paranoid column: :active, sentinel_value: true + + def paranoia_restore_attributes + { + deleted_at: nil, + active: true + } + end + + def paranoia_destroy_attributes + { + deleted_at: current_time_from_proper_timezone, + active: nil + } + end +end + +class ActiveColumnModelWithHasManyRelationship < ActiveRecord::Base + has_many :paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships + acts_as_paranoid column: :active, sentinel_value: true + + def paranoia_restore_attributes + { + deleted_at: nil, + active: true + } + end + + def paranoia_destroy_attributes + { + deleted_at: current_time_from_proper_timezone, + active: nil + } + end +end + +class ParanoidModelWithBelongsToActiveColumnModelWithHasManyRelationship < ActiveRecord::Base + belongs_to :active_column_model_with_has_many_relationship + acts_as_paranoid column: :active, sentinel_value: true def paranoia_restore_attributes { deleted_at: nil,