debian-mirror-gitlab/app/finders/bulk_imports/entities_finder.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
1 KiB
Ruby
Raw Normal View History

2021-09-30 23:02:18 +05:30
# frozen_string_literal: true
module BulkImports
class EntitiesFinder
2022-06-21 17:19:12 +05:30
def initialize(user:, bulk_import: nil, params: {})
2021-09-30 23:02:18 +05:30
@user = user
@bulk_import = bulk_import
2022-06-21 17:19:12 +05:30
@params = params
2021-09-30 23:02:18 +05:30
end
def execute
::BulkImports::Entity
.preload(:failures) # rubocop: disable CodeReuse/ActiveRecord
.by_user_id(user.id)
2023-04-23 21:23:45 +05:30
.then { |entities| filter_by_bulk_import(entities) }
.then { |entities| filter_by_status(entities) }
.then { |entities| sort(entities) }
2021-09-30 23:02:18 +05:30
end
private
attr_reader :user, :bulk_import, :status
def filter_by_bulk_import(entities)
return entities unless bulk_import
2022-06-21 17:19:12 +05:30
entities.by_bulk_import_id(bulk_import.id)
2021-09-30 23:02:18 +05:30
end
def filter_by_status(entities)
2022-06-21 17:19:12 +05:30
return entities unless ::BulkImports::Entity.all_human_statuses.include?(@params[:status])
2021-09-30 23:02:18 +05:30
2022-06-21 17:19:12 +05:30
entities.with_status(@params[:status])
end
def sort(entities)
return entities unless @params[:sort]
entities.order_by_created_at(@params[:sort])
2021-09-30 23:02:18 +05:30
end
end
end