Sha256: 634dd15dc479ecdacd8a06f837195170ea51e471ab5ddd0984374b33820f9460
Contents?: true
Size: 1.53 KB
Versions: 3
Compression:
Stored size: 1.53 KB
Contents
module Uuids # Stores uuids assigned to corresponding records. # # Attributes: # +value+:: A value of uuid as defined in # {RFC4122}[http://www.ietf.org/rfc/rfc4122.txt]. # Assigned by default on creation. Cannot be set or edited manually. # +record+:: An AR record the uuid is assigned to. # Required attribute. Can be changed. # # @example # # Create the uuid # uuid = Uuids::Uuid.create! record: some_record # # # Find a record by uuid # Uuid.find_by_value(uuid.value).record # => some_record # # # Reassigns a uuid to another record # uuid.update_attributes! record: another_record # class Uuid < ActiveRecord::Base self.table_name = :uuids_uuids # A format for UUID following RFC4122 UUID_FORMAT = /[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}/ attr_readonly :value belongs_to :record, polymorphic: true validates :value, format: { with: UUID_FORMAT }, allow_nil: true validate :record_present? before_create :set_default_value before_destroy :forbid_destruction private # Validates record presence with a custom error message. def record_present? return if record errors.add :record, :blank, uuid: value end # Sets a new value by default def set_default_value self.value = SecureRandom.uuid unless value end # Forbids destruction of the record. def forbid_destruction errors.add :base, :destruction_forbidden false end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
uuids-1.4.2 | app/models/uuids/uuid.rb |
uuids-1.4.1 | app/models/uuids/uuid.rb |
uuids-1.4.0 | app/models/uuids/uuid.rb |