Class: Getto::Params::Search::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/getto/params/search.rb

Defined Under Namespace

Classes: Order

Instance Method Summary collapse

Constructor Details

#initialize(sort:) ⇒ Sort

Returns a new instance of Sort.



51
52
53
# File 'lib/getto/params/search.rb', line 51

def initialize(sort:)
  @sort = [sort.split(".")].to_h
end

Instance Method Details

#to_h(sort:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/getto/params/search.rb', line 55

def to_h(sort:)
  spec = {}
  if sort
    sort.call Order.new(spec)
  end

  result = {
    column: nil,
    order: true,
  }

  spec.map{|key,is_straight|
    if sort = @sort[key]
      result[:column] = key.to_sym
      result[:order] =
        if sort == "asc"
          is_straight
        else
          not is_straight
        end
    end
  }

  result
end