Commit 1897f97b by Nick Anderson Committed by John Hawthorn

Test for 'cannot touch a new record object' error.

This is a test for a case when a model has a has_one association with an
after_validation callback, and we have failed validation. Paranoia will
attempt to touch the deleted_at column on the child record that has
been built but not saved.

Fixes #180
parent addbcc80
......@@ -15,9 +15,11 @@ def setup!
ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_build_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_and_build_id INTEGER, name VARCHAR(32))'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_anthor_class_name_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_foreign_key_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE not_paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_has_one_and_builds (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, color VARCHAR(32), deleted_at DATETIME, has_one_foreign_key_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))'
ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
......@@ -214,6 +216,11 @@ class ParanoiaTest < test_framework
assert_equal 1, model.class.unscoped.count
end
def test_destroy_behavior_for_has_one_with_build_and_validation_error
model = ParanoidModelWithHasOneAndBuild.create
model.destroy
end
# Regression test for #24
def test_chaining_for_paranoid_models
scope = FeaturefulModel.where(:name => "foo").only_deleted
......@@ -863,6 +870,23 @@ class ParanoidModelWithHasOne < ParanoidModel
has_one :not_paranoid_model_with_belong, :dependent => :destroy
end
class ParanoidModelWithHasOneAndBuild < ActiveRecord::Base
has_one :paranoid_model_with_build_belong, :dependent => :destroy
validates :color, :presence => true
after_validation :build_paranoid_model_with_build_belong, on: :create
private
def build_paranoid_model_with_build_belong
super.tap { |child| child.name = "foo" }
end
end
class ParanoidModelWithBuildBelong < ActiveRecord::Base
acts_as_paranoid
validates :name, :presence => true
belongs_to :paranoid_model_with_has_one_and_build
end
class ParanoidModelWithBelong < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_model_with_has_one
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment