Sha256: ba6aab67895742ebc8693e73ec16ad1b12417aca760743f8646be1db72145d33

Contents?: true

Size: 674 Bytes

Versions: 1

Compression:

Stored size: 674 Bytes

Contents

require 'hashid/rails/version'
require 'hashids'
require 'active_record'

module Hashid
  module Rails
    def self.included(base)
      base.extend ClassMethods
    end

    def encoded_id
      self.class.encode_id(id)
    end

    def to_param
      encoded_id
    end
    alias_method :hashid, :to_param

    module ClassMethods
      def hashids
        Hashids.new(table_name, 6)
      end

      def encode_id(id)
        hashids.encode(id)
      end

      def decode_id(id)
        hashids.decode(id.to_s).first
      end

      def find(hashid)
        super decode_id(hashid) || hashid
      end
    end
  end
end

ActiveRecord::Base.send :include, Hashid::Rails

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hashid-rails-0.1.2 lib/hashid/rails.rb