The #as_json method is triggered behind the scenes by the render_json call in the controller. This will return all the data that DataTables expects including all the relevant rows from the database.
# File app/datatables/account_balance_histories_datatable.rb, line 4 def as_json(options = {}) @user = User.find(params[:id]) scope = @user.account_balance.account_balance_histories.union(@user.withdrawable_balance.try(:account_balance_histories)) params[:iColumns].to_i.times do |i| search_value = params["sSearch_#{i}"] unless search_value.blank? column_name = ['id','account_balance_id','amount','resource_type','account_id','resource_id','account_type','credit','created_at'][i] column_type = AccountBalanceHistory.columns_hash[column_name].type case column_type when :string if column_name == "account_type" scope = scope.where("account_balance_histories.#{column_name} LIKE :q", q: "%#{search_value.downcase}%") elsif column_name == "resource_type" scope = scope.where("account_balance_histories.#{column_name} LIKE :q", q: "%#{search_value.downcase}%") end scope = scope.where("account_balance_histories.#{column_name} LIKE :q", q: "%#{search_value.downcase}%") when :integer if column_name == "amount" scope = scope.where("account_balance_histories.amount = :q", q: search_value) elsif column_name == "account_id" credit_card_search = CreditCard.where("number LIKE :q", q: "%#{search_value.downcase}%") scope = scope.where("account_balance_histories.account_id = :q", q: credit_card_search.first.try(:id)) elsif column_name == 'id' scope = scope.where("account_balance_histories.id = :q", q: search_value) elsif column_name == 'account_balance_id' user_search = AccountBalance.joins("JOIN users u ON account_balances.user_id = u.id AND u.name like '#{search_value}%'").pluck(:id) scope = scope.where("account_balance_histories.account_balance_id IN (:q)", q: user_search) end when :boolean scope = scope.where("account_balance_histories.#{column_name} = :q", q: search_value) when :datetime search_values = search_value.split("-") if search_values.length == 2 if search_values.first.blank? scope = scope.where("DATE_FORMAT(account_balance_histories.#{column_name},'%m/%d/%Y %I:%M %p') <= :q", q: search_values.last) else scope = scope.where("DATE_FORMAT(account_balance_histories.#{column_name},'%m/%d/%Y %I:%M %p') BETWEEN :q1 AND :q2", q1: search_values.first, q2: search_values.last) end elsif !search_values.first.blank? scope = scope.where("DATE_FORMAT(account_balance_histories.#{column_name},'%m/%d/%Y %I:%M %p') >= :q", q: search_values.first) end end end end as_datatable(scope) end
This fetches the correct page of data in the correct order.
# File app/datatables/account_balance_histories_datatable.rb, line 53 def data_hash(abh) { id: abh.id, paid_by: abh.resource.try(:user).try(:full_name), resource_type: abh.resource_type, amount: abh.amount, account_id: abh.account.try(:number), account_uri: abh.account.try(:uri), account_type: abh.account_type, credit: abh.credit? ? 'Credit' : 'Debit', created_at: abh.created_at.try(:day_month_date_hr_min_mer) } end
Provide the sequence in which columns to be sorted.
# File app/datatables/account_balance_histories_datatable.rb, line 68 def sort_columns ["account_balance_histories.id", "account_balance_histories.account_balance_id", "account_balance_histories.resource_id", "account_balance_histories.resource_type", "account_balance_histories.account_id", "account_balance_histories.account_type", "account_balance_histories.amount", "account_balance_histories.credit", "account_balance_histories.created_at", "account_balance_histories.updated_at"] end