Module: Trailblazer::Finder::Helpers::Sorting

Defined in:
lib/trailblazer/finder/helpers/sorting.rb

Instance Method Summary collapse

Instance Method Details

#new_sort_params_for(attribute) ⇒ Object



51
52
53
# File 'lib/trailblazer/finder/helpers/sorting.rb', line 51

def new_sort_params_for(attribute)
  params.merge! sort: "#{attribute} #{sort_direction_for(attribute)}"
end

#remove_sort_params_for(attribute) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/trailblazer/finder/helpers/sorting.rb', line 43

def remove_sort_params_for(attribute)
  return unless sorting.include?(attribute.to_s)

  sort = sorting.gsub(/#{attribute} #{sort_direction_for(attribute)}/, "").split(",")
  sort.delete_if(&:blank?)
  params.merge! sort: sort.join(",")
end

#reverse_sort_direction_for(attribute) ⇒ Object



24
25
26
27
28
# File 'lib/trailblazer/finder/helpers/sorting.rb', line 24

def reverse_sort_direction_for(attribute)
  return "desc" if (!sorting.nil? && sorting.include?("#{attribute} asc")) || @find.config[:sorting][attribute.to_sym] == :asc

  "asc"
end

#sort?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/trailblazer/finder/helpers/sorting.rb', line 14

def sort?(attribute)
  sorting.include?(attribute.to_s)
end

#sort_direction_for(attribute) ⇒ Object



18
19
20
21
22
# File 'lib/trailblazer/finder/helpers/sorting.rb', line 18

def sort_direction_for(attribute)
  return "asc" if (!sorting.nil? && sorting.include?("#{attribute} asc")) || @find.config[:sorting][attribute.to_sym] == :asc

  "desc"
end

#sort_params_for(attribute) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trailblazer/finder/helpers/sorting.rb', line 30

def sort_params_for(attribute)
  if sorting.nil?
    params.merge! sort: "#{attribute} #{sort_direction_for(attribute)}"
  elsif sorting.include?(attribute.to_s)
    params.merge! sort: sorting.gsub(
      /#{attribute} #{sort_direction_for(attribute)}/,
      "#{attribute} #{reverse_sort_direction_for(attribute)}"
    )
  else
    params.merge! sort: "#{sorting}, #{attribute} #{sort_direction_for(attribute)}"
  end
end

#sortingObject



7
8
9
10
11
12
# File 'lib/trailblazer/finder/helpers/sorting.rb', line 7

def sorting
  return if @errors.any?
  return if @find.sorting.empty?

  @sorting ||= Utils::Hash.remove_keys_from_hash(@find.sorting, [:handler]).map { |r| r.join(" ") }.join(", ")
end