RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

In a Rails app, what is the Rails Way to organize non-model utility classes?

I'm abstracting the payment gateway I'm using out a a Subscription class and into a PaymentGatewayApi class so that it can be easily changed in the future without touching models. My APIs are located in app/api which appears to be just fine as I only have a few of them (although I'll move them if someone smarter than me says I should).

In trying to write quality code and reducing my parameter calls, I need to make some simple utility classes. One of these is a CreditCardCustomer that can be instantiated in the Subscription model and then have the instance passed to the PaymentGatewayApi.

My question is, where should I put this class? Does it need to be in its own file, or can I put multiple classes in the same file? What is the best practice both for Ruby in general and Rails in particular?

Upvotes: 2

Views: 332

Answers (2)

Jim Stewart
Jim Stewart

Reputation: 17343

CreditCardCustomer sounds like a model to me. Even if it's not an ActiveRecord model, it belongs in app/models if it's a business/domain object. If it really is a utility class, then by all means put it in lib.

Upvotes: 0

fotanus
fotanus

Reputation: 20116

My question is, where should I put this class?

Normally these classes lives in \lib. However, if you are planning to use these classes across your projects, you should consider create a gem for it (rails 3 have some nice support for that), and maybe release it as open source?

Does it need to be in its own file, or can I put multiple classes in the same file?

It is usual to have one class per file, but Ruby is not Java, so follow your heart. Try not put two big classes in the same file, for instance. You might want to look at this answer

Upvotes: 2

Related Questions