Module: ValidatesUniquenessOfInMemory

Defined in:
lib/validate_uniqueness_of_in_memory.rb

Instance Method Summary collapse

Instance Method Details

#validate_uniqueness_of_in_memory(collection, attrs, message) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/validate_uniqueness_of_in_memory.rb', line 5

def validate_uniqueness_of_in_memory(collection, attrs, message)
  hashes = self.send(collection).inject({}) do |hash, record|
    key = attrs.map {|a| record.send(a).to_s }.join
    if key.blank? || record.marked_for_destruction?
      key = record.object_id
    end
    hash[key] = record unless hash[key]
    hash
  end
  if collection.length > hashes.length
    self.errors.add(:base, message)
  end
end