mongoid-data_table 0.0.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 +4 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +104 -0
- data/LICENSE +20 -0
- data/README.rdoc +54 -0
- data/Rakefile +8 -0
- data/TODO +1 -0
- data/lib/mongoid/data_table/proxy.rb +119 -0
- data/lib/mongoid/data_table/version.rb +5 -0
- data/lib/mongoid/data_table.rb +45 -0
- data/lib/mongoid-data_table.rb +1 -0
- data/mongoid-data_table.gemspec +27 -0
- data/spec/active_record_data_table_spec.rb +52 -0
- data/spec/data_table_spec.rb +82 -0
- data/spec/mongoid_data_table_spec.rb +47 -0
- data/spec/spec_helper.rb +3 -0
- metadata +152 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongoid-data_table (0.0.1)
|
5
|
+
rails (~> 3.0.0)
|
6
|
+
will_paginate (~> 3.0.pre2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
abstract (1.0.0)
|
12
|
+
actionmailer (3.0.7)
|
13
|
+
actionpack (= 3.0.7)
|
14
|
+
mail (~> 2.2.15)
|
15
|
+
actionpack (3.0.7)
|
16
|
+
activemodel (= 3.0.7)
|
17
|
+
activesupport (= 3.0.7)
|
18
|
+
builder (~> 2.1.2)
|
19
|
+
erubis (~> 2.6.6)
|
20
|
+
i18n (~> 0.5.0)
|
21
|
+
rack (~> 1.2.1)
|
22
|
+
rack-mount (~> 0.6.14)
|
23
|
+
rack-test (~> 0.5.7)
|
24
|
+
tzinfo (~> 0.3.23)
|
25
|
+
activemodel (3.0.7)
|
26
|
+
activesupport (= 3.0.7)
|
27
|
+
builder (~> 2.1.2)
|
28
|
+
i18n (~> 0.5.0)
|
29
|
+
activerecord (3.0.7)
|
30
|
+
activemodel (= 3.0.7)
|
31
|
+
activesupport (= 3.0.7)
|
32
|
+
arel (~> 2.0.2)
|
33
|
+
tzinfo (~> 0.3.23)
|
34
|
+
activeresource (3.0.7)
|
35
|
+
activemodel (= 3.0.7)
|
36
|
+
activesupport (= 3.0.7)
|
37
|
+
activesupport (3.0.7)
|
38
|
+
arel (2.0.9)
|
39
|
+
bson (1.3.1)
|
40
|
+
bson_ext (1.3.1)
|
41
|
+
builder (2.1.2)
|
42
|
+
diff-lcs (1.1.2)
|
43
|
+
erubis (2.6.6)
|
44
|
+
abstract (>= 1.0.0)
|
45
|
+
i18n (0.5.0)
|
46
|
+
mail (2.2.19)
|
47
|
+
activesupport (>= 2.3.6)
|
48
|
+
i18n (>= 0.4.0)
|
49
|
+
mime-types (~> 1.16)
|
50
|
+
treetop (~> 1.4.8)
|
51
|
+
mime-types (1.16)
|
52
|
+
mongo (1.3.1)
|
53
|
+
bson (>= 1.3.1)
|
54
|
+
mongoid (2.0.1)
|
55
|
+
activemodel (~> 3.0)
|
56
|
+
mongo (~> 1.3)
|
57
|
+
tzinfo (~> 0.3.22)
|
58
|
+
will_paginate (~> 3.0.pre)
|
59
|
+
polyglot (0.3.1)
|
60
|
+
rack (1.2.2)
|
61
|
+
rack-mount (0.6.14)
|
62
|
+
rack (>= 1.0.0)
|
63
|
+
rack-test (0.5.7)
|
64
|
+
rack (>= 1.0)
|
65
|
+
rails (3.0.7)
|
66
|
+
actionmailer (= 3.0.7)
|
67
|
+
actionpack (= 3.0.7)
|
68
|
+
activerecord (= 3.0.7)
|
69
|
+
activeresource (= 3.0.7)
|
70
|
+
activesupport (= 3.0.7)
|
71
|
+
bundler (~> 1.0)
|
72
|
+
railties (= 3.0.7)
|
73
|
+
railties (3.0.7)
|
74
|
+
actionpack (= 3.0.7)
|
75
|
+
activesupport (= 3.0.7)
|
76
|
+
rake (>= 0.8.7)
|
77
|
+
thor (~> 0.14.4)
|
78
|
+
rake (0.8.7)
|
79
|
+
rr (1.0.2)
|
80
|
+
rspec (2.5.0)
|
81
|
+
rspec-core (~> 2.5.0)
|
82
|
+
rspec-expectations (~> 2.5.0)
|
83
|
+
rspec-mocks (~> 2.5.0)
|
84
|
+
rspec-core (2.5.2)
|
85
|
+
rspec-expectations (2.5.0)
|
86
|
+
diff-lcs (~> 1.1.2)
|
87
|
+
rspec-mocks (2.5.0)
|
88
|
+
shoulda (2.11.3)
|
89
|
+
thor (0.14.6)
|
90
|
+
treetop (1.4.9)
|
91
|
+
polyglot (>= 0.3.1)
|
92
|
+
tzinfo (0.3.27)
|
93
|
+
will_paginate (3.0.pre2)
|
94
|
+
|
95
|
+
PLATFORMS
|
96
|
+
ruby
|
97
|
+
|
98
|
+
DEPENDENCIES
|
99
|
+
bson_ext (~> 1.3.0)
|
100
|
+
mongoid (~> 2.0.1)
|
101
|
+
mongoid-data_table!
|
102
|
+
rr (~> 1.0.2)
|
103
|
+
rspec (~> 2.5.0)
|
104
|
+
shoulda (~> 2.11.3)
|
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 categories.name county state), %w(name fein)) do |provider|
|
29
|
+
["<%= link_to(provider, provider) %>", provider.fein, provider.category.name, 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,119 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module DataTable
|
3
|
+
class Proxy < ::Mongoid::Relations::Proxy
|
4
|
+
|
5
|
+
attr_reader :klass, :controller, :options, :block, :params, :criteria, :unscoped, :fields
|
6
|
+
|
7
|
+
def initialize(klass, controller, options = {}, &block)
|
8
|
+
@klass = klass
|
9
|
+
@controller = controller
|
10
|
+
@options = klass.data_table_options.merge(options)
|
11
|
+
@block = block
|
12
|
+
|
13
|
+
@params = options[:params] || controller.params.dup
|
14
|
+
@criteria = options[:criteria] || klass.scoped
|
15
|
+
@unscoped = options[:unscoped] || klass.unscoped
|
16
|
+
@fields = options[:fields] || klass.data_table_fields
|
17
|
+
end
|
18
|
+
|
19
|
+
def collection(force = false)
|
20
|
+
reload if force
|
21
|
+
@collection ||= conditions.paginate({
|
22
|
+
:page => current_page,
|
23
|
+
:per_page => per_page
|
24
|
+
})
|
25
|
+
end
|
26
|
+
|
27
|
+
def reload
|
28
|
+
(@collection = nil).nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
## pagination options ##
|
32
|
+
|
33
|
+
def current_page
|
34
|
+
params[:page].present? ? params[:page].to_i : (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i rescue 0)+1
|
35
|
+
end
|
36
|
+
|
37
|
+
def per_page
|
38
|
+
(params[:iDisplayLength] || klass.per_page rescue 10).to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
def conditions
|
42
|
+
criteria.order_by(order_by_conditions).where(filter_conditions).where(filter_field_conditions)
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_hash(&inline_block)
|
46
|
+
inline_block = block || default_data_table_block unless block_given?
|
47
|
+
{
|
48
|
+
:sEcho => params[:sEcho].to_i,
|
49
|
+
:iTotalRecords => unscoped.count,
|
50
|
+
:iTotalDisplayRecords => conditions.count,
|
51
|
+
:aaData => collection.map do |object|
|
52
|
+
data = controller.instance_eval { inline_block[object] }
|
53
|
+
data.inject(data.is_a?(Hash) ? {} : []) do |result, item|
|
54
|
+
Rails.logger.silence do
|
55
|
+
controller.instance_eval(&render_data_table_block(klass, item, object, result))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_json(*args)
|
63
|
+
to_hash.to_json
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
def order_by_conditions
|
69
|
+
order_params = params.select { |k,v| k =~ /(i|s)Sort(Col|Dir)_\d+/ }
|
70
|
+
return options[:order_by] || [] if order_params.blank?
|
71
|
+
order_params.select { |k,v| k =~ /iSortCol_\d+/ }.sort_by(&:first).map do |col,field|
|
72
|
+
i = /iSortCol_(\d+)/.match(col)[1]
|
73
|
+
[ fields[field.to_i], order_params["sSortDir_#{i}"] || :asc ]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def filter_conditions
|
78
|
+
return unless (query = params[:sSearch]).present?
|
79
|
+
|
80
|
+
{"$or" => klass.data_table_search_fields.map { |field| { field => /#{query}/i} } }
|
81
|
+
end
|
82
|
+
|
83
|
+
def filter_field_conditions
|
84
|
+
order_params = params.select { |k,v| k =~ /sSearch_\d+/ }.inject({}) do |h,(k,v)|
|
85
|
+
i = /sSearch_(\d+)/.match(k)[1]
|
86
|
+
h[fields[i.to_i]] = Regexp.new(params["sSearch_#{i}"]) if params["sSearch_#{i}"].present?
|
87
|
+
h
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def default_data_table_block
|
94
|
+
lambda do |object|
|
95
|
+
Hash[fields.map { |c| [ fields.index(c), object.send(c) ] }].merge(:DT_RowId => object._id)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def render_data_table_block(klass, item, object, result = [])
|
100
|
+
data_table = self
|
101
|
+
lambda do |base|
|
102
|
+
if result.is_a?(Hash)
|
103
|
+
result.store(*(item.map do |value|
|
104
|
+
self.instance_eval(&data_table.send(:render_data_table_block, klass, value, object, ''))
|
105
|
+
end))
|
106
|
+
else
|
107
|
+
result << render_to_string(:inline => item.to_s, :locals => { :"#{klass.name.underscore}" => object, :object => object })
|
108
|
+
end
|
109
|
+
result
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def method_missing(method, *args, &block) #:nodoc:
|
114
|
+
collection.send(method, *args, &block)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'active_support/core_ext/object/blank'
|
3
|
+
require 'active_support/core_ext/object/to_json'
|
4
|
+
require 'active_support/json/encoding'
|
5
|
+
require 'active_support/core_ext/string/output_safety'
|
6
|
+
require 'active_support/core_ext/string/inflections'
|
7
|
+
require 'active_support/core_ext/class/inheritable_attributes'
|
8
|
+
|
9
|
+
require 'mongoid/data_table/proxy'
|
10
|
+
require 'mongoid/data_table/version'
|
11
|
+
|
12
|
+
module Mongoid
|
13
|
+
module DataTable
|
14
|
+
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
|
17
|
+
included do
|
18
|
+
self.class_attribute :data_table_options
|
19
|
+
self.data_table_options ||= {}
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
|
24
|
+
def data_table_fields
|
25
|
+
self.data_table_options[:fields] ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
def data_table_searchable_fields
|
29
|
+
self.data_table_options[:searchable] ||= self.data_table_fields
|
30
|
+
end
|
31
|
+
|
32
|
+
def data_table_sortable_fields
|
33
|
+
self.data_table_options[:sortable] ||= self.data_table_fields
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_data_table(controller, options = {}, explicit_block = nil, &implicit_block) #fields, search_fields=nil, explicit_block=nil, &implicit_block)
|
37
|
+
block = (explicit_block or implicit_block)
|
38
|
+
|
39
|
+
DataTable::Proxy.new(self, controller, options, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'mongoid/data_table'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "mongoid/data_table/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "mongoid-data_table"
|
6
|
+
s.version = Mongoid::DataTable::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Jason Dew", "Andrew Bennett"]
|
9
|
+
s.email = ["jason.dew@gmail.com", "potatosaladx@gmail.com"]
|
10
|
+
s.homepage = "http://rubygems.org/gems/mongoid-data_table"
|
11
|
+
s.summary = %q{Simple data preparation from Mongoid to the jQuery DataTables plugin}
|
12
|
+
s.description = %q{Simple data preparation from Mongoid to the jQuery DataTables plugin}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency "rails", "~>3.0.0"
|
20
|
+
s.add_dependency "will_paginate", "~>3.0.pre2"
|
21
|
+
|
22
|
+
s.add_development_dependency "bson_ext", "~>1.3.0"
|
23
|
+
s.add_development_dependency "mongoid", "~>2.0.1"
|
24
|
+
s.add_development_dependency "rspec", "~>2.5.0"
|
25
|
+
s.add_development_dependency "shoulda", "~>2.11.3"
|
26
|
+
s.add_development_dependency "rr", "~>1.0.2"
|
27
|
+
end
|
@@ -0,0 +1,52 @@
|
|
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)._discover_joins(%w(foo bar baz)) { [] }
|
13
|
+
mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
|
14
|
+
mock(self)._order_fields(params, %w(foo bar baz)) { "order" }
|
15
|
+
|
16
|
+
mock(self).where("where clause") { mock!.includes([]) { mock!.order("order") { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } } }
|
17
|
+
mock(self)._page(params) { :page }
|
18
|
+
mock(self)._per_page(params) { :per_page }
|
19
|
+
|
20
|
+
_find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
context "#_where_conditions" do
|
26
|
+
|
27
|
+
it "should return nil if the query is blank" do
|
28
|
+
send(:_where_conditions, "", %w(foo bar baz)).should == nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return an AR array with an entry for each search field" do
|
32
|
+
send(:_where_conditions, "query", %w(foo bar)).should == ["UPPER(foo) LIKE ? OR UPPER(bar) LIKE ?", "%QUERY%", "%QUERY%"]
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
context "#_discover_joins" do
|
38
|
+
|
39
|
+
it "should return the joins on the fields" do
|
40
|
+
_discover_joins(%w(foo.bar foz.ber baz)).to_a.should == [:foo, :foz]
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
context "#_order_fields" do
|
46
|
+
|
47
|
+
it "should find the field name and pass the sort direction" do
|
48
|
+
send(:_order_fields, {:iSortCol_0 => "1", :sSortDir_0 => "asc"}, %w(foo bar baz)).should == "bar ASC"
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe DataTable do
|
4
|
+
|
5
|
+
include DataTable::Base::ClassMethods
|
6
|
+
|
7
|
+
context "on being included" do
|
8
|
+
it "should extend ClassMethods" do
|
9
|
+
klass = Class.new
|
10
|
+
klass.instance_eval %{include DataTable::Base}
|
11
|
+
(class << klass; self.included_modules; end).should include(DataTable::Base::ClassMethods)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "#for_data_table" do
|
16
|
+
|
17
|
+
it "should produce JSON for the datatables plugin to consume" do
|
18
|
+
params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => "10", :sEcho => "1"}
|
19
|
+
controller = mock!.params { params }.subject
|
20
|
+
|
21
|
+
fields = %w(foo bar baz)
|
22
|
+
search_fields = %w(foo bar)
|
23
|
+
block = :block
|
24
|
+
|
25
|
+
mock(self).count { 42 }
|
26
|
+
objects = mock!.total_entries { 10 }.subject
|
27
|
+
mock(self)._find_objects(params, fields, search_fields) { objects }
|
28
|
+
mock(self)._yield_and_render_array(controller, objects, block) { :results }
|
29
|
+
|
30
|
+
result = for_data_table(controller, fields, search_fields, block)
|
31
|
+
result.should == {:sEcho => 1, :iTotalRecords => 42, :iTotalDisplayRecords => 10, :aaData => :results}.to_json.html_safe
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
context "#_yield_and_render_array" do
|
37
|
+
|
38
|
+
it "should walk through the array and render it, passing in the appropriate local name" do
|
39
|
+
block = lambda {|x| mock!.map { [42] }.subject }
|
40
|
+
|
41
|
+
result = _yield_and_render_array Object.new, [:foo], block
|
42
|
+
result.should == [[42]]
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
context "#_page" do
|
48
|
+
|
49
|
+
context "with a display length of 10" do
|
50
|
+
it "should return 1 when start is blank" do
|
51
|
+
send(:_page, {:iDisplayStart => "", :iDisplayLength => "10"}).should == 1
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return 1 when start is 0" do
|
55
|
+
send(:_page, {:iDisplayStart => "0", :iDisplayLength => "10"}).should == 1
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return 2 when start is 10" do
|
59
|
+
send(:_page, {:iDisplayStart => "10", :iDisplayLength => "10"}).should == 2
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
context "#_per_page" do
|
66
|
+
|
67
|
+
it "should return 10 given an iDisplayLength of 10" do
|
68
|
+
send(:_per_page, {:iDisplayLength => "10"}).should == 10
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should return a default of 25 given an invalid iDisplayLength" do
|
72
|
+
send(:_per_page, {:iDisplayLength => "foobar"}).should == 25
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return self.count given an iDisplayLength of -1" do
|
76
|
+
mock(self).count { :all }
|
77
|
+
send(:_per_page, {:iDisplayLength => "-1"}).should == :all
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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 => :per_page}) { :answer } } }
|
15
|
+
mock(self)._page(params) { :page }
|
16
|
+
mock(self)._per_page(params) { :per_page }
|
17
|
+
|
18
|
+
_find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "#_where_conditions" do
|
23
|
+
|
24
|
+
it "should return nil if the query is blank" do
|
25
|
+
send(:_where_conditions, "", %w(foo bar baz)).should == nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should strip out slashes" do
|
29
|
+
send(:_where_conditions, "//", %w(foo bar baz)).should == nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return a mongoid $or hash with an entry for each search field" do
|
33
|
+
send(:_where_conditions, "q", %w(foo bar)).should == {"$or" => [{"foo" => /q/i}, {"bar" => /q/i}]}
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
context "#_order_by_fields" do
|
39
|
+
|
40
|
+
it "should find the field name and pass the sort direction" do
|
41
|
+
send(:_order_by_fields,
|
42
|
+
{:iSortCol_0 => "1", :sSortDir_0 => "asc"},
|
43
|
+
%w(foo bar baz)).should == ["bar", "asc"]
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-data_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason Dew
|
9
|
+
- Andrew Bennett
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2011-05-16 00:00:00 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rails
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 3.0.0
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: will_paginate
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.pre2
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bson_ext
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.3.0
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: mongoid
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.0.1
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rspec
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.5.0
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: shoulda
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ~>
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 2.11.3
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id006
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: rr
|
84
|
+
prerelease: false
|
85
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ~>
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 1.0.2
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id007
|
93
|
+
description: Simple data preparation from Mongoid to the jQuery DataTables plugin
|
94
|
+
email:
|
95
|
+
- jason.dew@gmail.com
|
96
|
+
- potatosaladx@gmail.com
|
97
|
+
executables: []
|
98
|
+
|
99
|
+
extensions: []
|
100
|
+
|
101
|
+
extra_rdoc_files: []
|
102
|
+
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- Gemfile
|
106
|
+
- Gemfile.lock
|
107
|
+
- LICENSE
|
108
|
+
- README.rdoc
|
109
|
+
- Rakefile
|
110
|
+
- TODO
|
111
|
+
- lib/mongoid-data_table.rb
|
112
|
+
- lib/mongoid/data_table.rb
|
113
|
+
- lib/mongoid/data_table/proxy.rb
|
114
|
+
- lib/mongoid/data_table/version.rb
|
115
|
+
- mongoid-data_table.gemspec
|
116
|
+
- spec/active_record_data_table_spec.rb
|
117
|
+
- spec/data_table_spec.rb
|
118
|
+
- spec/mongoid_data_table_spec.rb
|
119
|
+
- spec/spec_helper.rb
|
120
|
+
homepage: http://rubygems.org/gems/mongoid-data_table
|
121
|
+
licenses: []
|
122
|
+
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: "0"
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: "0"
|
140
|
+
requirements: []
|
141
|
+
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 1.7.2
|
144
|
+
signing_key:
|
145
|
+
specification_version: 3
|
146
|
+
summary: Simple data preparation from Mongoid to the jQuery DataTables plugin
|
147
|
+
test_files:
|
148
|
+
- spec/active_record_data_table_spec.rb
|
149
|
+
- spec/data_table_spec.rb
|
150
|
+
- spec/mongoid_data_table_spec.rb
|
151
|
+
- spec/spec_helper.rb
|
152
|
+
has_rdoc:
|