orm_adapter
orm_adapter copied to clipboard
get implementation for ActiveRecord
Hey folks, I'm making use of encrypted_id which overrides ActiveRecord's find
method in order to handle decryption of an obfuscated id. I'm also using devise which makes use of orm_adapter to interact with ActiveRecord. At present devise is using the get
method which is implemented using where/first vs a find and thus encrypted_id override doesn't work.
In order to consolidate on overriding find I have implemented a new version of the get
method below.
# lib/orm_adapters/adapters/active_record.rb
# @see OrmAdapter::Base#get
def get(id)
# Ignore exceptions, be consistent on use of find for get and get!
begin
klass.find(wrap_key(id))
rescue
nil
end
# OR use dynamic finder which will return nil on not match, easier to subclass than a where
klass.find_by_id(wrap_key(id))
# Previous implementation with where and first
# klass.where(klass.primary_key => wrap_key(id)).first
end
Any thoughts on why this is a bad idea or maybe a better way that it could be done? If I opened a PR is this something that orm_adapter would be willing to accept?
Thanks for the ideas / feedback.
Yes, I think that this is a good idea. @gudata had a similar PR (referenced above).