Class: OpenAPIRest::Operations::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_rest/operations/sort.rb

Overview

Rest sort operation

Instance Method Summary collapse

Constructor Details

#initialize(query_builder) ⇒ Sort

Returns a new instance of Sort.



7
8
9
# File 'lib/openapi_rest/operations/sort.rb', line 7

def initialize(query_builder)
  @query_builder = query_builder
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/openapi_rest/operations/sort.rb', line 11

def execute
  return if @query_builder.single? || !@query_builder.sort.present?

  sorts = @query_builder.sort.split(',')
  order = sorts.map do |s|
    if URI.encode_www_form_component(s)[0] == '+'
      "#{s[1..s.length]} ASC"
    else
      "#{s[1..s.length]} DESC"
    end
  end.join(',')

  @query_builder.api_model.model = @query_builder.api_model.model.order(order)
end