问题描述
我们使用 https://github.com/peterhellberg/hashids.rb 在我们的 API 中混淆数据库 ID:
HASHID = hashids.new("this is my salt")
product_id = 12345
hash = HASHID.encode(product_id)
=> "NkK9"
在解码 hashids 时,我们必须这样做:
Product.find_by(id: HASHID.decode(params[:hashid]))
这种模式在我们的应用程序中重复了很多。我可以编写一些像 find_by_hashid
或 where_hashid
这样的辅助函数来处理解码和可能的错误处理。但是当将它们与其他查询方法结合使用时,这很快就会变得脆弱。
所以我想知道,是否可以扩展 ActiveRecord 查询接口以支持特殊的虚拟列 hashid
,以便这样的事情成为可能:
Product.where(hashid: ["Nkk9","69PV"])
Product.where.not(hashid: "69PV")
Product.find_by(hashid: "Nkk9")
Product.find("Nkk9")
# store has_many :products
store.products.where(hashid: "69PV")
这个想法很简单,只要找到 hashid
键,把它变成 id
并解码给定的 hashid 字符串。出错时,返回 nil
。
但我不确定 ActiveRecord 是否提供了一种方法来做到这一点,而无需大量的猴子修补。
解决方法
您可能可以按如下方式使用此基本选项,但我永远不会推荐它:
module HashIDable
module Findable
def find(*args,&block)
args = args.flatten.map! do |arg|
next arg unless arg.is_a?(String)
decoded = ::HASHID.decode(arg)
::HASHID.encode(decoded.to_i) == arg ? decoded : arg
end
super(*args,&block)
end
end
module Whereable
def where(*args)
args.each do |arg|
if arg.is_a?(Hash) && arg.key?(:hashid)
arg.merge!(id: ::HASHID.decode(arg.delete(:hashid).to_s))
end
end
super(*args)
end
end
end
ActiveRecord::FinderMethods.prepend(HashIDable::Findable)
ActiveRecord::QueryMethods.prepend(HashIDable::Whereable)
您可以将此文件放在“config/initializers”中,看看会发生什么,但这种实现非常幼稚且非常脆弱。
上述情况可能有 101 个地方无法计算,包括但绝对不限于:
MyModel.where("hashid = ?","Nkk9")
MyModel.joins(:other_model).where(other_models: {hashid: "Nkk9"})