This file contains 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
#------------------------------------------------------------------------------------------------------------------ | |
# type name description | |
#------------------------------------------------------------------------------------------------------------------ | |
# int iTotalRecords Total records, before filtering (i.e. the total number of records in | |
# the database) | |
# | |
# int iTotalDisplayRecords Total records, after filtering (i.e. the total number of records after | |
# filtering has been applied - not just the number of records being returned | |
# in this result set) | |
# |
This file contains 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 render(object) | |
receiver = object | |
accessors = accessor.split(".") | |
while accessor = accessors.shift | |
if receiver.class.reflect_on_association(accessor.to_sym) | |
receiver = receiver.send(accessor) | |
else | |
return receiver.send(accessor) | |
end | |
end |
This file contains 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
# 1. data table subclass is initialized | |
# class methods are going to be called on the class instance, and the class will store table data | |
# | |
# 2. instantiate an instance of the data table subclass ( OrdersIndex.new) | |
# | |
# 3. query the instance w/ pagination and sorting params | |
# a query gets executed w/ params -> ARel | |
# results get stored -> AR | |
# results get passed back as json | |
class DataTable |
This file contains 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
class Foo | |
# create class instance variables | |
class << self; | |
attr_accessor :bar; | |
end | |
#initialize class instance variables | |
@bar = 42 | |
# use class instance variables in class methods |
This file contains 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
require 'spec_helper' | |
describe 'Data tables subclasses' do | |
# it 'should work' do | |
# Order.create!(:order_number => 32) | |
# data_table = OrdersIndex.new | |
# params = {} | |
# json = data_table.query(params).json | |
# json['aaRecords'][0][0].should == 32 |
This file contains 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
# apps/data_tables/orders_index.rb | |
class OrdersIndex < DataTable | |
model Order | |
column :order_number, :width => 300 | |
joins :customer | |
column :first_name | |
column :last_name | |
end | |
column :memo | |
order [:memo, :first_name] |
This file contains 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
# undefine the class if it's defined | |
Object.send(:remove_const, :OrderExample) rescue nil | |
# define the class | |
class OrderExample < DataTable | |
set_model Order | |
column :id | |
column :order_number | |
joins :customer do | |
column :first_name |
This file contains 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
dealers = Dealer.arel_table | |
sales_reps = SalesRep.arel_table | |
dealer_counts = dealers. | |
group(dealers[:sales_rep_id]). | |
where(dealers[:deleted_at].eq(nil)). | |
where(dealers[:ended].eq(nil)). | |
project(dealers[:sales_rep_id], dealers[:id].count.as('value')) | |
def left_outer_join(subselect,as,on) | |
result = Arel.sql("") |
This file contains 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 next | |
loop do | |
if cache.empty? | |
cache.concat(next_block) | |
if cache.empty? | |
nil | |
else | |
redo | |
end | |
else |
This file contains 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
class Datatable::Base | |
#.... | |
include ActionView::Helpers::UrlHelper | |
include ActionView::Helpers::TagHelper | |
def method_missing(symbol, *args, &block) | |
if symbol.to_s =~ /(path|url)$/ | |
return Rails.application.routes.url_helpers.send(symbol, *args) | |
end | |
super(symbol, *args, &block) | |
end |