module DatatableUtils
Constants
- VERSION
Public Class Methods
included(base)
click to toggle source
# File lib/datatable_utils.rb, line 4 def self.included(base) base.class_eval do delegate :params, :h, :l, :link_to, to: :@view end end
new(view, collection, controller = nil)
click to toggle source
# File lib/datatable_utils.rb, line 10 def initialize(view, collection, controller = nil) @view = view @collection = collection @controller = controller end
Public Instance Methods
as_json(options = {})
click to toggle source
# File lib/datatable_utils.rb, line 16 def as_json(options = {}) { sEcho: params[:sEcho].to_i, iTotalRecords: records_count, iTotalDisplayRecords: (paginate? ? cached_collection.total_entries : records_count), aaData: data } end
Private Instance Methods
cached_collection()
click to toggle source
# File lib/datatable_utils.rb, line 33 def cached_collection @cached_collection ||= fetch_collection end
columns()
click to toggle source
# File lib/datatable_utils.rb, line 89 def columns condition_map ? self.class::COLUMNS.filter(condition_map) : self.class::COLUMNS end
columns_for_search()
click to toggle source
# File lib/datatable_utils.rb, line 93 def columns_for_search condition_map ? self.class::COLUMNS_FOR_SEARCH.filter(condition_map) : self.class::COLUMNS_FOR_SEARCH end
columns_search()
click to toggle source
# File lib/datatable_utils.rb, line 80 def columns_search params[:columns].select{|_, v| v['search']['value'].present?}.each.map do |i, param| "#{columns_for_search[i.to_i]} ILIKE '%#{param['search']['value']}%'" end.join(' AND ') if params[:columns] end
condition_map()
click to toggle source
# File lib/datatable_utils.rb, line 86 def condition_map end
data()
click to toggle source
# File lib/datatable_utils.rb, line 27 def data cached_collection.map do |objekt| data_proc.call objekt end end
fetch_collection()
click to toggle source
# File lib/datatable_utils.rb, line 41 def fetch_collection collection = @collection.where(columns_search).order(sort) if paginate? collection = collection.page(page).per_page(per_page) end if params[:search].present? and params[:search][:value].present? collection = collection.search(params[:search][:value]) end collection end
order_default()
click to toggle source
# File lib/datatable_utils.rb, line 37 def order_default '' end
page()
click to toggle source
# File lib/datatable_utils.rb, line 54 def page params[:start].to_i/per_page + 1 end
paginate?()
click to toggle source
# File lib/datatable_utils.rb, line 62 def paginate? params[:length].to_i > 0 end
per_page()
click to toggle source
# File lib/datatable_utils.rb, line 58 def per_page params[:length].to_i > 0 ? params[:length].to_i : 10 end
records_count()
click to toggle source
# File lib/datatable_utils.rb, line 66 def records_count @records_count ||= @collection.count end
sort()
click to toggle source
# File lib/datatable_utils.rb, line 70 def sort if params[:order] params[:order].each_value.map do |order| "#{columns[order['column'].to_i]} #{order['dir']}" end.join(', ') else order_default end end