summaryrefslogtreecommitdiff
path: root/app/finders/branches_finder.rb
blob: 533076585c05c0a9d97bc928e9bd4dbddeda1172 (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.include?(search) }
    else
      branches
    end
  end
end