Last active
July 20, 2016 13:21
-
-
Save vincentlkl/9340e24593e3010b51d2596e42854224 to your computer and use it in GitHub Desktop.
Revisions
-
vincentlkl revised this gist
Jul 20, 2016 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,8 @@ class ItemsDatatable delegate :params, :h, to: :@view def initialize(view) @view = view end def as_json(options = {}) -
vincentlkl renamed this gist
Jul 20, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
vincentlkl created this gist
Jul 20, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ class ItemsDatatable delegate :params, :h, to: :@view def initialize(view, user) @view = view @user = user end def as_json(options = {}) { sEcho: params[:sEcho].to_i, iTotalRecords: Item.count, iTotalDisplayRecords: items.total_entries, aaData: data } end private def data items.map do |item| [ item.id, ] end end def items @items ||= fetch_items end def fetch_items items = Item.order("#{sort_column} #{sort_direction}") items = items.page(page).per_page(per_page) if params[:sSearch].present? items = items.where("CAST(items.id AS TEXT) ilike :search", search: "%#{params[:sSearch]}%") end items end def page params[:iDisplayStart].to_i/per_page + 1 end def per_page params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10 end def sort_column columns = %w[id] sort_col = "items.#{columns[params[:iSortCol_0].to_i]}" end def sort_direction params[:sSortDir_0] == "desc" ? "desc" : "asc" end end