In our previous post, The Basics of MVC in Rails, we discussed theoretical aspects of the MVC design pattern. We defined what MVC stands for, identified what each MVC component is responsible for, addressed what happens when a component contains redundant logic, and, most importantly, we introduced the concept of refactoring. In this article, as promised, we'll show you each design pattern at work.
Service Objects (and Interactor Objects)
Service Objects are created when an action:
- is complex (such as calculating an employee's salary)
- uses APIs of external services
- clearly doesn't belong to one model (for example, deleting outdated data)
- uses several models (for example, importing data from one file to several models)
Example
In the example below, work is performed by the external Stripe service. The Stripe service creates a Stripe customer based on an email address and a source (token), and ties any service payment to this account.
Continue reading %7 Design Patterns to Refactor MVC Components in Rails%
by Viktoria Kotsurenko via SitePoint