lib/hashid/rails.rb in hashid-rails-1.2.2 vs lib/hashid/rails.rb in hashid-rails-1.3.0
- old
+ new
@@ -34,10 +34,27 @@
self.class.encode_id(id)
end
alias to_param hashid
module ClassMethods
+ 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
+
+ def hashid_configuration
+ @hashid_configuration || hashid_config
+ end
+
+ def reset_hashid_config
+ @hashid_configuration = nil
+ end
+
def relation
super.tap { |r| r.extend ClassMethods }
end
def has_many(*args, &block)
@@ -69,11 +86,11 @@
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
+ if hashid_configuration.override_find
super(decode_id(uniq_ids, fallback: true))
else
super
end
end
@@ -87,25 +104,25 @@
end
private
def hashids
- Hashids.new(*Hashid::Rails.configuration.for_table(table_name))
+ Hashids.new(*hashid_configuration.to_args)
end
def hashid_encode(id)
- if Hashid::Rails.configuration.sign_hashids
+ if hashid_configuration.sign_hashids
hashids.encode(HASHID_TOKEN, id)
else
hashids.encode(id)
end
end
def hashid_decode(id, fallback:)
fallback_value = fallback ? id : nil
decoded_hashid = hashids.decode(id.to_s)
- if Hashid::Rails.configuration.sign_hashids
+ if hashid_configuration.sign_hashids
valid_hashid?(decoded_hashid) ? decoded_hashid.last : fallback_value
else
decoded_hashid.first || fallback_value
end
rescue Hashids::InputError