Sha256: 7ae116f495ab1b2f241ae114dbdc6216d6b5ae38d9201ba7ae0624cd33724bb3
Contents?: true
Size: 1.29 KB
Versions: 1
Compression:
Stored size: 1.29 KB
Contents
# frozen_string_literal: true require 'uniqable/version' # Generates uniq, random token for ActiveRecord model's fields module Uniqable def self.included(base) base.extend ClassMethods end module ClassMethods # Uniqable fields and options declaration # @example: # uniqable :uid, :slug, to_param: :uid def uniqable(*fields, to_param: nil) fields = [:uid] if fields.blank? @_uniqable_fields = fields fields.each do |name| before_create { |record| record.uniqable_uid(name) } end # :to_param option if to_param define_method :to_param do public_send(to_param) end end end # Find record by one of the uniq field # usage @example: # uniqable :uid, :slug # ... # MyModel.find_uniqable params[:uid] # can be uid or slug column # @return [self] def find_uniqable(uid) where_sql = @_uniqable_fields.map{ |r| "#{table_name}.#{r} = :uid"}.join(' OR ') self.where(where_sql, uid: uid).take(1) end end # Generate and set random and uniq field # @TODO: split into 2 actions generate and set def uniqable_uid(field) loop do send("#{field}=", SecureRandom.hex(8)) break unless self.class.where(field => send(field.to_sym)).exists? end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
uniqable-0.1.1 | lib/uniqable.rb |