app/views/layouts/application.datatables.jsonify in simple_datatables-0.2.0 vs app/views/layouts/application.datatables.jsonify in simple_datatables-0.2.1
- old
+ new
@@ -1,38 +1,34 @@
-total_entries = nil
-current_page_entries = 0
+total_entries ||= nil
+current_page_entries ||= 0
collection ||= nil
-instance_variables.each do |vn|
- v = instance_variable_get(vn)
+# Looking for object with pagination if pages count not set
+if total_entries.nil? and current_page_entries==0
+
+ # looking for the right collection if not initiated via parameter
+ if collection.nil?
+ instance_variables.each do |vn|
+ v = instance_variable_get(vn)
- # if a collection variable was specified, use it only
- next if collection && collection != v
-
- next if v.class.name.nil?
- if v.class.name.start_with?("MetaSearch::Searches") and v.respond_to?('size')
- current_page_entries = v.size
- end
- if v.class.name.start_with?("ActiveRecord::Relation") and v.respond_to?('size')
- current_page_entries = v.size
- end
- if v.class.name.start_with?("Array") and v.respond_to?('size') and (current_page_entries == 0)
- current_page_entries = v.size
- end
+ # if a collection variable was specified, use it only
+ next if collection && collection != v
+ next if v.class.name.nil?
- if v.class.name.start_with?("Mongoid::Criteria")
- total_entries = v.length
- break;
+ collection = v if v.class.name.start_with?("MetaSearch::Searches", "ActiveRecord::Relation", "Mongoid::Criteria")
+ end
end
- if v.respond_to?('total_entries')
- total_entries = v.total_entries
- current_page_entries = v.size
- break
- end
-end
+ # receiving correct current page entries count
+ current_page_entries = collection.size if collection.respond_to?('size')
-total_entries = current_page_entries if total_entries.nil?
+ # looking for the total entries count
+ total_entries = collection.length if collection.class.name.start_with?("Mongoid::Criteria")
+ total_entries = collection.total_entries if collection.respond_to?('total_entries')
+
+ # fallback to 1 page if total entries count not found
+ total_entries = current_page_entries if total_entries.nil?
+end
json.iTotalRecords current_page_entries
json.iTotalDisplayRecords total_entries
json.sEcho params["sEcho"].to_i
json.aaData do