Class: Rails::Surrender::SortBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/surrender/helpers/sort_builder.rb

Overview

apply sort directives to the given resource, based on the given sort controls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, sort:) ⇒ SortBuilder

Returns a new instance of SortBuilder.



9
10
11
12
# File 'lib/rails/surrender/helpers/sort_builder.rb', line 9

def initialize(resource:, sort:)
  @resource = resource
  @sort = sort
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



7
8
9
# File 'lib/rails/surrender/helpers/sort_builder.rb', line 7

def resource
  @resource
end

#sortObject (readonly)

Returns the value of attribute sort.



7
8
9
# File 'lib/rails/surrender/helpers/sort_builder.rb', line 7

def sort
  @sort
end

Instance Method Details

#build!Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rails/surrender/helpers/sort_builder.rb', line 14

def build!
  return resource unless resource.is_a? ActiveRecord::Relation

  return resource_attribute_order if resource_has_attribute?

  return scope_method_order if resource_has_scope_method?

  return association_attribute_order if resource_has_association_with_attribute?

  raise Error, I18n.t('surrender.error.query_string.sort.invalid_column', param: sort.request)
end