Skip to content

Instantly share code, notes, and snippets.

View mgreenly's full-sized avatar

Michael Greenly mgreenly

View GitHub Profile
@mgreenly
mgreenly / gist:873316
Created March 16, 2011 21:11
datatable.to_json
#------------------------------------------------------------------------------------------------------------------
# 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)
#
@mgreenly
mgreenly / gist:876350
Created March 18, 2011 16:15
recursively walk association.accessor chain and return the result
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
@mgreenly
mgreenly / gist:971540
Created May 14, 2011 00:23
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
@mgreenly
mgreenly / gist:971546
Created May 14, 2011 00:35
ruby class instance variables
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
@mgreenly
mgreenly / gist:971885
Created May 14, 2011 03:23
datatable spec
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
@mgreenly
mgreenly / gist:977842
Created May 18, 2011 01:43
respond_to respond_with
# 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]
# 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
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("")
@mgreenly
mgreenly / gist:1014710
Created June 8, 2011 16:01
ruby catch/throw
def next
loop do
if cache.empty?
cache.concat(next_block)
if cache.empty?
nil
else
redo
end
else
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