lundi 7 décembre 2015

managing parallel models in ruby

I'm working on a rails project that relies on both business logic in the standard ActiveRecord::Base and a lookup from my oauth source (which forms the entire account creation process). As I have implemented it currently I extract certain immediately relevant information for my postgres database and store the rest of my oauth object in a postgres text field (oauth_hash) by calling to_json on it- it still gets refreshed somewhat regularly but this way I don't need to call my oauth service every time i need that hash (this serves no specific oauth-related purpose though.

i then overwrite my oauth_hash getter with

def oauth_hash
  self[:oauth_hash] ? JSON.parse(self[:oauth_hash]) : nil
end

to seamlessly return the hash when I need it- actually passing it back to the oauth class itself to make a new object to work with. Needless to say testing this has been a mess at times, with lots of code spent stubbing out these methods and duplicating fixtures, mocking the oauth itself and stubbing out the oauth objects at various stages.

  1. I'm sorry if this is a confusing description and will edit for clarity if asked.
  2. Is there a better way to do this? I feel like I'm dangerously close to having two parallel models, but i need my service's oauth gem to make certain methods available and want to avoid having to call it every time I load a new page with information specific to my service.

Aucun commentaire:

Enregistrer un commentaire