diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-10-31 00:19:21 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-12-18 08:35:03 -0200 |
commit | ba492af25ac985be350e5a3812574b1b11b2dc6c (patch) | |
tree | 0b43a8646ed554acd10ba0d9b69870353a434dbc /activerecord | |
parent | ba98dad113c494ac7a3c09abf16044fce4c0ef5e (diff) | |
download | rails-ba492af25ac985be350e5a3812574b1b11b2dc6c.tar.gz rails-ba492af25ac985be350e5a3812574b1b11b2dc6c.tar.bz2 rails-ba492af25ac985be350e5a3812574b1b11b2dc6c.zip |
Refactor uniqueness validator logic a bit
Cleanup some code, avoid extra hash with reverse_merge, and don't use
send for :id, since it's always present.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index f27fc78717..31dbf7a889 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -4,7 +4,7 @@ module ActiveRecord module Validations class UniquenessValidator < ActiveModel::EachValidator # :nodoc: def initialize(options) - super(options.reverse_merge(:case_sensitive => true)) + super({ case_sensitive: true }.merge!(options)) end # Unfortunately, we have to tie Uniqueness validators to a class. @@ -17,19 +17,17 @@ module ActiveRecord table = finder_class.arel_table coder = record.class.serialized_attributes[attribute.to_s] - if value && coder value = coder.dump value end relation = build_relation(finder_class, table, attribute, value) - relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted? + relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id)) if record.persisted? Array(options[:scope]).each do |scope_item| - reflection = record.class.reflect_on_association(scope_item) - if reflection + if reflection = record.class.reflect_on_association(scope_item) scope_value = record.send(reflection.foreign_key) - scope_item = reflection.foreign_key + scope_item = reflection.foreign_key else scope_value = record.read_attribute(scope_item) end @@ -37,10 +35,7 @@ module ActiveRecord end relation = finder_class.unscoped.where(relation) - - if options[:conditions] - relation = relation.merge(options[:conditions]) - end + relation = relation.merge(options[:conditions]) if options[:conditions] if relation.exists? record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope, :conditions).merge(:value => value)) |