Class: Katalyst::Tables::Collection::Sorting::Sort

Inherits:
Object
  • Object
show all
Includes:
Backend
Defined in:
app/models/concerns/katalyst/tables/collection/sorting.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Backend

#default_summary_table_component, #default_table_component, #default_table_pagination_component, #default_table_query_component

Constructor Details

#initialize(app) ⇒ Sort

Returns a new instance of Sort.



114
115
116
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 114

def initialize(app)
  @app = app
end

Instance Method Details

#call(collection) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 120

def call(collection)
  collection = @app.call(collection)

  column, direction = collection.sort.to_h.values_at(:column, :direction)

  return collection if column.nil?

  if collection.items.respond_to?(:"order_by_#{column}")
    collection.items = collection.items.reorder(nil).public_send(:"order_by_#{column}", direction.to_sym)
  elsif collection.model.has_attribute?(column)
    collection.items = collection.items.reorder(column => direction)
  end

  collection
end