开发者

Where would I place custom classes and how would I access them?

开发者 https://www.devze.com 2022-12-10 06:04 出处:网络
I have the following class that I\'d like access to within certain controllers in my app: class Spreedly

I have the following class that I'd like access to within certain controllers in my app:

class Spreedly
  include HTTParty
  base_uri 'https://example.com/api/1234'
  basic_auth 'user', 'xyz124'
  headers 'Accept' => 'text/xml'
  headers 'Cont开发者_JAVA技巧ent-Type' => 'text/xml'
  format :xml  
end

Where would I put that class so that I could then access it in a controller like Spreedly.post(xyz)?


You can simply place this in a file called spreedly.rb in the lib/ directory. It'll be auto-loaded by rails and will be available for use.


True, but there is nothing stopping you from putting it in your app/models directory.

And to me it seems a better place to put it - because your are accessing data like any other model in your app, and when you reference Spreedly.post etc from your controllers another programmer will naturally (imho) look in the app/models directory first before the /lib ..


Anything that needs to be referenced in multiple places or anything that is large, I put in lib. If it is tiny, and if it is used in a single model, I will put it in the same model file.


I'm very interested in testing this custom classes quickly, just as if you we're testing any other model. One problem I faced by trying to put the file under /lib, is that Spork or Zeus will reboot the environment to run the test.

I love the idea of storing the file under /lib, but this is making my continuos testing process slower. I ended up putting it under app/models.

If there was a way to test a a file under /lib more easily, that'd be great.

0

精彩评论

暂无评论...
验证码 换一张
取 消