Skip to content

Commit

Permalink
Merge pull request #251 from evopark/fix/uniqueness_validator_for_non…
Browse files Browse the repository at this point in the history
…_paranoid_relations

Fix uniqueness validator for non-paranoid associations
  • Loading branch information
radar committed Jul 1, 2015
2 parents 7098750 + f173454 commit 3b6ff6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ class UniquenessValidator < ActiveModel::EachValidator
protected
def build_relation_with_paranoia(klass, table, attribute, value)
relation = build_relation_without_paranoia(klass, table, attribute, value)
relation.and(klass.arel_table[klass.paranoia_column].eq(nil))
if klass.respond_to?(:paranoia_column)
relation.and(klass.arel_table[klass.paranoia_column].eq(nil))
else
relation
end
end
alias_method_chain :build_relation, :paranoia
end
Expand Down
22 changes: 22 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def setup!
'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',
'unparanoid_unique_models' => 'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER'
}.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})"
end
Expand Down Expand Up @@ -821,6 +822,13 @@ def test_callbacks_for_counter_cache_column_update_on_destroy
# assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
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
if ActiveRecord::VERSION::STRING !~ /\A4\.1/
def test_counter_cache_column_update_on_really_destroy
Expand Down Expand Up @@ -862,6 +870,16 @@ class ParanoidModel < ActiveRecord::Base
acts_as_paranoid
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
belongs_to :parent_model
acts_as_paranoid
Expand All @@ -874,6 +892,10 @@ class FeaturefulModel < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
end

class NonParanoidChildModel < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
end

class PlainModel < ActiveRecord::Base
end

Expand Down

0 comments on commit 3b6ff6e

Please sign in to comment.