Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Created May 14, 2011 00:23
Show Gist options
  • Save mgreenly/971540 to your computer and use it in GitHub Desktop.
Save mgreenly/971540 to your computer and use it in GitHub Desktop.
datatable with arel and reflection
# 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
def self.current_klass
@inner_klass || @klass
end
def self.relation
@relation
end
#
# always passing an arel object in select ensures
# that we always get fully qualified sql queries
# using the 'table'.'column' format
#
def self.column(name)
@relation = relation.select(current_klass.arel_table[name])
end
#
# keep the inner_klass the actual class keeps it more consistent
# with the klass variable. Using reflect_on_association through
# the class should ensure we don't have to worry about guessing
# table names from model names.
#
def self.join(association, &block)
@inner_klass = current_klass.reflect_on_association(association).klass
@relation = relation.joins(association)
instance_eval(&block) if block_given?
@inner_klass = nil
end
def self.set_model(klass)
@klass = klass
@relation = klass
end
attr_accessor :echo
attr_accessor :data
attr_accessor :total_count
attr_accessor :displayed_count
def initialize(params={})
@echo = (params['sEcho'] || -1).to_i
@data = []
@displayed_count = 0
@total_count = 0
end
def json
{
'sEcho' => echo,
'aaData' => data,
'iTotalRecords' => total_count,
'iTotalDisplayRecords' => displayed_count,
'aaRecords' => []
}
end
def query(params)
self
end
def sql
self.class.relation.to_sql
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment