Design patterns in Ruby on Rails Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a Service Object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When to use service objects?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of View Objects (Presenter) in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Query Object in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Decorators in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Form Object in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Value Object in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Policy Object in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Builder pattern in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should you use the Interactor pattern in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Observer pattern in Rails?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does the Service Object pattern improve controller code?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do View Objects (Presenters) improve code readability in Rails?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Give an example of when to use a Query Object.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What problem do Decorators solve in Rails?

A

Decorators add behavior to objects dynamically without changing the objects themselves, helping clean up logic that would otherwise be in the view or controller.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why use a Form Object instead of adding validation directly to the model?

A

A Form Object separates form-specific validation and persistence logic from the model, making the code more reusable and keeping the model clean from form-specific concerns.

17
Q

What makes a Value Object different from a typical object in Rails?

A

A Value Object represents a simple, immutable entity without unique identifiers, focusing on value rather than identity, and encapsulates specific logic related to the value.

18
Q

Describe a scenario where a Policy Object is more suitable than using a gem like CanCan or Pundit.

A

A Policy Object is more suitable for applications with high complexity in authorization rules, where customized, granular read permissions are required beyond the capabilities of standard authorization gems.