summaryrefslogtreecommitdiff
path: root/app/finders/branches_finder.rb
blob: 852eac3647d4c41e691f40eaec976cc22f347aaf (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
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