Sha256: b8c06c95e8e7380d0de3153add0885eeebb233e9511eb8b15dd95d660b982e62

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

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

module Hashid
  module Rails

    # Get configuration or load defaults
    def self.configuration
      @configuration ||= Configuration.new
    end

    # Set configuration settings with a block
    def self.configure
      yield(configuration)
    end

    # Reset gem configuration to defaults
    def self.reset
      @configuration = Configuration.new
    end

    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
        secret = Hashid::Rails.configuration.secret
        length = Hashid::Rails.configuration.length
        alphabet = Hashid::Rails.configuration.alphabet

        arguments = ["#{table_name}#{secret}", length]
        arguments << alphabet if alphabet.present?

        Hashids.new(*arguments)
      end

      def encode_id(ids)
        if ids.is_a?(Array)
          ids.map { |id| hashid_encode(id) }
        else
          hashid_encode(ids)
        end
      end

      def decode_id(ids)
        if ids.is_a?(Array)
          ids.map { |id| hashid_decode(id) }
        else
          hashid_decode(ids)
        end
      end

      def find(hashid)
        model_reload? ? super(hashid) : super( decode_id(hashid) || hashid )
      end

      def find_by_hashid(hashid)
        find_by!(id: hashid_decode(hashid))
      end

      private

      def model_reload?
        caller.any? {|s| s =~ /active_record\/persistence.*reload/}
      end

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

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

    class Configuration
      attr_accessor :secret, :length, :alphabet

      def initialize
        @secret = ''
        @length = 6
        @alphabet = nil
      end
    end

  end
end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hashid-rails-0.5.0 lib/hashid/rails.rb
hashid-rails-0.4.1 lib/hashid/rails.rb
hashid-rails-0.4.0 lib/hashid/rails.rb