Sha256: 4897d5f6d67b7453c099c1f3c660ce0aabaa6ef1c5446834258187652db8225c

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module ActsAsKeyed
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def acts_as_keyed(options={})
      class_inheritable_accessor :options
      options[:size] ||= 10
      options[:chars] ||= ('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a - ['l','I','O']
      self.options = options

      raise ArgumentError, "#{self.name} is missing key column" if columns_hash['key'].nil?

      if Rails.version < '3'
        before_validation_on_create :create_key
      else
        before_validation :create_key, :on => :create
      end

      attr_protected :key

      class << self
        def find(*args)
          if self.options[:as_param] && args.first.is_a?(String)
            find_by_key(args)
          else
            super(*args)
          end
        end
      end
    
      include InstanceMethods
    end
  end

  module InstanceMethods

    def to_param
      options[:as_param] ? self.key : self.id.to_s
    end

    def regenerate_key!
      self.create_key
      self.save
    end

    protected

    def create_key
      k = random_key
      while(self.class.count(:conditions => { :key => k }) > 0)
        k = random_key
      end
      self.key = k
    end

    def random_key
      code_array=[]
      1.upto(options[:size]) { code_array << options[:chars][rand(options[:chars].length)] }
      code_array.join('')
    end
  end
end

ActiveRecord::Base.send :include, ActsAsKeyed

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_keyed-0.0.8 lib/acts_as_keyed.rb
acts_as_keyed-0.0.7 lib/acts_as_keyed.rb