Design patterns in Ruby on Rails Flashcards
What is a Service Object?
A Service Object is PORO – Plain Old Ruby Object, which is meant to encapsulate business logic and complex calculations into manageable classes and methods. This is one of the most used design patterns in ruby on rails.
When to use service objects?
When we need to perform complex calculations or business logic, e.g., if we need to calculate employees’ salaries based on their attendance.
When we need to implement any other API, we need to implement a payment gateway such as Stripe.
When we want to import CSV that contains the bulk of data. 4. When we need to clear garbage/unused/old data from the database efficiently, it won’t affect the existing data.
What is the purpose of View Objects (Presenter) in Rails?
View Objects encapsulate all view-related logic to keep models and views clean and separate. They help in making complex view logic more testable and maintainable.
What is a Query Object in Rails?
A Query Object is a design pattern that extracts query logic from controllers and models into reusable classes, making code more modular and easier to test.
What are Decorators in Rails?
Decorators are a design pattern that allows dynamic addition of behavior to objects without affecting other objects of the same class. They help clean up logic in views and controllers.
What is a Form Object in Rails?
A Form Object encapsulates validation and persistence logic into a single class, making it reusable and keeping models and controllers clean. It separates form-specific logic from models.
What is a Value Object in Rails?
A Value Object represents a simple entity that contains values but not unique identifiers. It is used for small, immutable objects that encapsulate specific logic or attributes, like extracting the username and domain from an email.
What is a Policy Object in Rails?
A Policy Object is responsible for read operations, often used for authorization. It is similar to a Service Object, which handles write operations. Policy Objects return boolean values based on conditions.
What is the Builder pattern in Rails?
The Builder pattern is used to construct complex objects step by step, allowing the creation of different representations and simplifying the object instantiation process. It is useful when creating objects with many permutations.
When should you use the Interactor pattern in Rails?
Use the Interactor pattern to break down large tasks into smaller, inter-dependent steps. It is helpful when dealing with complex processes or integrating external APIs.
What is the Observer pattern in Rails?
The Observer pattern allows an object (the observed) to notify other objects (observers) about state changes. It is useful when multiple views depend on a single object’s state or when an object’s state affects other objects.
How does the Service Object pattern improve controller code?
The Service Object pattern moves complex business logic out of controllers, making controllers “skinny” and more readable by encapsulating logic in separate, manageable classes.
How do View Objects (Presenters) improve code readability in Rails?
View Objects separate complex view-related logic from views and models, making the code more modular, easier to test, and maintain. They encapsulate logic that would otherwise clutter the view.
Give an example of when to use a Query Object.
Use a Query Object when you need to fetch a list of records with specific conditions that are reusable across different parts of the application, such as filtering posts by type and view count.
What problem do Decorators solve in Rails?
Decorators add behavior to objects dynamically without changing the objects themselves, helping clean up logic that would otherwise be in the view or controller.