simple_datatables 0.2.0 → 0.2.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/README.rdoc CHANGED
@@ -75,6 +75,18 @@ By default "starts_with" is used due to database performance reasons.
75
75
 
76
76
  Independent fields search will always search using "starts_with" finder.
77
77
 
78
+ == Custom pagination implementations
79
+
80
+ If you are getting strange results (for instance, iTotalRecords and iTotalDisplayRecords both equal 0, or entirely the wrong collection is used), you might want to specify which collection to use:
81
+
82
+ respond_with @products, :locals => {:collection => @products}
83
+
84
+ Also if you are using own or uncommon implementation of pagination you may use direct page count variables setting as follows:
85
+
86
+ respond_with @products, :locals => {:total_entries => 12, :current_page_entries => 10}
87
+
88
+ In this case gem will not try to find collection or pagination and will use the counters passed via locals.
89
+
78
90
  == Example
79
91
 
80
92
  The following code will show products list datatables. Manufacturer is belongs_to association for Product.
@@ -89,11 +101,7 @@ In your controller:
89
101
  respond_with @products
90
102
  end
91
103
 
92
- If you are getting strange results (for instance, iTotalRecords and iTotalDisplayRecords both equal 0, or entirely the wrong collection is used), you might want to specify which collection to use:
93
-
94
- respond_with @products, :locals => {:collection => @products}
95
-
96
- In your search view:
104
+ In your search view (app/views/products/search.jsonify):
97
105
 
98
106
  @products.each do |product|
99
107
  json << [product.name, product.manufacturer.name]
@@ -112,7 +120,7 @@ In your index view:
112
120
 
113
121
  In your javascript:
114
122
 
115
- $("products").dataTable($("table#products"), {
123
+ $("#products").dataTable({
116
124
  "sAjaxSource" : "/products/search.datatables",
117
125
  "aaSorting" : [[0, 'asc']],
118
126
  "aoColumns" : [
@@ -1,37 +1,33 @@
1
- total_entries = nil
2
- current_page_entries = 0
1
+ total_entries ||= nil
2
+ current_page_entries ||= 0
3
3
  collection ||= nil
4
4
 
5
- instance_variables.each do |vn|
6
- v = instance_variable_get(vn)
7
-
8
- # if a collection variable was specified, use it only
9
- next if collection && collection != v
5
+ # Looking for object with pagination if pages count not set
6
+ if total_entries.nil? and current_page_entries==0
7
+
8
+ # looking for the right collection if not initiated via parameter
9
+ if collection.nil?
10
+ instance_variables.each do |vn|
11
+ v = instance_variable_get(vn)
10
12
 
11
- next if v.class.name.nil?
12
- if v.class.name.start_with?("MetaSearch::Searches") and v.respond_to?('size')
13
- current_page_entries = v.size
14
- end
15
- if v.class.name.start_with?("ActiveRecord::Relation") and v.respond_to?('size')
16
- current_page_entries = v.size
17
- end
18
- if v.class.name.start_with?("Array") and v.respond_to?('size') and (current_page_entries == 0)
19
- current_page_entries = v.size
20
- end
13
+ # if a collection variable was specified, use it only
14
+ next if collection && collection != v
15
+ next if v.class.name.nil?
21
16
 
22
- if v.class.name.start_with?("Mongoid::Criteria")
23
- total_entries = v.length
24
- break;
17
+ collection = v if v.class.name.start_with?("MetaSearch::Searches", "ActiveRecord::Relation", "Mongoid::Criteria")
18
+ end
25
19
  end
26
20
 
27
- if v.respond_to?('total_entries')
28
- total_entries = v.total_entries
29
- current_page_entries = v.size
30
- break
31
- end
32
- end
21
+ # receiving correct current page entries count
22
+ current_page_entries = collection.size if collection.respond_to?('size')
33
23
 
34
- total_entries = current_page_entries if total_entries.nil?
24
+ # looking for the total entries count
25
+ total_entries = collection.length if collection.class.name.start_with?("Mongoid::Criteria")
26
+ total_entries = collection.total_entries if collection.respond_to?('total_entries')
27
+
28
+ # fallback to 1 page if total entries count not found
29
+ total_entries = current_page_entries if total_entries.nil?
30
+ end
35
31
 
36
32
  json.iTotalRecords current_page_entries
37
33
  json.iTotalDisplayRecords total_entries
@@ -1,3 +1,3 @@
1
1
  module SimpleDatatables
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -68,11 +68,7 @@ root.simpleDatatables = ( sSource, aoData, fnCallback ) ->
68
68
  );
69
69
 
70
70
  if sSearch
71
- op =
72
- if bRegex
73
- "_contains"
74
- else
75
- "_sw"
71
+ op = bRegex ? "_contains" : "_sw"
76
72
  data.push({name: "search["+searchcolumns.join("_or_")+op+"]", value: sSearch});
77
73
 
78
74
  $.ajax( { "dataType": 'json', "type": "GET", "url": sSource, "data": data, "success": fnCallback } );
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000Z
12
+ date: 2011-12-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &21099948 !ruby/object:Gem::Requirement
16
+ requirement: &28322484 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *21099948
24
+ version_requirements: *28322484
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: meta_search
27
- requirement: &21274632 !ruby/object:Gem::Requirement
27
+ requirement: &28322184 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *21274632
35
+ version_requirements: *28322184
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jsonify-rails
38
- requirement: &21341652 !ruby/object:Gem::Requirement
38
+ requirement: &28321956 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *21341652
46
+ version_requirements: *28321956
47
47
  description: Simple datatables to rails mapping using meta_search, will_paginage and
48
48
  jsonify
49
49
  email: