Sha256: 30cb149e52ccc4b13be72877318a29e0a5bf2252cf4d11c17c65cca5c4f76001
Contents?: true
Size: 845 Bytes
Versions: 18
Compression:
Stored size: 845 Bytes
Contents
module Ext # # Includers will have a uuid generated for them before_create # Method self.multi_find can be used to find by either id or uuid # # Requires a :uuid column on the including class # module Uuid #Required to prevent Ruby from parsing uuid's with leading digits into a primary key id PREFIX = "art-" def self.included(base) base.class_eval do before_create :set_uuid end base.extend ClassMethods end module ClassMethods def multi_find(key, includes = []) arel = self.arel_table where(arel[:id].eq(key).or(arel[:uuid].eq(key))).includes(includes).first end end def set_uuid if self.uuid.nil? self.uuid = SecureRandom.uuid end end def self.uuid PREFIX + SecureRandom.uuid end end end
Version data entries
18 entries across 18 versions & 1 rubygems