2014-02-25 17:15:08 +00:00
|
|
|
# Finders
|
|
|
|
|
2020-10-07 15:08:40 +00:00
|
|
|
These types of classes are responsible for retrieving collection items based on different conditions.
|
|
|
|
They prevent lookup methods in models like this:
|
|
|
|
|
2014-02-25 17:15:08 +00:00
|
|
|
|
2014-02-25 17:21:53 +00:00
|
|
|
```ruby
|
2020-10-07 15:08:40 +00:00
|
|
|
class Project < ApplicationRecord
|
2014-02-25 17:15:08 +00:00
|
|
|
def issues_for_user_filtered_by(user, filter)
|
|
|
|
# A lot of logic not related to project model itself
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
issues = project.issues_for_user_filtered_by(user, params)
|
|
|
|
```
|
|
|
|
|
2020-10-07 15:08:40 +00:00
|
|
|
The GitLab approach is to use a Finder:
|
2014-02-25 17:15:08 +00:00
|
|
|
|
2014-02-25 17:21:53 +00:00
|
|
|
```ruby
|
2015-05-25 11:36:28 +00:00
|
|
|
issues = IssuesFinder.new(project, user, filter).execute
|
2014-02-25 17:15:08 +00:00
|
|
|
```
|
|
|
|
|
2020-10-07 00:08:24 +00:00
|
|
|
It will help keep models thinner.
|