Sha256: 81c189b30778eb9d6e2284eb18c1bfa3033dda022468382d442a016648e985b6
Contents?: true
Size: 1.9 KB
Versions: 1
Compression:
Stored size: 1.9 KB
Contents
require "hashid/rails/version" require "hashid/rails/configuration" 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 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) decoded_ids = ids.map { |id| hashid_decode(id) } decoded_ids.any? ? decoded_ids : nil 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 =~ %r{ 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 end end ActiveRecord::Base.send :include, Hashid::Rails
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hashid-rails-0.6.0 | lib/hashid/rails.rb |