Permalink
Browse files

Add support for loading based on cookie data. Version bump 1.2.1

  • Loading branch information...
1 parent df21975 commit 2baa85b280612f8f10e9f014239123157e385aaf @potatosalad committed Aug 3, 2011
Showing with 65 additions and 4 deletions.
  1. +2 −2 Gemfile.lock
  2. +62 −1 lib/mongoid/data_table/proxy.rb
  3. +1 −1 lib/mongoid/data_table/version.rb
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- mongoid-data_table (1.2.0)
+ mongoid-data_table (1.2.1)
kaminari (~> 0.12.4)
rails (>= 3.0.0)
@@ -54,7 +54,7 @@ GEM
mocha (0.9.12)
mongo (1.3.1)
bson (>= 1.3.1)
- mongoid (2.1.2)
+ mongoid (2.1.4)
activemodel (~> 3.0)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
@@ -1,12 +1,27 @@
+require 'json'
+require 'uri'
+
module Mongoid
module DataTable
class Proxy < ::Mongoid::Relations::Proxy
- attr_reader :klass, :controller, :options, :extension, :params, :criteria, :unscoped, :fields, :aliases, :locals
+ attr_reader :klass,
+ :controller,
+ :cookies,
+ :cookie,
+ :options,
+ :extension,
+ :params,
+ :criteria,
+ :unscoped,
+ :fields,
+ :aliases,
+ :locals
def initialize(klass, controller, options = {}, &block)
@klass = klass
@controller = controller
+ @cookies = @controller.send(:cookies)
@options = klass.data_table_options.merge(options)
@extension = block || options[:dataset] || klass.data_table_dataset || default_data_table_dataset
@@ -19,6 +34,9 @@ def initialize(klass, controller, options = {}, &block)
@locals = options[:locals] || {}
params[:iDisplayLength] = conditions.count if (params[:iDisplayLength].to_i rescue 0) == -1
+
+ @cookie = @cookies["SpryMedia_DataTables_#{options[:sInstance]}"]
+ @cookie = ::JSON.load(@cookie) if @cookie.is_a?(String)
end
def collection(force = false)
@@ -30,6 +48,49 @@ def reload
(@collection = nil).nil?
end
+ def load_cookie!
+ if cookie.present?
+
+ state_params = HashWithIndifferentAccess.new.tap do |h|
+
+ h[:iDisplayStart] = cookie['iStart']
+ h[:iDisplayLength] = cookie['iLength']
+ h[:sSearch] = cookie['sFilter']
+
+ h[:iSortCol_0], h[:sSortDir_0] = (cookie['aaSorting'].first rescue [ nil, nil ])
+
+ begin
+ cookie['aaSearchCols'].each_with_index do |(value,regex),index|
+ h[:"bRegex_#{index}"] = !regex
+ h[:"sSearch_#{index}"] = value
+ end
+ rescue
+ # do nothing
+ end
+
+ end
+
+ params.merge!(state_params)
+
+ @collection = nil
+
+ end
+ return self
+ end
+
+ def search_fields
+ return [] if cookie.blank?
+ hash = []
+ begin
+ cookie['aaSearchCols'].each_with_index do |(value,regex),index|
+ hash.push(value)
+ end
+ rescue
+ # do nothing
+ end
+ return hash
+ end
+
## pagination options ##
def current_page
@@ -1,5 +1,5 @@
module Mongoid
module DataTable
- VERSION = "1.2.0"
+ VERSION = "1.2.1"
end
end

0 comments on commit 2baa85b

Please sign in to comment.