ianwhite / orm_adapter

Provides a single point of entry for using basic features of ruby ORMs
MIT License
194 stars 76 forks source link

get implementation for ActiveRecord #33

Open krsyoung opened 10 years ago

krsyoung commented 10 years ago

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 commented 10 years ago

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