data_table 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ data_table (0.1.0)
5
+ actionpack (~> 3.0.0)
6
+ activesupport (~> 3.0.0)
7
+ will_paginate (~> 3.0.pre2)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ abstract (1.0.0)
13
+ actionpack (3.0.1)
14
+ activemodel (= 3.0.1)
15
+ activesupport (= 3.0.1)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.4.1)
19
+ rack (~> 1.2.1)
20
+ rack-mount (~> 0.6.12)
21
+ rack-test (~> 0.5.4)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.1)
24
+ activesupport (= 3.0.1)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.4.1)
27
+ activesupport (3.0.1)
28
+ builder (2.1.2)
29
+ diff-lcs (1.1.2)
30
+ erubis (2.6.6)
31
+ abstract (>= 1.0.0)
32
+ i18n (0.4.1)
33
+ rack (1.2.1)
34
+ rack-mount (0.6.13)
35
+ rack (>= 1.0.0)
36
+ rack-test (0.5.6)
37
+ rack (>= 1.0)
38
+ rr (1.0.0)
39
+ rspec (2.0.1)
40
+ rspec-core (~> 2.0.1)
41
+ rspec-expectations (~> 2.0.1)
42
+ rspec-mocks (~> 2.0.1)
43
+ rspec-core (2.0.1)
44
+ rspec-expectations (2.0.1)
45
+ diff-lcs (>= 1.1.2)
46
+ rspec-mocks (2.0.1)
47
+ rspec-core (~> 2.0.1)
48
+ rspec-expectations (~> 2.0.1)
49
+ shoulda (2.11.3)
50
+ tzinfo (0.3.23)
51
+ will_paginate (3.0.pre2)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ actionpack (~> 3.0.0)
58
+ activesupport (~> 3.0.0)
59
+ data_table!
60
+ rr (~> 1.0.0)
61
+ rspec (~> 2.0.0)
62
+ shoulda (~> 2.11.0)
63
+ will_paginate (~> 3.0.pre2)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jason Dew
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,54 @@
1
+ = Data Table
2
+
3
+ Makes it easy to ship data to a jQuery DataTable from ActiveRecord or Mongoid.
4
+
5
+ == Quick example:
6
+
7
+ in your javascript:
8
+
9
+ $(".providers-data-table").dataTable({"bJQueryUI" : true,
10
+ "bProcessing" : true,
11
+ "bAutoWidth" : false,
12
+ "sPaginationType" : "full_numbers",
13
+ "aoColumns" : [{"sType" : "html"}, null, null, null, null],
14
+ "aaSorting" : [[0, 'asc'], [1, 'asc']],
15
+ "bServerSide" : true,
16
+ "sAjaxSource" : "/providers.json" }).fnSetFilteringDelay();
17
+
18
+ Note: the fnSetFilteringDelay() call isn't required but highly recommended: http://datatables.net/plug-ins/api#fnSetFilteringDelay
19
+
20
+ in your controller:
21
+
22
+ class ProvidersController < ApplicationController
23
+
24
+ def index
25
+ respond_to do |wants|
26
+ wants.html
27
+ wants.json do
28
+ render(:json => Provider.for_data_table(self, %w(name fein category county state), %w(name fein)) do |provider|
29
+ ["<%= link_to(provider, provider) %>", provider.fein, provider.category, provider.county, provider.state]
30
+ end)
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ in your view (assuming HAML):
38
+
39
+ %table.providers-data-table
40
+ %thead
41
+ %tr
42
+ %th Name
43
+ %th FEIN
44
+ %th Category
45
+ %th County
46
+ %th State
47
+
48
+ %tbody
49
+
50
+ Patches welcome, enjoy!
51
+
52
+ == Copyright
53
+
54
+ Copyright (c) 2010 Jason Dew. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler"
2
+
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "data_table/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "data_table"
6
+ s.version = DataTable::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Jason Dew"]
9
+ s.email = ["jason.dew@gmail.com"]
10
+ s.homepage = "http://rubygems.org/gems/data_table"
11
+ s.summary = %q{Simple data preparation from AR/Mongoid to the jQuery DataTables plugin}
12
+ s.description = %q{Simple data preparation from AR/Mongoid to the jQuery DataTables plugin}
13
+
14
+ s.rubyforge_project = "data_table"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "actionpack", "~>3.0.0"
22
+ s.add_dependency "activesupport", "~>3.0.0"
23
+ s.add_dependency "will_paginate", "~>3.0.pre2"
24
+
25
+ s.add_development_dependency "rspec", "~>2.0.0"
26
+ s.add_development_dependency "shoulda", "~>2.11.0"
27
+ s.add_development_dependency "rr", "~>1.0.0"
28
+ end
@@ -0,0 +1,22 @@
1
+ module DataTable
2
+ module ActiveRecord
3
+ module ClassMethods
4
+ def _find_objects params, fields, search_fields
5
+ self.where(_where_conditions params[:sSearch], search_fields).
6
+ order(_order_fields params, fields).
7
+ paginate :page => _page(params), :per_page => params[:iDisplayLength]
8
+ end
9
+
10
+ def _where_conditions query, search_fields
11
+ return if query.blank?
12
+
13
+ [search_fields.map {|field| ["#{field} LIKE ?"] }.join(" OR "), *(["%#{query}%"] * search_fields.size)]
14
+ end
15
+
16
+ def _order_fields params, fields
17
+ direction = params[:sSortDir_0] == "asc" ? "ASC" : "DESC"
18
+ %{#{fields[params[:iSortCol_0].to_i]} #{direction}}
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,42 @@
1
+ module DataTable
2
+
3
+ def self.included base
4
+ base.send :extend, ClassMethods
5
+ base.send :extend, Mongoid::ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def for_data_table controller, fields, search_fields=nil, explicit_block=nil, &implicit_block
10
+ params = controller.params
11
+ search_fields ||= fields
12
+ block = (explicit_block or implicit_block)
13
+
14
+ objects = _find_objects params, fields, search_fields
15
+
16
+ {:sEcho => params[:sEcho].to_i,
17
+ :iTotalRecords => self.count,
18
+ :iTotalDisplayRecords => objects.total_entries,
19
+ :aaData => _yield_and_render_array(controller, objects, block)
20
+ }.to_json.html_safe
21
+ end
22
+
23
+ private
24
+
25
+ def _yield_and_render_array controller, objects, block
26
+ objects.map do |object|
27
+ block[object].map do |string|
28
+ controller.instance_eval %{
29
+ Rails.logger.silence do
30
+ render_to_string :inline => %Q|#{string}|, :locals => {:#{self.name.downcase} => object}
31
+ end
32
+ }
33
+ end
34
+ end
35
+ end
36
+
37
+ def _page params
38
+ params[:iDisplayStart].to_i / params[:iDisplayLength].to_i + 1
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,22 @@
1
+ module DataTable
2
+ module Mongoid
3
+ module ClassMethods
4
+ def _find_objects params, fields, search_fields
5
+ self.where(_where_conditions params[:sSearch], search_fields).
6
+ order_by(_order_by_fields params, fields).
7
+ paginate :page => _page(params), :per_page => params[:iDisplayLength]
8
+ end
9
+
10
+ def _where_conditions raw_query, search_fields
11
+ return if (query = raw_query.gsub(/\//, "")).blank?
12
+
13
+ {"$or" => search_fields.map {|field| {field => /#{query}/i} }}
14
+ end
15
+
16
+ def _order_by_fields params, fields
17
+ [fields[params[:iSortCol_0].to_i], params[:sSortDir_0]]
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,4 @@
1
+ ActiveSupport.on_load(:active_record) do
2
+ ActiveRecord::Base.send :extend, DataTable::ClassMethods
3
+ ActiveRecord::Base.send :extend, DataTable::ActiveRecord::ClassMethods
4
+ end
@@ -0,0 +1,3 @@
1
+ module DataTable
2
+ VERSION = "0.1.1"
3
+ end
data/lib/data_table.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "active_support/core_ext/object/blank"
2
+ require "active_support/core_ext/object/to_json"
3
+ require "active_support/json/encoding"
4
+ require "active_support/core_ext/string/output_safety"
5
+
6
+ require "data_table/base"
7
+ require "data_table/active_record"
8
+ require "data_table/mongoid"
9
+ require "data_table/rails"
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe DataTable do
4
+
5
+ include DataTable::ActiveRecord::ClassMethods
6
+
7
+ context "#_find_objects" do
8
+
9
+ it "should find the objects required based on the params" do
10
+ params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => 10, :sEcho => 1}
11
+
12
+ mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
13
+ mock(self)._order_fields(params, %w(foo bar baz)) { "order" }
14
+
15
+ mock(self).where("where clause") { mock!.order("order") { mock!.paginate({:page => :page, :per_page => 10}) { :answer } } }
16
+ mock(self)._page(params) { :page }
17
+
18
+ _find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
19
+ end
20
+
21
+ end
22
+
23
+ context "#_where_conditions" do
24
+
25
+ it "should return nil if the query is blank" do
26
+ send(:_where_conditions, "", %w(foo bar baz)).should == nil
27
+ end
28
+
29
+ it "should return an AR array with an entry for each search field" do
30
+ send(:_where_conditions, "query", %w(foo bar)).should == ["foo LIKE ? OR bar LIKE ?", "%query%", "%query%"]
31
+ end
32
+
33
+ end
34
+
35
+ context "#_order_fields" do
36
+
37
+ it "should find the field name and pass the sort direction" do
38
+ send(:_order_fields, {:iSortCol_0 => "1", :sSortDir_0 => "asc"}, %w(foo bar baz)).should == "bar ASC"
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,66 @@
1
+ require "spec_helper"
2
+
3
+ describe DataTable do
4
+
5
+ include DataTable::ClassMethods
6
+
7
+ context "on being included" do
8
+ it "should extend ClassMethods" do
9
+ klass = Class.new
10
+ mock(klass).send(:extend, DataTable::ClassMethods)
11
+ mock(klass).send(:extend, DataTable::Mongoid::ClassMethods)
12
+ klass.instance_eval %{include DataTable}
13
+ end
14
+ end
15
+
16
+ context "#for_data_table" do
17
+
18
+ it "should produce JSON for the datatables plugin to consume" do
19
+ params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => "10", :sEcho => "1"}
20
+ controller = mock!.params { params }.subject
21
+
22
+ fields = %w(foo bar baz)
23
+ search_fields = %w(foo bar)
24
+ block = :block
25
+
26
+ mock(self).count { 42 }
27
+ objects = mock!.total_entries { 10 }.subject
28
+ mock(self)._find_objects(params, fields, search_fields) { objects }
29
+ mock(self)._yield_and_render_array(controller, objects, block) { :results }
30
+
31
+ result = for_data_table(controller, fields, search_fields, block)
32
+ result.should == {:sEcho => 1, :iTotalRecords => 42, :iTotalDisplayRecords => 10, :aaData => :results}.to_json.html_safe
33
+ end
34
+
35
+ end
36
+
37
+ context "#_yield_and_render_array" do
38
+
39
+ it "should walk through the array and render it, passing in the appropriate local name" do
40
+ block = lambda {|x| mock!.map { [42] }.subject }
41
+
42
+ result = _yield_and_render_array Object.new, [:foo], block
43
+ result.should == [[42]]
44
+ end
45
+
46
+ end
47
+
48
+ context "#_page" do
49
+
50
+ context "with a display length of 10" do
51
+ it "should return 1 when start is blank" do
52
+ send(:_page, {:iDisplayStart => "", :iDisplayLength => "10"}).should == 1
53
+ end
54
+
55
+ it "should return 1 when start is 0" do
56
+ send(:_page, {:iDisplayStart => "0", :iDisplayLength => "10"}).should == 1
57
+ end
58
+
59
+ it "should return 2 when start is 10" do
60
+ send(:_page, {:iDisplayStart => "10", :iDisplayLength => "10"}).should == 2
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ describe DataTable do
4
+
5
+ include DataTable::Mongoid::ClassMethods
6
+
7
+ context "#_find_objects" do
8
+ it "should find the objects required based on the params" do
9
+ params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => 10, :sEcho => 1}
10
+
11
+ mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
12
+ mock(self)._order_by_fields(params, %w(foo bar baz)) { "order by" }
13
+
14
+ mock(self).where("where clause") { mock!.order_by("order by") { mock!.paginate({:page => :page, :per_page => 10}) { :answer } } }
15
+ mock(self)._page(params) { :page }
16
+
17
+ _find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
18
+ end
19
+ end
20
+
21
+ context "#_where_conditions" do
22
+
23
+ it "should return nil if the query is blank" do
24
+ send(:_where_conditions, "", %w(foo bar baz)).should == nil
25
+ end
26
+
27
+ it "should strip out slashes" do
28
+ send(:_where_conditions, "//", %w(foo bar baz)).should == nil
29
+ end
30
+
31
+ it "should return a mongoid $or hash with an entry for each search field" do
32
+ send(:_where_conditions, "q", %w(foo bar)).should == {"$or" => [{"foo" => /q/i}, {"bar" => /q/i}]}
33
+ end
34
+
35
+ end
36
+
37
+ context "#_order_by_fields" do
38
+
39
+ it "should find the field name and pass the sort direction" do
40
+ send(:_order_by_fields,
41
+ {:iSortCol_0 => "1", :sSortDir_0 => "asc"},
42
+ %w(foo bar baz)).should == ["bar", "asc"]
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ require "data_table"
2
+
3
+ RSpec.configure {|config| config.mock_with :rr }
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_table
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Jason Dew
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-23 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: actionpack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activesupport
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 3
48
+ - 0
49
+ - 0
50
+ version: 3.0.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: will_paginate
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: -1876988247
62
+ segments:
63
+ - 3
64
+ - 0
65
+ - pre2
66
+ version: 3.0.pre2
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 15
78
+ segments:
79
+ - 2
80
+ - 0
81
+ - 0
82
+ version: 2.0.0
83
+ type: :development
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: shoulda
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ hash: 35
94
+ segments:
95
+ - 2
96
+ - 11
97
+ - 0
98
+ version: 2.11.0
99
+ type: :development
100
+ version_requirements: *id005
101
+ - !ruby/object:Gem::Dependency
102
+ name: rr
103
+ prerelease: false
104
+ requirement: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ hash: 23
110
+ segments:
111
+ - 1
112
+ - 0
113
+ - 0
114
+ version: 1.0.0
115
+ type: :development
116
+ version_requirements: *id006
117
+ description: Simple data preparation from AR/Mongoid to the jQuery DataTables plugin
118
+ email:
119
+ - jason.dew@gmail.com
120
+ executables: []
121
+
122
+ extensions: []
123
+
124
+ extra_rdoc_files: []
125
+
126
+ files:
127
+ - .gitignore
128
+ - Gemfile
129
+ - Gemfile.lock
130
+ - LICENSE
131
+ - README.rdoc
132
+ - Rakefile
133
+ - data_table.gemspec
134
+ - lib/data_table.rb
135
+ - lib/data_table/active_record.rb
136
+ - lib/data_table/base.rb
137
+ - lib/data_table/mongoid.rb
138
+ - lib/data_table/rails.rb
139
+ - lib/data_table/version.rb
140
+ - spec/active_record_data_table_spec.rb
141
+ - spec/data_table_spec.rb
142
+ - spec/mongoid_data_table_spec.rb
143
+ - spec/spec_helper.rb
144
+ has_rdoc: true
145
+ homepage: http://rubygems.org/gems/data_table
146
+ licenses: []
147
+
148
+ post_install_message:
149
+ rdoc_options: []
150
+
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 3
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ hash: 3
168
+ segments:
169
+ - 0
170
+ version: "0"
171
+ requirements: []
172
+
173
+ rubyforge_project: data_table
174
+ rubygems_version: 1.3.7
175
+ signing_key:
176
+ specification_version: 3
177
+ summary: Simple data preparation from AR/Mongoid to the jQuery DataTables plugin
178
+ test_files:
179
+ - spec/active_record_data_table_spec.rb
180
+ - spec/data_table_spec.rb
181
+ - spec/mongoid_data_table_spec.rb
182
+ - spec/spec_helper.rb