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

Version Path
artfully_ose-1.2.0.pre.19 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.18 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.17 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.16 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.15 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.12 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.11 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.10 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.9 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.8 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.7 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.6 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.5 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.4 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.3 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.2 app/models/ext/ext.rb
artfully_ose-1.2.0.pre.1 app/models/ext/ext.rb
artfully_ose-1.2.0.pre app/models/ext/ext.rb