mongoid-data_table 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +3 -3
- data/README.rdoc +72 -21
- data/lib/mongoid/data_table/criteria.rb +31 -0
- data/lib/mongoid/data_table/proxy.rb +10 -6
- data/lib/mongoid/data_table/version.rb +1 -1
- data/lib/mongoid/data_table.rb +6 -0
- data/spec/models/category.rb +11 -0
- data/spec/models/provider.rb +31 -0
- data/spec/unit/mongoid/data_table/criteria_spec.rb +56 -0
- data/spec/unit/mongoid/data_table/proxy_spec.rb +1 -1
- data/spec/unit/mongoid-data_table_spec.rb +72 -0
- metadata +9 -3
- data/TODO +0 -1
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongoid-data_table (1.0.
|
4
|
+
mongoid-data_table (1.0.3)
|
5
5
|
rails (~> 3.0.0)
|
6
6
|
will_paginate (~> 3.0.pre2)
|
7
7
|
|
@@ -35,7 +35,7 @@ GEM
|
|
35
35
|
activemodel (= 3.0.7)
|
36
36
|
activesupport (= 3.0.7)
|
37
37
|
activesupport (3.0.7)
|
38
|
-
arel (2.0.
|
38
|
+
arel (2.0.10)
|
39
39
|
bson (1.3.1)
|
40
40
|
bson_ext (1.3.1)
|
41
41
|
builder (2.1.2)
|
@@ -81,7 +81,7 @@ GEM
|
|
81
81
|
rspec-core (~> 2.6.0)
|
82
82
|
rspec-expectations (~> 2.6.0)
|
83
83
|
rspec-mocks (~> 2.6.0)
|
84
|
-
rspec-core (2.6.
|
84
|
+
rspec-core (2.6.1)
|
85
85
|
rspec-expectations (2.6.0)
|
86
86
|
diff-lcs (~> 1.1.2)
|
87
87
|
rspec-mocks (2.6.0)
|
data/README.rdoc
CHANGED
@@ -1,40 +1,91 @@
|
|
1
|
-
= Data Table
|
1
|
+
= Mongoid: Data Table
|
2
2
|
|
3
|
-
Makes it easy to ship data to a jQuery DataTable from
|
3
|
+
Makes it easy to ship data to a jQuery DataTable from Mongoid.
|
4
4
|
|
5
5
|
== Quick example:
|
6
6
|
|
7
|
-
|
7
|
+
=== Javascript
|
8
8
|
|
9
|
-
$(".providers-data-table").dataTable({
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
$(".providers-data-table").dataTable({
|
10
|
+
"bJQueryUI" : true,
|
11
|
+
"bProcessing" : true,
|
12
|
+
"bAutoWidth" : false,
|
13
|
+
"sPaginationType" : "full_numbers",
|
14
|
+
"aoColumns" : [{"sType" : "html"}, null, null, null, null],
|
15
|
+
"aaSorting" : [[0, 'asc'], [1, 'asc']],
|
16
|
+
"bServerSide" : true,
|
17
|
+
"sAjaxSource" : "/providers.json"
|
18
|
+
}).fnSetFilteringDelay();
|
17
19
|
|
18
|
-
Note
|
20
|
+
*Note:* the fnSetFilteringDelay() call isn't required but highly recommended: http://datatables.net/plug-ins/api#fnSetFilteringDelay
|
19
21
|
|
20
|
-
|
22
|
+
=== Model
|
23
|
+
|
24
|
+
class Provider
|
25
|
+
|
26
|
+
include Mongoid::Documenta
|
27
|
+
include Mongoid::DataTable
|
28
|
+
|
29
|
+
## fields ##
|
30
|
+
field :name
|
31
|
+
field :fein
|
32
|
+
field :country
|
33
|
+
field :state
|
34
|
+
|
35
|
+
## associations ##
|
36
|
+
referenced_in :category
|
37
|
+
|
38
|
+
## data_table ##
|
39
|
+
data_table_options.merge!({
|
40
|
+
:fields => %w(name fein category country state),
|
41
|
+
:searchable => %w(name fein),
|
42
|
+
:dataset => lambda do |provider|
|
43
|
+
{
|
44
|
+
0 => "<%= link_to(provider.name, provider) %>",
|
45
|
+
1 => provider.fein,
|
46
|
+
2 => provider.category.name,
|
47
|
+
3 => provider.country,
|
48
|
+
4 => provider.state,
|
49
|
+
:DT_RowId => provider._id
|
50
|
+
}
|
51
|
+
end
|
52
|
+
})
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
=== Controller (InheritedResources::Base)
|
57
|
+
|
58
|
+
*Recommended:* https://github.com/josevalim/inherited_resources
|
59
|
+
|
60
|
+
class PrividersController < InheritedResources::Base
|
61
|
+
|
62
|
+
respond_to :json, :only => :index
|
63
|
+
|
64
|
+
protected
|
65
|
+
|
66
|
+
def collection
|
67
|
+
@providers ||= end_of_association_chain.to_data_table(self)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
=== Controller (ActionController::Base)
|
21
73
|
|
22
74
|
class ProvidersController < ApplicationController
|
23
75
|
|
24
76
|
def index
|
25
|
-
respond_to do |
|
26
|
-
|
27
|
-
|
28
|
-
render
|
29
|
-
["<%= link_to(provider, provider) %>", provider.fein, provider.category.name, provider.county, provider.state]
|
30
|
-
end)
|
77
|
+
respond_to do |format|
|
78
|
+
format.html
|
79
|
+
format.json do
|
80
|
+
render :json => Provider.to_data_table(self)
|
31
81
|
end
|
32
82
|
end
|
33
83
|
end
|
34
84
|
|
35
85
|
end
|
36
86
|
|
37
|
-
|
87
|
+
|
88
|
+
=== View (HAML)
|
38
89
|
|
39
90
|
%table.providers-data-table
|
40
91
|
%thead
|
@@ -51,4 +102,4 @@ Patches welcome, enjoy!
|
|
51
102
|
|
52
103
|
== Copyright
|
53
104
|
|
54
|
-
Copyright (c) 2010 Jason Dew. See LICENSE for details.
|
105
|
+
Copyright (c) 2010-2011 Jason Dew, Andrew Bennett. See LICENSE for details.
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module DataTable
|
3
|
+
module Criteria
|
4
|
+
# do nothing
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class Criteria
|
9
|
+
|
10
|
+
# Used for chaining +Criteria+ scopes together in the for of class methods
|
11
|
+
# on the +Document+ the criteria is for.
|
12
|
+
#
|
13
|
+
# Options:
|
14
|
+
#
|
15
|
+
# name: The name of the class method on the +Document+ to chain.
|
16
|
+
# args: The arguments passed to the method.
|
17
|
+
# block: Optional block to pass
|
18
|
+
#
|
19
|
+
# Returns: <tt>Criteria</tt>
|
20
|
+
def method_missing(name, *args, &block)
|
21
|
+
if @klass.respond_to?(name)
|
22
|
+
@klass.send(:with_scope, self) do
|
23
|
+
@klass.send(name, *args, &block)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
return entries.send(name, *args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -8,9 +8,9 @@ module Mongoid
|
|
8
8
|
@klass = klass
|
9
9
|
@controller = controller
|
10
10
|
@options = klass.data_table_options.merge(options)
|
11
|
-
@extension = block
|
11
|
+
@extension = block || klass.data_table_dataset || default_data_table_dataset
|
12
12
|
|
13
|
-
@params = options[:params] || controller.params.dup
|
13
|
+
@params = options[:params] || (controller.params.dup rescue {})
|
14
14
|
@criteria = options[:criteria] || klass.criteria
|
15
15
|
@unscoped = options[:unscoped] || klass.unscoped
|
16
16
|
@fields = options[:fields] || klass.data_table_fields
|
@@ -44,7 +44,7 @@ module Mongoid
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def to_hash(&inline_block)
|
47
|
-
inline_block = extension
|
47
|
+
inline_block = extension unless block_given?
|
48
48
|
{
|
49
49
|
:sEcho => params[:sEcho].to_i,
|
50
50
|
:iTotalRecords => unscoped.count,
|
@@ -60,8 +60,12 @@ module Mongoid
|
|
60
60
|
}
|
61
61
|
end
|
62
62
|
|
63
|
-
def
|
64
|
-
to_hash.to_json
|
63
|
+
def as_json(options = nil, &inline_block)
|
64
|
+
to_hash(&inline_block).to_json(options)
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_json(*args, &inline_block)
|
68
|
+
as_json(*args, &inline_block)
|
65
69
|
end
|
66
70
|
|
67
71
|
protected
|
@@ -105,7 +109,7 @@ module Mongoid
|
|
105
109
|
|
106
110
|
private
|
107
111
|
|
108
|
-
def
|
112
|
+
def default_data_table_dataset
|
109
113
|
lambda do |object|
|
110
114
|
Hash[aliases.map { |c| [ aliases.index(c), object.send(c) ] }].merge(:DT_RowId => object._id)
|
111
115
|
end
|
data/lib/mongoid/data_table.rb
CHANGED
@@ -6,8 +6,10 @@ require 'active_support/core_ext/string/inflections'
|
|
6
6
|
require 'active_support/core_ext/class/inheritable_attributes'
|
7
7
|
require 'active_support/concern'
|
8
8
|
|
9
|
+
require 'mongoid/criteria'
|
9
10
|
require 'mongoid/relations/proxy'
|
10
11
|
|
12
|
+
require 'mongoid/data_table/criteria'
|
11
13
|
require 'mongoid/data_table/proxy'
|
12
14
|
require 'mongoid/data_table/version'
|
13
15
|
|
@@ -35,6 +37,10 @@ module Mongoid
|
|
35
37
|
self.data_table_options[:sortable] ||= self.data_table_fields
|
36
38
|
end
|
37
39
|
|
40
|
+
def data_table_dataset
|
41
|
+
self.data_table_options[:dataset]
|
42
|
+
end
|
43
|
+
|
38
44
|
def to_data_table(controller, options = {}, &block)
|
39
45
|
::Mongoid::DataTable::Proxy.new(self, controller, options, &block)
|
40
46
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Provider
|
2
|
+
|
3
|
+
include Mongoid::Document
|
4
|
+
include Mongoid::DataTable
|
5
|
+
|
6
|
+
## fields ##
|
7
|
+
field :name
|
8
|
+
field :fein
|
9
|
+
field :country
|
10
|
+
field :state
|
11
|
+
|
12
|
+
## associations ##
|
13
|
+
referenced_in :category
|
14
|
+
|
15
|
+
## data_table ##
|
16
|
+
data_table_options.merge!({
|
17
|
+
:fields => %w(name fein category country state),
|
18
|
+
:searchable => %w(name fein),
|
19
|
+
:dataset => lambda do |provider|
|
20
|
+
{
|
21
|
+
0 => "<%= link_to(provider.name, '#') %>",
|
22
|
+
1 => provider.fein,
|
23
|
+
2 => provider.category.name,
|
24
|
+
3 => provider.country,
|
25
|
+
4 => provider.state,
|
26
|
+
:DT_RowId => provider._id
|
27
|
+
}
|
28
|
+
end
|
29
|
+
})
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::DataTable::Criteria do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Rails.stubs(:logger).returns(LOGGER)
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:controller) do
|
10
|
+
Class.new(ActionController::Base).new.tap do |c|
|
11
|
+
c.stubs(:params).returns({})
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:custom_array) { %w(custom) }
|
16
|
+
|
17
|
+
let(:custom_block) do
|
18
|
+
lambda { |object| custom_array }
|
19
|
+
end
|
20
|
+
|
21
|
+
context "with custom block" do
|
22
|
+
|
23
|
+
let(:bob) do
|
24
|
+
Person.find_or_create_by(:name => 'Bob')
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:dt) do
|
28
|
+
bob
|
29
|
+
Person.criteria.to_data_table(controller, {}, &custom_block)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should store it as an @extension" do
|
33
|
+
dt.extension.should == custom_block
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#to_hash" do
|
37
|
+
|
38
|
+
it "should run the custom block" do
|
39
|
+
dt.to_hash[:aaData].first.should == custom_array
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with inline block" do
|
43
|
+
|
44
|
+
it "should run the inline block" do
|
45
|
+
a = custom_array.push('inline')
|
46
|
+
h = dt.to_hash { |object| a }
|
47
|
+
h[:aaData].first.should == a
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -1,2 +1,74 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
describe 'mongoid-data_table' do
|
4
|
+
|
5
|
+
let(:category) do
|
6
|
+
Category.find_or_create_by(:name => 'my_category')
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:provider) do
|
10
|
+
Provider.find_or_create_by({
|
11
|
+
:name => 'my_name',
|
12
|
+
:fein => 'my_fein',
|
13
|
+
:country => 'my_country',
|
14
|
+
:state => 'my_state',
|
15
|
+
:category_id => category.id
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
before do
|
20
|
+
Rails.stubs(:logger).returns(LOGGER)
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:controller) do
|
24
|
+
Class.new(ActionController::Base).new.tap do |c|
|
25
|
+
c.stubs(:params).returns({})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when using example from README" do
|
30
|
+
|
31
|
+
def sample_data(id)
|
32
|
+
{
|
33
|
+
"sEcho" => 0,
|
34
|
+
"iTotalRecords" => 1,
|
35
|
+
"iTotalDisplayRecords" => 1,
|
36
|
+
"aaData" => [{
|
37
|
+
"0" => "<a href=\"#\">my_name</a>",
|
38
|
+
"1" => "my_fein",
|
39
|
+
"2" => "my_category",
|
40
|
+
"3" => "my_country",
|
41
|
+
"4" => "my_state",
|
42
|
+
"DT_RowId" => id.to_s
|
43
|
+
}]
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return sample data" do
|
48
|
+
provider.category.should == category
|
49
|
+
JSON.load(Provider.where(:name => 'my_name').to_data_table(controller).to_json).should == sample_data(provider.id)
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with inline block" do
|
53
|
+
|
54
|
+
def custom_sample_data(id)
|
55
|
+
{
|
56
|
+
"sEcho" => 0,
|
57
|
+
"iTotalRecords" => 1,
|
58
|
+
"iTotalDisplayRecords" => 1,
|
59
|
+
"aaData" => [[id.to_s]]
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return sample data" do
|
64
|
+
provider.category.should == category
|
65
|
+
JSON.load(Provider.where(:name => 'my_name').to_data_table(controller).to_json do |provider|
|
66
|
+
[ provider.id ]
|
67
|
+
end).should == custom_sample_data(provider.id)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid-data_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jason Dew
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-05-
|
14
|
+
date: 2011-05-19 00:00:00 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
@@ -120,16 +120,19 @@ files:
|
|
120
120
|
- LICENSE
|
121
121
|
- README.rdoc
|
122
122
|
- Rakefile
|
123
|
-
- TODO
|
124
123
|
- lib/mongoid-data_table.rb
|
125
124
|
- lib/mongoid/data_table.rb
|
125
|
+
- lib/mongoid/data_table/criteria.rb
|
126
126
|
- lib/mongoid/data_table/proxy.rb
|
127
127
|
- lib/mongoid/data_table/version.rb
|
128
128
|
- mongoid-data_table.gemspec
|
129
|
+
- spec/models/category.rb
|
129
130
|
- spec/models/person.rb
|
131
|
+
- spec/models/provider.rb
|
130
132
|
- spec/models/user_agent.rb
|
131
133
|
- spec/spec_helper.rb
|
132
134
|
- spec/unit/mongoid-data_table_spec.rb
|
135
|
+
- spec/unit/mongoid/data_table/criteria_spec.rb
|
133
136
|
- spec/unit/mongoid/data_table/proxy_spec.rb
|
134
137
|
- spec/unit/mongoid/data_table_spec.rb
|
135
138
|
homepage: http://rubygems.org/gems/mongoid-data_table
|
@@ -160,10 +163,13 @@ signing_key:
|
|
160
163
|
specification_version: 3
|
161
164
|
summary: Simple data preparation from Mongoid to the jQuery DataTables plugin
|
162
165
|
test_files:
|
166
|
+
- spec/models/category.rb
|
163
167
|
- spec/models/person.rb
|
168
|
+
- spec/models/provider.rb
|
164
169
|
- spec/models/user_agent.rb
|
165
170
|
- spec/spec_helper.rb
|
166
171
|
- spec/unit/mongoid-data_table_spec.rb
|
172
|
+
- spec/unit/mongoid/data_table/criteria_spec.rb
|
167
173
|
- spec/unit/mongoid/data_table/proxy_spec.rb
|
168
174
|
- spec/unit/mongoid/data_table_spec.rb
|
169
175
|
has_rdoc:
|