lib/hashid/rails.rb in hashid-rails-0.3.1 vs lib/hashid/rails.rb in hashid-rails-0.3.2

- old
+ new

@@ -44,25 +44,46 @@ arguments << alphabet if alphabet.present? Hashids.new(*arguments) end - def encode_id(id) - hashids.encode(id) + def encode_id(ids) + if ids.is_a?(Array) + ids.map { |id| hashid_encode(id) } + else + hashid_encode(ids) + end end - def decode_id(id) - hashids.decode(id.to_s).first + 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 + # Calls `find` with decoded hashid + 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