Created
May 11, 2017 17:52
-
-
Save leandromarques/86fac39095384cd8d2d9d3a35ffbd9cf to your computer and use it in GitHub Desktop.
ajax-datatables-rails Rails 5.1 fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sort_records(records) | |
sort_by = [] | |
params[:order].to_unsafe_h.each do |item| | |
sort_by << "#{sort_column(item[1]["column"])} #{sort_direction(item[1]["dir"])}" | |
end | |
records.order(sort_by.join(", ")) | |
end | |
def sort_column(item) | |
new_sort_column(item) | |
rescue | |
::AjaxDatatablesRails::Base.deprecated '[DEPRECATED] Using table_name.column_name notation is deprecated. Please refer to: https://github.com/antillas21/ajax-datatables-rails#searchable-and-sortable-columns-syntax' | |
deprecated_sort_column(item) | |
end | |
def sort_direction(item) | |
options = %w(desc asc) | |
options.include?(item) ? item.upcase : 'ASC' | |
end | |
def new_sort_column(item) | |
model, column = sortable_columns[sortable_displayed_columns.index(item.to_i)].split('.') | |
col = [model.constantize.table_name, column].join('.') | |
end | |
def generate_sortable_displayed_columns | |
@sortable_displayed_columns = [] | |
params[:columns].to_unsafe_h.each do |column| | |
@sortable_displayed_columns << column[1]["data"].to_i if column[1]["orderable"] == 'true' | |
end | |
@sortable_displayed_columns | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment