Commit 72287624 by Landon Wilkins

Fix association class name for namespaced has one model lookups

Use association.klass.name instead of association.class_name to get the
namespaced class name and therefore prevent:

`Object.get_const(association_class_name)`

from raising "uninitialized constant ParanoidHasOne"
in the following situation:

module Something
  def self.table_name_prefix
    "something_"
  end

  class ParanoidHasOne < ActiveRecord::Base
    acts_as_paranoid
    has_one :paranoid_belongs_to, dependent: :destroy
  end

  class ParanoidBelongsTo < ActiveRecord::Base
    acts_as_paranoid
    belongs_to :paranoid_has_one
  end
end

hasOne = Namespaced::ParanoidHasOne.create
hasOne.destroy
hasOne.restore(:recursive => true) # previously would explode
parent ceb01415
...@@ -144,7 +144,7 @@ module Paranoia ...@@ -144,7 +144,7 @@ module Paranoia
end end
if association_data.nil? && association.macro.to_s == "has_one" if association_data.nil? && association.macro.to_s == "has_one"
association_class_name = association.class_name association_class_name = association.klass.name
association_foreign_key = association.foreign_key association_foreign_key = association.foreign_key
if association.type if association.type
......
...@@ -34,7 +34,9 @@ def setup! ...@@ -34,7 +34,9 @@ def setup!
'custom_column_models' => 'destroyed_at DATETIME', 'custom_column_models' => 'destroyed_at DATETIME',
'custom_sentinel_models' => 'deleted_at DATETIME NOT NULL', 'custom_sentinel_models' => 'deleted_at DATETIME NOT NULL',
'non_paranoid_models' => 'parent_model_id INTEGER', 'non_paranoid_models' => 'parent_model_id INTEGER',
'polymorphic_models' => 'parent_id INTEGER, parent_type STRING, deleted_at DATETIME' 'polymorphic_models' => 'parent_id INTEGER, parent_type STRING, deleted_at DATETIME',
'namespaced_paranoid_has_ones' => 'deleted_at DATETIME, paranoid_belongs_tos_id INTEGER',
'namespaced_paranoid_belongs_tos' => 'deleted_at DATETIME, paranoid_has_one_id INTEGER',
}.each do |table_name, columns_as_sql_string| }.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})" ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})"
end end
...@@ -618,6 +620,19 @@ class ParanoiaTest < test_framework ...@@ -618,6 +620,19 @@ class ParanoiaTest < test_framework
assert hasOne.reload.deleted_at.nil? assert hasOne.reload.deleted_at.nil?
end end
def test_restore_with_module_scoped_has_one_association
# setup and destroy test object
hasOne = Namespaced::ParanoidHasOne.create
hasOne.destroy
assert_equal false, hasOne.reload.deleted_at.nil?
# Does it raise "uninitialized constant ParanoidBelongsTo"
# on restore of ParanoidHasOne?
hasOne.restore(:recursive => true)
assert hasOne.reload.deleted_at.nil?
end
# covers #185 # covers #185
def test_restoring_recursive_has_one_restores_correct_object def test_restoring_recursive_has_one_restores_correct_object
hasOnes = 2.times.map { ParanoidModelWithHasOne.create } hasOnes = 2.times.map { ParanoidModelWithHasOne.create }
...@@ -1001,3 +1016,19 @@ class PolymorphicModel < ActiveRecord::Base ...@@ -1001,3 +1016,19 @@ class PolymorphicModel < ActiveRecord::Base
acts_as_paranoid acts_as_paranoid
belongs_to :parent, polymorphic: true belongs_to :parent, polymorphic: true
end end
module Namespaced
def self.table_name_prefix
"namespaced_"
end
class ParanoidHasOne < ActiveRecord::Base
acts_as_paranoid
has_one :paranoid_belongs_to, dependent: :destroy
end
class ParanoidBelongsTo < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_has_one
end
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