Class: AWS::Record::UniquenessValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/simple_unique/validations/uniqueness.rb

Constant Summary collapse

ACCEPTED_OPTIONS =
[:message, :scope, :allow_nil, :allow_blank, :if, :unless]

Instance Method Summary collapse

Instance Method Details

#messageObject



30
31
32
# File 'lib/simple_unique/validations/uniqueness.rb', line 30

def message
  options[:message] || 'has already been taken'
end

#validate_attribute(record, attribute_name, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/simple_unique/validations/uniqueness.rb', line 9

def validate_attribute record, attribute_name, value

  # @TODO - Model the initial relation setup after the setup
  # found in ActiveRecord::Validations::UniquenessValidator.
  # Not sure it we have to go to such lengths or not...
  relation = record.class

  scope_array(options[:scope]).each do |scope_item|
    scope_value = record.send(scope_item.to_sym)
    relation = relation.where(scope_item.to_sym => scope_value)
  end

  existing_record = SimpleDB.consistent_reads do
    relation.where(attribute_name.to_sym => value).first
  end
  taken = !existing_record.nil?

  record.errors.add(attribute_name, message) if taken #blank

end