Service Objects in Rails (Refactoring)

"Fat models, skinny controllers" has been the design style for Rails app development. However, with time this style has been outdated as with code growth the models become too fat to handle.

Why not use concerns?

ActiveSupport concern can be used to extract the fat code out of models and share in multiple models. However ActiveSupport concerns are not useful in much scenarios as we end up including these in our models thus increasing burden.

In such cases we use a special purpose classes called 'service objects'. So in this tutorial we will look how these classes are used.

Service objects:

As SRP (single responsibility principle) focuses on a single responsibility for methods and classes. If our model is handling too many responsibilities, we can extract these out in special classes which focuses on particular bit of business logic each class. Suppose we have some e-commerce website. Where we receive order details from customer, we do some validation and construct some formatted data before sending it to payment gateway. We put this logic in our controller or model which is not directly related to the model. In such scenario we can extract this functionality out and abstract in some special class that only focuses on the processing details. These special classes are called service objects.

Service objects help in organizing the code in a structured way. It makes it easy to understand and debug the code. Also it is easy to test multiple small functionalities than one large functionality.

We keep service objects under app/service folder. The name should explain the functionality of service object. So our service object files look like this in our app:

Lets take an example of registration process where a user registers to the application. After creating user a welcome email is sent to user. This functionality can be extracted out in a service as below:

In above code we extracted the mailer functionality from controller in a service object class. The class is concerned about the mailing functionality only. This is just a simple example, we can abstract the large code in multiple classes.

Lets see little complex example. In one of my projects i implemented layer chatting functionality. Layer is a third party api provider. It gives chatting facility. It store all chat data on their server and exposed apis to retrieve chats. I created a super class Conversation:

0 Comment(s)

Comment on it

AttachFiles,Zip etc

RecordFrom Desktop

RecordFrom Webcam

Preview

Enter captcha to proceed

Unable to start Java!! Mr. Nerd figure out why...

We have detected you are using Google Chrome and might be unable to use the Java plugin from this browser. Starting with Version 42 (released April 2015), Chrome has disabled the standard way in which browsers support plugins. More info

Chrome Version Support

Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).

Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.

Chrome Version Support

Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).

Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.

Chrome Version Support

Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).

Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.

Unable to start Java!! Mr. Nerd figure out why..

We have detected that Java plugin is not installed/enabled on your browser. Unfortunately some of our below listed tools require Java plugin:

Desktop recorder.

Error Screen capture.

Time Tracker.

To use our product please click on the link to install/Detect Java plugin.

You have exceeded the maximum number of characters allowed for a comment without sign in.Either enter Content Or Record

Your Post will be visible in the listing once it has been approved by the administrator.