Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get implementation for ActiveRecord #33

Open
krsyoung opened this issue Feb 17, 2014 · 1 comment
Open

get implementation for ActiveRecord #33

krsyoung opened this issue Feb 17, 2014 · 1 comment

Comments

@krsyoung
Copy link

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.

@ianwhite
Copy link
Owner

ianwhite commented Aug 5, 2014

Yes, I think that this is a good idea. @gudata had a similar PR (referenced above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants