Commit 3b6ff6ec by Ryan Bigg

Merge pull request #251 from evopark/fix/uniqueness_validator_for_non_paranoid_relations

Fix uniqueness validator for non-paranoid associations
parents 7098750c f173454b
...@@ -250,7 +250,11 @@ module ActiveRecord ...@@ -250,7 +250,11 @@ module ActiveRecord
protected protected
def build_relation_with_paranoia(klass, table, attribute, value) def build_relation_with_paranoia(klass, table, attribute, value)
relation = build_relation_without_paranoia(klass, table, attribute, value) relation = build_relation_without_paranoia(klass, table, attribute, value)
if klass.respond_to?(:paranoia_column)
relation.and(klass.arel_table[klass.paranoia_column].eq(nil)) relation.and(klass.arel_table[klass.paranoia_column].eq(nil))
else
relation
end
end end
alias_method_chain :build_relation, :paranoia alias_method_chain :build_relation, :paranoia
end end
......
...@@ -37,6 +37,7 @@ def setup! ...@@ -37,6 +37,7 @@ def setup!
'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_has_ones' => 'deleted_at DATETIME, paranoid_belongs_tos_id INTEGER',
'namespaced_paranoid_belongs_tos' => 'deleted_at DATETIME, paranoid_has_one_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'
}.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
...@@ -821,6 +822,13 @@ class ParanoiaTest < test_framework ...@@ -821,6 +822,13 @@ class ParanoiaTest < test_framework
# assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called) # assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
end end
def test_uniqueness_for_unparanoid_associated
parent_model = ParanoidWithUnparanoids.create
related = parent_model.unparanoid_unique_models.create
# will raise exception if model is not checked for paranoia
related.valid?
end
# TODO: find a fix for Rails 4.1 # TODO: find a fix for Rails 4.1
if ActiveRecord::VERSION::STRING !~ /\A4\.1/ if ActiveRecord::VERSION::STRING !~ /\A4\.1/
def test_counter_cache_column_update_on_really_destroy def test_counter_cache_column_update_on_really_destroy
...@@ -862,6 +870,16 @@ class ParanoidModel < ActiveRecord::Base ...@@ -862,6 +870,16 @@ class ParanoidModel < ActiveRecord::Base
acts_as_paranoid acts_as_paranoid
end end
class ParanoidWithUnparanoids < ActiveRecord::Base
self.table_name = 'plain_models'
has_many :unparanoid_unique_models
end
class UnparanoidUniqueModel < ActiveRecord::Base
belongs_to :paranoid_with_unparanoids
validates :name, :uniqueness => true
end
class FailCallbackModel < ActiveRecord::Base class FailCallbackModel < ActiveRecord::Base
belongs_to :parent_model belongs_to :parent_model
acts_as_paranoid acts_as_paranoid
...@@ -874,6 +892,10 @@ class FeaturefulModel < ActiveRecord::Base ...@@ -874,6 +892,10 @@ class FeaturefulModel < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true validates :name, :presence => true, :uniqueness => true
end end
class NonParanoidChildModel < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
end
class PlainModel < ActiveRecord::Base class PlainModel < ActiveRecord::Base
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