Commit 10194e05 by Ryan Bigg

Merge pull request #123 from JeskTop/setting_restore

hasOne association could not restore
parents ceaaecb1 c98d4083
...@@ -140,6 +140,12 @@ module Paranoia ...@@ -140,6 +140,12 @@ module Paranoia
end end
end end
end end
if association_data.nil? && association.macro.to_s == "has_one"
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
clear_association_cache if destroyed_associations.present? clear_association_cache if destroyed_associations.present?
......
...@@ -14,6 +14,8 @@ def connect! ...@@ -14,6 +14,8 @@ def connect!
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)'
...@@ -446,7 +448,11 @@ class ParanoiaTest < test_framework ...@@ -446,7 +448,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
...@@ -460,6 +466,61 @@ class ParanoiaTest < test_framework ...@@ -460,6 +466,61 @@ 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
def test_new_restore_with_has_one_association
# setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.save!
hasOne.destroy
assert_equal false, hasOne.deleted_at.nil?
assert_equal false, belongsTo.deleted_at.nil?
# Does it restore has_one associations?
newHasOne = ParanoidModelWithHasOne.with_deleted.find(hasOne.id)
newHasOne.restore(:recursive => true)
newHasOne.save!
assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
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
def test_model_restore_with_has_one_association
# setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.save!
hasOne.destroy
assert_equal false, hasOne.deleted_at.nil?
assert_equal false, belongsTo.deleted_at.nil?
# Does it restore has_one associations?
ParanoidModelWithHasOne.restore(hasOne.id, :recursive => true)
hasOne.save!
assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
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
...@@ -664,6 +725,8 @@ end ...@@ -664,6 +725,8 @@ 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
...@@ -671,6 +734,24 @@ class ParanoidModelWithBelong < ActiveRecord::Base ...@@ -671,6 +734,24 @@ class ParanoidModelWithBelong < ActiveRecord::Base
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
class FlaggedModel < PlainModel
acts_as_paranoid :flag_column => :is_deleted
end
class FlaggedModelWithCustomIndex < PlainModel
acts_as_paranoid :flag_column => :is_deleted, :indexed_column => :is_deleted
end
class AsplodeModel < ActiveRecord::Base class AsplodeModel < ActiveRecord::Base
acts_as_paranoid acts_as_paranoid
before_destroy do |r| before_destroy do |r|
......
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