aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-10-31 00:45:47 -0200
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-12-18 09:04:23 -0200
commit0bb15174558fdeaa0be1889a94347eacfc605f0b (patch)
tree35974c37f5ff4cf9dbab0728716ccaff6f41e3b5 /activerecord/lib/active_record/validations
parent1127262a610486ff54596967e6707a78e944b502 (diff)
downloadrails-0bb15174558fdeaa0be1889a94347eacfc605f0b.tar.gz
rails-0bb15174558fdeaa0be1889a94347eacfc605f0b.tar.bz2
rails-0bb15174558fdeaa0be1889a94347eacfc605f0b.zip
Extract some methods
Diffstat (limited to 'activerecord/lib/active_record/validations')
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb38
1 files changed, 22 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index 2a4a70cd59..1427189851 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -13,25 +13,11 @@ module ActiveRecord
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
table = finder_class.arel_table
-
- coder = record.class.serialized_attributes[attribute.to_s]
- if value && coder
- value = coder.dump value
- end
+ value = deserialize_attribute(record, attribute, value)
relation = build_relation(finder_class, table, attribute, value)
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id)) if record.persisted?
-
- Array(options[:scope]).each do |scope_item|
- if reflection = record.class.reflect_on_association(scope_item)
- scope_value = record.send(reflection.foreign_key)
- scope_item = reflection.foreign_key
- else
- scope_value = record.read_attribute(scope_item)
- end
- relation = relation.and(table[scope_item].eq(scope_value))
- end
-
+ relation = scope_relation(record, table, relation)
relation = finder_class.unscoped.where(relation)
relation.merge!(options[:conditions]) if options[:conditions]
@@ -78,6 +64,26 @@ module ActiveRecord
table[attribute].eq(value)
end
end
+
+ def scope_relation(record, table, relation)
+ Array(options[:scope]).each do |scope_item|
+ if reflection = record.class.reflect_on_association(scope_item)
+ scope_value = record.send(reflection.foreign_key)
+ scope_item = reflection.foreign_key
+ else
+ scope_value = record.read_attribute(scope_item)
+ end
+ relation = relation.and(table[scope_item].eq(scope_value))
+ end
+
+ relation
+ end
+
+ def deserialize_attribute(record, attribute, value)
+ coder = record.class.serialized_attributes[attribute.to_s]
+ value = coder.dump value if value && coder
+ value
+ end
end
module ClassMethods