Module: UuidProperties::ClassMethods

Defined in:
lib/uuid_properties.rb

Instance Method Summary collapse

Instance Method Details

#find_by_uuid(uuid, options = {}) ⇒ Object

Postgres will blow up when a UUID type is compared to a string, so sanitize our input first. This also makes sense as a performance optimization – if the input isn’t a UUID anyway, it doesn’t make any sense to go to the database.



26
27
28
29
30
31
32
33
34
35
# File 'lib/uuid_properties.rb', line 26

def find_by_uuid(uuid, options={})
  if uuid =~ UuidProperties::UUID_PATTERN
    # copy merge in case of incoming frozen hash
    options = options.merge(:conditions => ["#{table_name}.uuid = ?", uuid])
    record = self.find(:first, options)
    record
  else
    nil
  end
end

#find_by_uuid!(uuid, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/uuid_properties.rb', line 37

def find_by_uuid!(uuid, options={})
  unless uuid =~ UuidProperties::UUID_PATTERN
    raise ArgumentError, "Improperly formatted UUID."
  end
  # copy merge in case of incoming frozen hash
  options = options.merge(:conditions => ["#{table_name}.uuid = ?", uuid])
  record = self.find(:first, options)
  record || raise(ActiveRecord::RecordNotFound)
end