Commit 96aefb80 by ray

use classs_name and foreign_key restore

when the model has_one use class_name or foreign_key, restore use
class_name or setting the foreign_key.
parent 46200fd5
...@@ -123,7 +123,9 @@ module Paranoia ...@@ -123,7 +123,9 @@ module Paranoia
end end
if association_data.nil? && association.macro.to_s == "has_one" if association_data.nil? && association.macro.to_s == "has_one"
Object.const_get(association.name.to_s.camelize).only_deleted.where("#{self.class.name.to_s.underscore}_id", self.id).first.try(:restore, recursive: true) association_class_name = association.options[:class_name].present? ? association.options[:class_name] : association.name.to_s.camelize
association_foreign_key = association.options[:foreign_key].present? ? association.options[:foreign_key] : "#{self.class.name.to_s.underscore}_id"
Object.const_get(association_class_name).only_deleted.where(association_foreign_key, self.id).first.try(:restore, recursive: true)
end end
end end
end end
......
...@@ -13,6 +13,8 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memor ...@@ -13,6 +13,8 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memor
ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' 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_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_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_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 featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))' 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 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)' ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
...@@ -397,7 +399,11 @@ class ParanoiaTest < test_framework ...@@ -397,7 +399,11 @@ class ParanoiaTest < test_framework
# setup and destroy test objects # setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
hasOne.paranoid_model_with_belong = belongsTo hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.save! hasOne.save!
hasOne.destroy hasOne.destroy
...@@ -411,13 +417,19 @@ class ParanoiaTest < test_framework ...@@ -411,13 +417,19 @@ class ParanoiaTest < test_framework
assert_equal true, hasOne.reload.deleted_at.nil? assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}" assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record" assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
end end
def test_new_restore_with_has_one_association def test_new_restore_with_has_one_association
# setup and destroy test objects # setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
hasOne.paranoid_model_with_belong = belongsTo hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.save! hasOne.save!
hasOne.destroy hasOne.destroy
...@@ -432,13 +444,19 @@ class ParanoiaTest < test_framework ...@@ -432,13 +444,19 @@ class ParanoiaTest < test_framework
assert_equal true, hasOne.reload.deleted_at.nil? assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}" assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record" assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
end end
def test_model_restore_with_has_one_association def test_model_restore_with_has_one_association
# setup and destroy test objects # setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
hasOne.paranoid_model_with_belong = belongsTo hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.save! hasOne.save!
hasOne.destroy hasOne.destroy
...@@ -452,6 +470,8 @@ class ParanoiaTest < test_framework ...@@ -452,6 +470,8 @@ class ParanoiaTest < test_framework
assert_equal true, hasOne.reload.deleted_at.nil? assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}" assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record" assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
end end
def test_restore_with_nil_has_one_association def test_restore_with_nil_has_one_association
...@@ -592,9 +612,21 @@ end ...@@ -592,9 +612,21 @@ end
# refer back to regression test for #118 # refer back to regression test for #118
class ParanoidModelWithHasOne < ParanoidModel class ParanoidModelWithHasOne < ParanoidModel
has_one :paranoid_model_with_belong, :dependent => :destroy has_one :paranoid_model_with_belong, :dependent => :destroy
has_one :class_name_belong, :dependent => :destroy, :class_name => "ParanoidModelWithAnthorClassNameBelong"
has_one :paranoid_model_with_foreign_key_belong, :dependent => :destroy, :foreign_key => "has_one_foreign_key_id"
end end
class ParanoidModelWithBelong < ActiveRecord::Base class ParanoidModelWithBelong < ActiveRecord::Base
acts_as_paranoid acts_as_paranoid
belongs_to :paranoid_model_with_has_one belongs_to :paranoid_model_with_has_one
end end
class ParanoidModelWithAnthorClassNameBelong < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_model_with_has_one
end
class ParanoidModelWithForeignKeyBelong < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_model_with_has_one
end
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