module Hashid::Rails::ClassMethods
Public Instance Methods
decode_id(ids, fallback: false)
click to toggle source
@param ids [String, Integer, Array<Integer, String>] id(s) to decode. @param fallback [Boolean] indicates whether to return the passed in
id(s) if unable to decode or if already decoded.
# File lib/hashid/rails.rb, line 80 def decode_id(ids, fallback: false) if ids.is_a?(Array) ids.map { |id| hashid_decode(id, fallback: fallback) } else hashid_decode(ids, fallback: fallback) end end
encode_id(ids)
click to toggle source
# File lib/hashid/rails.rb, line 69 def encode_id(ids) if ids.is_a?(Array) ids.map { |id| hashid_encode(id) } else hashid_encode(ids) end end
find(*ids)
click to toggle source
Calls superclass method
# File lib/hashid/rails.rb, line 88 def find(*ids) expects_array = ids.first.is_a?(Array) uniq_ids = ids.flatten.compact.uniq uniq_ids = uniq_ids.first unless expects_array || uniq_ids.size > 1 if hashid_configuration.override_find super(decode_id(uniq_ids, fallback: true)) else super end end
find_by_hashid(hashid)
click to toggle source
# File lib/hashid/rails.rb, line 101 def find_by_hashid(hashid) find_by(id: decode_id(hashid, fallback: false)) end
find_by_hashid!(hashid)
click to toggle source
# File lib/hashid/rails.rb, line 105 def find_by_hashid!(hashid) find_by!(id: decode_id(hashid, fallback: false)) end
has_many(*args, &block)
click to toggle source
Calls superclass method
# File lib/hashid/rails.rb, line 63 def has_many(*args, &block) options = args.extract_options! options[:extend] = Array(options[:extend]).push(ClassMethods) super(*args, **options, &block) end
hashid_config(options = {})
click to toggle source
# File lib/hashid/rails.rb, line 42 def hashid_config(options = {}) config = Hashid::Rails.configuration.dup config.pepper = table_name options.each do |attr, value| config.public_send("#{attr}=", value) end @hashid_configuration = config end
hashid_configuration()
click to toggle source
# File lib/hashid/rails.rb, line 51 def hashid_configuration @hashid_configuration || hashid_config end
relation()
click to toggle source
Calls superclass method
# File lib/hashid/rails.rb, line 59 def relation super.tap { |r| r.extend ClassMethods } end
reset_hashid_config()
click to toggle source
# File lib/hashid/rails.rb, line 55 def reset_hashid_config @hashid_configuration = nil end
Private Instance Methods
hashid_decode(id, fallback:)
click to toggle source
# File lib/hashid/rails.rb, line 125 def hashid_decode(id, fallback:) fallback_value = fallback ? id : nil decoded_hashid = hashids.decode(id.to_s) if hashid_configuration.sign_hashids valid_hashid?(decoded_hashid) ? decoded_hashid.last : fallback_value else decoded_hashid.first || fallback_value end rescue Hashids::InputError fallback_value end
hashid_encode(id)
click to toggle source
# File lib/hashid/rails.rb, line 115 def hashid_encode(id) return nil if id.nil? if hashid_configuration.sign_hashids hashids.encode(HASHID_TOKEN, id) else hashids.encode(id) end end
hashids()
click to toggle source
# File lib/hashid/rails.rb, line 111 def hashids Hashids.new(*hashid_configuration.to_args) end
valid_hashid?(decoded_hashid)
click to toggle source
# File lib/hashid/rails.rb, line 138 def valid_hashid?(decoded_hashid) decoded_hashid.size == 2 && decoded_hashid.first == HASHID_TOKEN end