Sha256: 3296918a23874c20d1d2fcf0b5050de08e4b3bb595cb2a7072a1a8f67d59948d
Contents?: true
Size: 1.57 KB
Versions: 2
Compression:
Stored size: 1.57 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(id) hashids.encode(id) end def decode_id(id) hashids.decode(id.to_s).first end def find(hashid) model_reload? ? super(hashid) : super( decode_id(hashid) || hashid ) end private def model_reload? caller.any? {|s| s =~ /active_record\/persistence.*reload/} 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
hashid-rails-0.3.1 | lib/hashid/rails.rb |
hashid-rails-0.3.0 | lib/hashid/rails.rb |