Learn more about our current job openings and benefits of working at FSL.
Detailed reviews and feedback from past and current clients.
Get to know the Management Team behind FullStack Labs.
Our step-by-step process for designing and developing new applications.
Writings from our team on technology, design, and business.
Get answers to the questions most frequently asked by new clients.
Learn about our company culture and defining principles.
A high level overview of FullStack Labs, who we are, and what we do.
A JavaScript framework that allows rapid development of native Android and IOS apps.
A JavaScript framework maintained by Facebook that's ideal for building complex, modern user interfaces within single page web apps.
A server side programming language known for its ease of use and speed of development.
A lightweight and efficient backend javascript framework for web apps.
An interpreted high-level programming language great for general purpose programming.
A JavaScript framework maintained by Google that addresses many of the challenges encountered when building single-page apps.
A JavaScript framework that allows developers to build large, complex, scalable single-page web applications.
A progressive JavaScript framework known for its approachability, versatility, and performance.
A progressive JavaScript framework known for its approachability, versatility, and performance.
A progressive JavaScript framework known for its approachability, versatility, and performance.
A progressive JavaScript framework known for its approachability, versatility, and performance.
A progressive JavaScript framework known for its approachability, versatility, and performance.
A progressive JavaScript framework known for its approachability, versatility, and performance.
A progressive JavaScript framework known for its approachability, versatility, and performance.
View a sampling of our work implemented using a variety of our favorite technologies.
View examples of the process we use to build custom software solutions for our clients.
View projects implemented using this javascript framework ideal for building complex, modern user interfaces within single page web apps.
View projects implemented using this framework that allows rapid development of native Android and IOS apps.
View projects implemented using this backend javascript framework for web apps.
View projects implemented using this high-level programming language great for general purpose programming.
View projects implemented using this server side programming language known for its ease of use and speed of development.
We have vast experience crafting healthcare software development solutions, including UI/UX Design, Application Development, Legacy Healthcare Systems, and Team Augmentation. Our development services help the healthcare industry by enhancing accessibility, productivity, portability, and scalability.
We offer a range of custom software development solutions for education companies of all sizes. We're experts in Education Software Development and specialists in enhancing the learning experience across web, mobile, and conversational UI.
We're experts in developing Custom Software Solutions for the Logistics Industry. Our work offered a whole new and more efficient way for Logistics companies to manage their crucial operations.
We partner with various construction industry organizations to build custom software development solutions. Our Construction Software Development Services allow construction companies to manage projects, resources, and documentation.
We have vast experience crafting healthcare software development solutions, including UI/UX Design, Application Development, Legacy Healthcare Systems, and Team Augmentation. Our development services help the healthcare industry by enhancing accessibility, productivity, portability, and scalability.
We offer a range of custom software development solutions for education companies of all sizes. We're experts in Education Software Development and specialists in enhancing the learning experience across web, mobile, and conversational UI.
We're experts in developing Custom Software Solutions for the Logistics Industry. Our work offered a whole new and more efficient way for Logistics companies to manage their crucial operations.
We partner with various construction industry organizations to build custom software development solutions. Our Construction Software Development Services allow construction companies to manage projects, resources, and documentation.
Learn more about our current job openings and benefits of working at FSL.
Detailed reviews and feedback from past and current clients.
Get to know the Management Team behind FullStack Labs.
Our step-by-step process for designing and developing new applications.
Writings from our team on technology, design, and business.
Get answers to the questions most frequently asked by new clients.
Learn about our company culture and defining principles.
A high level overview of FullStack Labs, who we are, and what we do.
A JavaScript framework that allows rapid development of native Android and IOS apps.
A JavaScript framework maintained by Facebook that's ideal for building complex, modern user interfaces within single page web apps.
A server side programming language known for its ease of use and speed of development.
A lightweight and efficient backend javascript framework for web apps.
An interpreted high-level programming language great for general purpose programming.
A JavaScript framework maintained by Google that addresses many of the challenges encountered when building single-page apps.
A JavaScript framework that allows developers to build large, complex, scalable single-page web applications.
A progressive JavaScript framework known for its approachability, versatility, and performance.
A dynamic programming language used in all sorts of web and mobile applications.
A cross-platform programming language designed to run robust applications on any device.
A UI toolkit used to build natively compiled applications from a single codebase.
A functional programming language that’s ideal for scalability, maintainability, and reliability.
A Customer Relationship Management (CRM) platform that seamlessly integrates with your business operations.
A high-performance programming language that makes it easy to build simple, reliable, and efficient software.
View a sampling of our work implemented using a variety of our favorite technologies.
View examples of the process we use to build custom software solutions for our clients.
View projects implemented using this javascript framework ideal for building complex, modern user interfaces within single page web apps.
View projects implemented using this framework that allows rapid development of native Android and IOS apps.
View projects implemented using this backend javascript framework for web apps.
View projects implemented using this high-level programming language great for general purpose programming.
View projects implemented using this server side programming language known for its ease of use and speed of development.
In this blog post, I will show you a brief introduction on how to use the Interactor Ruby gem within your Rails application to easily encapsulate your business logic, along with my opinions on why I consider Interactors a good fit for any Rails application.
Interactor is a Ruby gem whose main purpose is to help development teams encapsulate their business logic.
Technically, all interactors start as a PORO (Plain Old Ruby Object) which will represent one thing that our application does. Does that ring a bell? Have you heard of Service objects?
We’ll see how to give the “Interactor” behavior to our POROs or Service objects later in this post.
All you need to start using the Interactor gem is to include it in your Gemfile.
Then, as usual, you need to complete the installation by executing:
That’s it. You are now ready to start using Interactors in your Rails app!
At this point, you have two different options to start creating your own interactors. Let’s start with the easiest one.
The gem comes with a handy generator that allows you to create the interactor class with a simple command:
You will get the following file:
This approach is helpful if you are working in an app with an early development stage (fresh Rails app), and if you have not yet defined how to encapsulate business logic.
As you might imagine from the previous example, any Ruby object can become an interactor. All you need to do is to include the module named `Interactor` in your class and implement or define a method named `call`.
That’s it. You have transformed your PORO into an Interactor.
This approach is useful if you are trying to move from Service objects to an Interactors approach. That way your Service objects can become Interactors.
Before we go deeper with Interactors’ functionality, let’s talk about this for a bit. Generally, the Service Objects approach is much better than doing everything in the Controllers.
I even prefer to use Service Objects over putting the heavy load of business logic in the Models. Some developers are comfortable with it because, as they tend to say, that’s what the Models are for. And yes, Models give us some tools to handle complex scenarios, e.g., Validations, Callbacks, Associations, Scopes, etc.
But have you heard of “The Callbacks Hell”? Well, I have seen it with my own eyes, and it’s a place that we should avoid whenever possible.
I prefer to keep the responsibility of my Models small enough i.e Associations, Simple Scopes, and Small Validations. For everything else, you can add new layers, like Presenters for the views or Service Objects/Interactors for business logic.
Why do I recommend Interactors over Service Objects? Interactors provide a standard way of performing actions within your application. The API they offer is really simple and small enough so you don’t have to deal with hundreds of methods at a time.
Service objects are POROs and in the end, we can write them as we want. You will be noticing soon that every developer in your team can come up with a different type or style of Service Object unless you define a guideline from the beginning.
With Interactors you can avoid that; there’s only one way to write them and they all expose the same API. That’s better for your codebase as it will look cleaner.
Let me guide you through the Interactor’s core concepts with a basic example.
Let’s imagine we are working on an Internal Book Review app. Books are reviewed by users before they get published. The book’s author needs to be notified after a review is posted by a reviewer. If the review score is perfect we need to notify the Administrative team about it. When the review score is pretty low, we need to send a different notification. Finally, we need to calculate and refresh the average of the scores received by the book. Here is the controller that does all of that:
Now the code is shorter and clearer. It is a good practice to name our interactors with a descriptive name of the action that is being performed. Try to name them thinking on your business needs and not on the technical implementation.
All an Interactor object needs to do is to implement a method named `call` which will receive only the necessary context from the controller to do its job, not a single bit more.
If the result of the interaction is successful then we can redirect or render whatever we need. We can also handle the scenario whenever the Interactor fails to succeed.
Let me show you the Interactor code:
With this example, you will notice that everything we passed to the `call` method from the controller is accessible and being used as a property in `context`. After the interactor does its job, the context will reflect all changes made by us and they will be available to us in the controller.
The only way this interactor can fail at this point is when we manually call the `context.fail!` method. Here in our example, it happens within the `create_review` method. And it means that the review was not saved in the database because of a validation error.
After the `context.fail!` method is called within an interactor, the execution is halted and returned early to the place the interactor was called (in this case, the controller). In our example, if the review can’t be persisted, then we won’t get any notifications, nor will the book stats be updated.
You might be thinking, Hey, we just added more code in a different place, and the controller was really simple the way it was before. And you are right, but in the real world, business logic can get more and more complex sooner than later.
Requirements can change at any moment, and at this point, we have already encapsulated the Review creation process in a single place. What if in the future we are asked to import reviews from a CSV file? Or to create reviews from an external API call or webhook? We could easily reuse our `CreateReview` interactor in those scenarios. We might only need small tweaks to support them.
That’s the main advantage of encapsulating business logic within Interactors.
Defining a standard way of encapsulating business logic in your Rails application is a win-win. Whether you use Service Objects, Interactors, or some other methodology, the end goal is to keep your code clean, well-designed, and easy to understand for the newest members of the development team.
If we use Interactors the right way, we will fulfill what’s stated by the following acronyms:
Obtaining all of that for the small price of adding a new layer/dependency is totally worth it.
Here at FullStack Labs, we help businesses thrive and reach their goals. If you’re looking for Ruby on Rails consultancy services, do not hesitate to contact us.
We’d love to learn more about your project.
Engagements start at $75,000.