Created
September 24, 2011 15:05
-
-
Save mgreenly/1239432 to your computer and use it in GitHub Desktop.
Ideas for Datable v0.3
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
# ./config/initializers/datatable.rb | |
Datatable::Base.configure do |config| | |
config.jquery_ui = true | |
config.table_tools = true | |
config.sDom = '<"H"lrf>t<"F"ip>' | |
config.iDisplayLength = 25 | |
config.oLanguage = { 'sProcessing' => "<img alt='Spinner' src='/images/loader.gif'/>", 'sInfoFiltered' => '' } | |
end | |
# ./app/datatables/orders_index.rb | |
class OrdersIndex < DataTable::Base | |
relation Order.join(:customer).where("orders.status like 'open'") | |
column "orders.order_number" do # the first parameter is the 'table.column' name. Behind the scenes datatable will | |
link_to "{{1}}", order_path("{{3}}") # use that parameter to automatically add a select to the relation. This eliminates | |
end # having to specificy the 'where' or the 'count' separately. Also notice that the | |
column "customers.name" # column method takes a block or option hash. | |
column "orders.id", :bVisible => false | |
option :aaSorting, [[1, 'ASC'], [2,'ASC']] # per datatable options (default sorting shown) | |
end | |
# ./app/datatables/orders_index.rb | |
class OrdersIndex < DataTable::Base | |
# A more complex example using Arel directly. | |
orders = Order.arel_table | |
items = Items.arel_table | |
customers = Customer.arel_table | |
relation orders. | |
join(items).on(items[:order_id].eq(orders[:id])). | |
join(customers).on(orders[:customer_id].eq(customers[:id])). | |
where(orders[:status].matches('open')). | |
group(items[:order_id]) | |
column orders[:order_number], :link_to => link_to("{{1}}","{{4}}") # still trying to come up with better ways to do the | |
column customers[:name] # variable substituion. No good ideas yet. I don't like | |
column items[:amount].sum # that you have to know the column indexes | |
column orders[:id], :bVisible => false | |
end | |
# Simple controller example. | |
class OrdersController < ApplicationController | |
respond_to :json, :html | |
def index | |
@data = OrderIndex.query(params) # @data is still just an array of rows like it is today | |
respond_to do |format| | |
format.html | |
format.json {render :json => @data} | |
end | |
end | |
end | |
# Example of per request modifications to the query. Use the 'relation' method you get the relation | |
# and then you can use any ActiveRelation calls; where, group, having, etc.. | |
class OrdersController < ApplicationController | |
respond_to :json, :html | |
def index | |
orders = Orders.arel_table | |
@data = OrderIndex.relation.where("orders.customer_id = #{current_user.id}").query(params) | |
authorize! :read, @orders | |
respond_to do |format| | |
format.html | |
format.json {render :json => @data} | |
end | |
end | |
end | |
# You can also use Arel directly with the relation like you'd expect | |
class OrdersController < ApplicationController | |
respond_to :json, :html | |
def index | |
orders_table = Orders.arel_table | |
@data = OrderIndex.where(orders_table[:user_id].eq(current_user.id)).query(params) | |
respond_to do |format| | |
format.html | |
format.json {render :json => @data} | |
end | |
end | |
end | |
# In the view we pass the class and the query result to the helper method | |
<%= datatable OrdersIndex, @orders %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment