2014-02-25 12:15:08 -05:00
|
|
|
# Finders
|
|
|
|
|
2020-10-07 11:08:40 -04: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 12:15:08 -05:00
|
|
|
|
2014-02-25 12:21:53 -05:00
|
|
|
```ruby
|
2020-10-07 11:08:40 -04:00
|
|
|
class Project < ApplicationRecord
|
2014-02-25 12:15:08 -05: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 11:08:40 -04:00
|
|
|
The GitLab approach is to use a Finder:
|
2014-02-25 12:15:08 -05:00
|
|
|
|
2014-02-25 12:21:53 -05:00
|
|
|
```ruby
|
2015-05-25 07:36:28 -04:00
|
|
|
issues = IssuesFinder.new(project, user, filter).execute
|
2014-02-25 12:15:08 -05:00
|
|
|
```
|
|
|
|
|
2020-10-06 20:08:24 -04:00
|
|
|
It will help keep models thinner.
|