lib/hashid/rails.rb in hashid-rails-1.2.1 vs lib/hashid/rails.rb in hashid-rails-1.2.2
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require "hashid/rails/version"
require "hashid/rails/configuration"
require "hashids"
require "active_record"
@@ -36,11 +38,11 @@
module ClassMethods
def relation
super.tap { |r| r.extend ClassMethods }
end
- def has_many(*args, &block) # rubocop:disable Style/PredicateName
+ def has_many(*args, &block)
options = args.extract_options!
options[:extend] = Array(options[:extend]).push(ClassMethods)
super(*args, options, &block)
end
@@ -97,17 +99,19 @@
hashids.encode(id)
end
end
def hashid_decode(id, fallback:)
- decoded_hashid = hashids.decode(id.to_s)
fallback_value = fallback ? id : nil
+ decoded_hashid = hashids.decode(id.to_s)
if Hashid::Rails.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
def valid_hashid?(decoded_hashid)
decoded_hashid.size == 2 && decoded_hashid.first == HASHID_TOKEN
end