lib/hashid/rails.rb in hashid-rails-0.7.0 vs lib/hashid/rails.rb in hashid-rails-1.0.0

- old
+ new

@@ -3,10 +3,18 @@ require "hashids" require "active_record" module Hashid module Rails + # Arbitrary value to verify hashid + # https://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#On_the_Internet_and_in_software + HASHID_TOKEN = 42 + + def self.included(base) + base.extend ClassMethods + end + # Get configuration or load defaults def self.configuration @configuration ||= Configuration.new end @@ -18,77 +26,70 @@ # Reset gem configuration to defaults def self.reset @configuration = Configuration.new end - def self.included(base) - base.extend ClassMethods - end - - def encoded_id + def hashid self.class.encode_id(id) end + alias to_param hashid - 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 + ids.map { |id| hashid_decode(id) } else hashid_decode(ids) end end - def find(hashid) - if model_reload? || Hashid::Rails.configuration.disable_find - super(hashid) + 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::Rails.configuration.override_find + super(decode_id(uniq_ids)) else - super(decode_id(hashid) || hashid) + super end end def find_by_hashid(hashid) - find_by!(id: hashid_decode(hashid)) + find_by(id: decode_id(hashid)) end + def find_by_hashid!(hashid) + find_by!(id: decode_id(hashid)) + end + private - def model_reload? - caller.any? { |s| s =~ %r{ active_record/persistence.*reload } } + def hashids + Hashids.new(*Hashid::Rails.configuration.for_table(table_name)) end + def hashid_encode(id) + hashids.encode(HASHID_TOKEN, id) + end + def hashid_decode(id) - hashids.decode(id.to_s).first + decoded_hashid = hashids.decode(id.to_s) + return id unless valid_hashid?(decoded_hashid) + decoded_hashid.last end - def hashid_encode(id) - hashids.encode(id) + def valid_hashid?(decoded_hashid) + decoded_hashid.size == 2 && decoded_hashid.first == HASHID_TOKEN end end end end - -ActiveRecord::Base.send :include, Hashid::Rails