+ if self.attributes[colname].class != attr.object_class
+ self.errors.add colname.to_sym, "must be a #{attr.object_class.to_s}, not a #{self.attributes[colname].class.to_s}"
+ elsif self.class.has_symbols? attributes[colname]
+ self.errors.add colname.to_sym, "must not contain symbols: #{attributes[colname].inspect}"
+ end
+ end
+ end
+ end
+
+ def convert_serialized_symbols_to_strings
+ # ensure_serialized_attribute_type should prevent symbols from
+ # getting into the database in the first place. If someone managed
+ # to get them into the database (perhaps using an older version)
+ # we'll convert symbols to strings when loading from the
+ # database. (Otherwise, loading and saving an object with existing
+ # symbols in a serialized field will crash.)
+ self.class.serialized_attributes.each do |colname, attr|
+ if self.class.has_symbols? attributes[colname]
+ attributes[colname] = self.class.recursive_stringify attributes[colname]
+ self.send(colname + '=',
+ self.class.recursive_stringify(attributes[colname]))
+ end
+ end
+ end
+
+ def foreign_key_attributes
+ attributes.keys.select { |a| a.match /_uuid$/ }
+ end
+
+ def skip_uuid_read_permission_check
+ %w(modified_by_client_uuid)
+ end
+
+ def skip_uuid_existence_check
+ []
+ end
+
+ def normalize_collection_uuids
+ foreign_key_attributes.each do |attr|
+ attr_value = send attr
+ if attr_value.is_a? String and
+ attr_value.match /^[0-9a-f]{32,}(\+[@\w]+)*$/
+ begin
+ send "#{attr}=", Collection.normalize_uuid(attr_value)
+ rescue
+ # TODO: abort instead of silently accepting unnormalizable value?
+ end
+ end
+ end
+ end
+
+ @@UUID_REGEX = /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/
+
+ @@prefixes_hash = nil
+ def self.uuid_prefixes
+ unless @@prefixes_hash
+ @@prefixes_hash = {}
+ ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
+ if k.respond_to?(:uuid_prefix)
+ @@prefixes_hash[k.uuid_prefix] = k
+ end
+ end
+ end
+ @@prefixes_hash
+ end
+
+ def self.uuid_like_pattern
+ "_____-#{uuid_prefix}-_______________"
+ end
+
+ def ensure_valid_uuids
+ specials = [system_user_uuid, 'd41d8cd98f00b204e9800998ecf8427e+0']
+
+ foreign_key_attributes.each do |attr|
+ if new_record? or send (attr + "_changed?")
+ next if skip_uuid_existence_check.include? attr
+ attr_value = send attr
+ next if specials.include? attr_value
+ if attr_value
+ if (r = ArvadosModel::resource_class_for_uuid attr_value)
+ unless skip_uuid_read_permission_check.include? attr
+ r = r.readable_by(current_user)
+ end
+ if r.where(uuid: attr_value).count == 0
+ errors.add(attr, "'#{attr_value}' not found")
+ end
+ end