summaryrefslogtreecommitdiff
path: root/app/finders/branches_finder.rb
blob: 970efa79dfb78107b50d26acda5337dec5108de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

class BranchesFinder
  def initialize(repository, params = {})
    @repository = repository
    @params = params
  end

  def execute
    branches = @repository.branches_sorted_by(sort)
    filter_by_name(branches)
  end

  private

  attr_reader :repository, :params

  def search
    @params[:search].presence
  end

  def sort
    @params[:sort].presence || 'name'
  end

  def filter_by_name(branches)
    if search
      branches.select { |branch| branch.name.upcase.include?(search.upcase) }
    else
      branches
    end
  end
end