Module: Parliament::Utils

Defined in:
lib/parliament/utils.rb

Class Method Summary collapse

Class Method Details

.reverse_sort_by(args) ⇒ Object


23
24
25
# File 'lib/parliament/utils.rb', line 23

def self.reverse_sort_by(args)
  Parliament::Utils.sort_by(args).reverse!
end

.sort_by(args) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/parliament/utils.rb', line 3

def self.sort_by(args)
  rejected = []
  args = sort_defaults.merge(args)
  list = args[:list].dup
  parameters = args[:parameters]

  list.delete_if do |object|
    rejected << object unless parameters.all? { |param| !object.send(param).nil? if object.respond_to?(param) }
  end

  list.sort_by! do |object|
    parameters.map do |param|
      object.send(param).is_a?(String) ? I18n.transliterate(object.send(param)).downcase : object.send(param)
    end
  end

  # Any rejected (nil) values will be added to the start of the result unless specified otherwise
  args[:prepend_rejected] ? rejected.concat(list) : list.concat(rejected)
end

.sort_defaultsObject


29
30
31
32
33
# File 'lib/parliament/utils.rb', line 29

def self.sort_defaults
  {
    prepend_rejected: true
  }
end