Permalink
Browse files

initial commit

  • Loading branch information...
jasondew committed Oct 23, 2010
0 parents commit 74d2df645894c6ea036698c40bb735c3a606a63a
@@ -0,0 +1,4 @@
+pkg/*
+*.gem
+.bundle
+*.swp
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
@@ -0,0 +1,63 @@
+PATH
+ remote: .
+ specs:
+ data_table (0.1.0)
+ actionpack (~> 3.0.0)
+ activesupport (~> 3.0.0)
+ will_paginate (~> 3.0.pre2)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionpack (3.0.1)
+ activemodel (= 3.0.1)
+ activesupport (= 3.0.1)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4.1)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.12)
+ rack-test (~> 0.5.4)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.1)
+ activesupport (= 3.0.1)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activesupport (3.0.1)
+ builder (2.1.2)
+ diff-lcs (1.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.4.1)
+ rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
+ rack-test (0.5.6)
+ rack (>= 1.0)
+ rr (1.0.0)
+ rspec (2.0.1)
+ rspec-core (~> 2.0.1)
+ rspec-expectations (~> 2.0.1)
+ rspec-mocks (~> 2.0.1)
+ rspec-core (2.0.1)
+ rspec-expectations (2.0.1)
+ diff-lcs (>= 1.1.2)
+ rspec-mocks (2.0.1)
+ rspec-core (~> 2.0.1)
+ rspec-expectations (~> 2.0.1)
+ shoulda (2.11.3)
+ tzinfo (0.3.23)
+ will_paginate (3.0.pre2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ actionpack (~> 3.0.0)
+ activesupport (~> 3.0.0)
+ data_table!
+ rr (~> 1.0.0)
+ rspec (~> 2.0.0)
+ shoulda (~> 2.11.0)
+ will_paginate (~> 3.0.pre2)
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Jason Dew
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
+= Data Table
+
+Makes it easy to ship data to a jQuery DataTable from ActiveRecord or Mongoid.
+
+== Quick example:
+
+in your javascript:
+
+ $(".providers-data-table").dataTable({"bJQueryUI" : true,
+ "bProcessing" : true,
+ "bAutoWidth" : false,
+ "sPaginationType" : "full_numbers",
+ "aoColumns" : [{"sType" : "html"}, null, null, null, null],
+ "aaSorting" : [[0, 'asc'], [1, 'asc']],
+ "bServerSide" : true,
+ "sAjaxSource" : "/providers.json" }).fnSetFilteringDelay();
+
+Note: the fnSetFilteringDelay() call isn't required but highly recommended: http://datatables.net/plug-ins/api#fnSetFilteringDelay
+
+in your controller:
+
+ class ProvidersController < ApplicationController
+
+ def index
+ respond_to do |wants|
+ wants.html
+ wants.json do
+ render(:json => Provider.for_data_table(self, %w(name fein category county state), %w(name fein)) do |provider|
+ ["<%= link_to(provider, provider) %>", provider.fein, provider.category, provider.county, provider.state]
+ end)
+ end
+ end
+ end
+
+ end
+
+in your view (assuming HAML):
+
+ %table.providers-data-table
+ %thead
+ %tr
+ %th Name
+ %th FEIN
+ %th Category
+ %th County
+ %th State
+
+ %tbody
+
+Patches welcome, enjoy!
+
+== Copyright
+
+Copyright (c) 2010 Jason Dew. See LICENSE for details.
@@ -0,0 +1,3 @@
+require "bundler"
+
+Bundler::GemHelper.install_tasks
@@ -0,0 +1,28 @@
+$:.push File.expand_path("../lib", __FILE__)
+require "data_table/version"
+
+Gem::Specification.new do |s|
+ s.name = "data_table"
+ s.version = DataTable::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Jason Dew"]
+ s.email = ["jason.dew@gmail.com"]
+ s.homepage = "http://rubygems.org/gems/data_table"
+ s.summary = %q{Simple data preparation from AR/Mongoid to the jQuery DataTables plugin}
+ s.description = %q{Simple data preparation from AR/Mongoid to the jQuery DataTables plugin}
+
+ s.rubyforge_project = "data_table"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency "actionpack", "~>3.0.0"
+ s.add_dependency "activesupport", "~>3.0.0"
+ s.add_dependency "will_paginate", "~>3.0.pre2"
+
+ s.add_development_dependency "rspec", "~>2.0.0"
+ s.add_development_dependency "shoulda", "~>2.11.0"
+ s.add_development_dependency "rr", "~>1.0.0"
+end
@@ -0,0 +1,8 @@
+require "active_support/core_ext/object/blank"
+require "active_support/core_ext/object/to_json"
+require "active_support/json/encoding"
+require "active_support/core_ext/string/output_safety"
+
+require "data_table/base"
+require "data_table/active_record"
+require "data_table/mongoid"
@@ -0,0 +1,22 @@
+module DataTable
+ module ActiveRecord
+ module ClassMethods
+ def _find_objects params, fields, search_fields
+ self.where(_where_conditions params[:sSearch], search_fields).
+ order(_order_fields params, fields).
+ paginate :page => _page(params), :per_page => params[:iDisplayLength]
+ end
+
+ def _where_conditions raw_query, search_fields
+ return if (query = sanitize(raw_query)).blank?
+
+ [search_fields.map {|field| ["#{field} LIKE ?"] }.join(" OR "), *(["%#{query}%"] * search_fields.size)]
+ end
+
+ def _order_fields params, fields
+ direction = params[:sSortDir_0] == "asc" ? "ASC" : "DESC"
+ %{#{fields[params[:iSortCol_0].to_i]} #{direction}}
+ end
+ end
+ end
+end
@@ -0,0 +1,42 @@
+module DataTable
+
+ def self.included base
+ base.send :extend, ClassMethods
+ base.send :extend, Mongoid::ClassMethods
+ end
+
+ module ClassMethods
+ def for_data_table controller, fields, search_fields=nil, explicit_block=nil, &implicit_block
+ params = controller.params
+ search_fields ||= fields
+ block = (explicit_block or implicit_block)
+
+ objects = _find_objects params, fields, search_fields
+
+ {:sEcho => params[:sEcho].to_i,
+ :iTotalRecords => self.count,
+ :iTotalDisplayRecords => objects.total_entries,
+ :aaData => _yield_and_render_array(controller, objects, block)
+ }.to_json.html_safe
+ end
+
+ private
+
+ def _yield_and_render_array controller, objects, block
+ objects.map do |object|
+ block[object].map do |string|
+ controller.instance_eval %{
+ Rails.logger.silence do
+ render_to_string :inline => %Q|#{string}|, :locals => {:#{self.name.downcase} => object}
+ end
+ }
+ end
+ end
+ end
+
+ def _page params
+ params[:iDisplayStart].to_i / params[:iDisplayLength].to_i + 1
+ end
+ end
+
+end
@@ -0,0 +1,22 @@
+module DataTable
+ module Mongoid
+ module ClassMethods
+ def _find_objects params, fields, search_fields
+ self.where(_where_conditions params[:sSearch], search_fields).
+ order_by(_order_by_fields params, fields).
+ paginate :page => _page(params), :per_page => params[:iDisplayLength]
+ end
+
+ def _where_conditions raw_query, search_fields
+ return if (query = raw_query.gsub(/\//, "")).blank?
+
+ {"$or" => search_fields.map {|field| {field => /#{query}/i} }}
+ end
+
+ def _order_by_fields params, fields
+ [fields[params[:iSortCol_0].to_i], params[:sSortDir_0]]
+ end
+ end
+ end
+end
+
@@ -0,0 +1,3 @@
+module DataTable
+ VERSION = "0.1.0"
+end
@@ -0,0 +1,44 @@
+require "spec_helper"
+
+describe DataTable do
+
+ include DataTable::ActiveRecord::ClassMethods
+
+ context "#_find_objects" do
+
+ it "should find the objects required based on the params" do
+ params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => 10, :sEcho => 1}
+
+ mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
+ mock(self)._order_fields(params, %w(foo bar baz)) { "order" }
+
+ mock(self).where("where clause") { mock!.order("order") { mock!.paginate({:page => :page, :per_page => 10}) { :answer } } }
+ mock(self)._page(params) { :page }
+
+ _find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
+ end
+
+ end
+
+ context "#_where_conditions" do
+
+ it "should return nil if the query is blank" do
+ mock(self).sanitize(:query) { "" }
+ send(:_where_conditions, :query, %w(foo bar baz)).should == nil
+ end
+
+ it "should return an AR array with an entry for each search field" do
+ mock(self).sanitize(:query) { "q" }
+ send(:_where_conditions, :query, %w(foo bar)).should == ["foo LIKE ? OR bar LIKE ?", "%q%", "%q%"]
+ end
+
+ end
+
+ context "#_order_fields" do
+
+ it "should find the field name and pass the sort direction" do
+ send(:_order_fields, {:iSortCol_0 => "1", :sSortDir_0 => "asc"}, %w(foo bar baz)).should == "bar ASC"
+ end
+
+ end
+end
@@ -0,0 +1,66 @@
+require "spec_helper"
+
+describe DataTable do
+
+ include DataTable::ClassMethods
+
+ context "on being included" do
+ it "should extend ClassMethods" do
+ klass = Class.new
+ mock(klass).send(:extend, DataTable::ClassMethods)
+ mock(klass).send(:extend, DataTable::Mongoid::ClassMethods)
+ klass.instance_eval %{include DataTable}
+ end
+ end
+
+ context "#for_data_table" do
+
+ it "should produce JSON for the datatables plugin to consume" do
+ params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => "10", :sEcho => "1"}
+ controller = mock!.params { params }.subject
+
+ fields = %w(foo bar baz)
+ search_fields = %w(foo bar)
+ block = :block
+
+ mock(self).count { 42 }
+ objects = mock!.total_entries { 10 }.subject
+ mock(self)._find_objects(params, fields, search_fields) { objects }
+ mock(self)._yield_and_render_array(controller, objects, block) { :results }
+
+ result = for_data_table(controller, fields, search_fields, block)
+ result.should == {:sEcho => 1, :iTotalRecords => 42, :iTotalDisplayRecords => 10, :aaData => :results}.to_json.html_safe
+ end
+
+ end
+
+ context "#_yield_and_render_array" do
+
+ it "should walk through the array and render it, passing in the appropriate local name" do
+ block = lambda {|x| mock!.map { [42] }.subject }
+
+ result = _yield_and_render_array Object.new, [:foo], block
+ result.should == [[42]]
+ end
+
+ end
+
+ context "#_page" do
+
+ context "with a display length of 10" do
+ it "should return 1 when start is blank" do
+ send(:_page, {:iDisplayStart => "", :iDisplayLength => "10"}).should == 1
+ end
+
+ it "should return 1 when start is 0" do
+ send(:_page, {:iDisplayStart => "0", :iDisplayLength => "10"}).should == 1
+ end
+
+ it "should return 2 when start is 10" do
+ send(:_page, {:iDisplayStart => "10", :iDisplayLength => "10"}).should == 2
+ end
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit 74d2df6

Please sign in to comment.