Permalink
Browse files

Version bump 1.3.3, fixing issues with data tables 1.9

  • Loading branch information...
1 parent a097b73 commit 23283e37979c1b1ecbc8af3c20abd34c206623a3 @potatosalad committed Jan 6, 2012
Showing with 89 additions and 42 deletions.
  1. +88 −41 lib/mongoid/data_table/proxy.rb
  2. +1 −1 lib/mongoid/data_table/version.rb
@@ -10,6 +10,7 @@ class Proxy < ::Mongoid::Relations::Proxy
attr_reader :klass,
:controller,
:cookies,
+ :raw_cookie,
:cookie,
:cookie_prefix,
:options,
@@ -28,7 +29,7 @@ def initialize(klass, controller, options = {}, &block)
@options = klass.data_table_options.merge(options)
@extension = block || options[:dataset] || klass.data_table_dataset || default_data_table_dataset
- @params = options[:params] || (controller.params.dup rescue {})
+ @params = (options[:params] || controller.params || {}).dup.symbolize_keys
@criteria = options[:criteria] || klass.criteria
@unscoped = options[:unscoped] || klass.unscoped
@fields = options[:fields] || klass.data_table_fields
@@ -50,56 +51,102 @@ def reload
(@collection = nil).nil?
end
- def load_cookie!(sInstance = nil)
+ def cookie(sInstance = nil)
+ @cookie ||= load_cookie!(sInstance)
+ end
+
+ def cookie!(sInstance = nil)
+ @cookie = nil
sInstance ||= options[:sInstance]
- @cookie = @cookies[cookie_name(sInstance)]
+ @raw_cookie = cookies[cookie_name(sInstance)]
begin
- @cookie = ::JSON.load(cookie) if cookie.is_a?(String)
- rescue
- @cookie = nil
- @cookies.delete cookie_name(sInstance)
- return self
+ @cookie = ::JSON.load(raw_cookie) if raw_cookie.kind_of?(String)
+ rescue JSON::ParserError
+ cookies.delete cookie_name(sInstance)
+ @cookie = {}
+ ensure
+ (@cookie ||= {}).symbolize_keys!
end
- if cookie.present?
+ end
- state_params = HashWithIndifferentAccess.new.tap do |h|
+ def load_cookie!(sInstance = nil)
+ tap do
- h[:iDisplayStart] = cookie['iStart']
- h[:iDisplayLength] = cookie['iLength']
- h[:sSearch] = cookie['sFilter']
+ cookie!(sInstance)
- h[:iSortCol_0], h[:sSortDir_0] = (cookie['aaSorting'].first rescue [ nil, nil ])
+ if cookie.present? and cookie.kind_of?(Hash)
+ load_defaults!(cookie)
+ elsif (defaults = options[:defaults]).present? and defaults.kind_of?(Hash)
+ load_defaults!(defaults)
+ end
- begin
- (cookie['aaSearchCols'] || cookie['aoSearchCols']).each_with_index do |(value,regex),index|
- h[:"bRegex_#{index}"] = !regex
- h[:"sSearch_#{index}"] = value
- end
- rescue
- # do nothing
+ end
+ end
+
+ def load_defaults!(defaults = {})
+ tap do
+ state_params = Hash.new.tap do |hash|
+ hash[:iDisplayStart] = defaults[:iStart] if defaults.has_key?(:iStart)
+ hash[:iDisplayLength] = defaults[:iLength] if defaults.has_key?(:iLength)
+ hash[:sSearch] = defaults[:sFilter] if defaults.has_key?(:sFilter)
+
+ if defaults.has_key?(:aaSorting)
+ sorting = [*defaults[:aaSorting]].first
+
+ hash[:iSortCol_0] = [*sorting][0]
+ hash[:sSortDir_0] = [*sorting][1]
end
+ if defaults.has_key?(:aoSearchCols)
+ [*defaults[:aoSearchCols]].each_with_index do |oSearch, index|
+ if oSearch.kind_of?(Hash)
+ oSearch.symbolize_keys!
+ hash[:"bRegex_#{index}"] = oSearch[:bRegex]
+ hash[:"sSearch_#{index}"] = URI.decode(oSearch[:sSearch])
+ else
+ hash[:"sSearch_#{index}"] = URI.decode(oSearch.to_s)
+ end
+ end
+ elsif defaults.has_key?(:aaSearchCols)
+ begin
+ [*defaults[:aaSearchCols]].each_with_index do |(value,regex),index|
+ hash[:"bRegex_#{index}"] = !regex
+ hash[:"sSearch_#{index}"] = URI.decode(value)
+ end
+ rescue
+ # do nothing
+ end
+ end
end
params.merge!(state_params)
@collection = nil
-
end
- return self
end
def search_fields
- return [] if cookie.blank?
- array = []
- begin
- (cookie['aaSearchCols'] || cookie['aoSearchCols']).each_with_index do |(value,regex),index|
- array.push(URI.decode(value.to_s))
+ return [] if cookie.blank? or not cookie.kind_of?(Hash)
+ [].tap do |array|
+
+ if cookie.has_key?(:aoSearchCols)
+ [*cookie[:aoSearchCols]].each_with_index do |oSearch, index|
+ if oSearch.kind_of?(Hash)
+ oSearch.symbolize_keys!
+ array.push(URI.decode(oSearch[:sSearch].to_s))
+ end
+ end
+ else
+ begin
+ [*cookie[:aaSearchCols]].each_with_index do |(value,regex),index|
+ array.push(URI.decode(value.to_s))
+ end
+ rescue
+ # do nothing
+ end
end
- rescue
- # do nothing
+
end
- return array
end
## pagination options ##
@@ -144,18 +191,18 @@ def to_json(*args, &inline_block)
protected
def order_by_conditions
- order_params = params.dup.select { |k,v| k =~ /(i|s)Sort(Col|Dir)_\d+/ }
+ order_params = params.dup.select { |k,v| k.to_s =~ /(i|s)Sort(Col|Dir)_\d+/ }
return options[:order_by] || [] if order_params.blank?
- order_params.select { |k,v| k =~ /iSortCol_\d+/ }.sort_by(&:first).map do |col,field|
- i = /iSortCol_(\d+)/.match(col)[1]
- [ fields[field.to_i], order_params["sSortDir_#{i}"] || :asc ]
+ order_params.select { |k,v| k.to_s =~ /iSortCol_\d+/ }.sort_by(&:first).map do |col,field|
+ i = /iSortCol_(\d+)/.match(col.to_s)[1]
+ [ fields[field.to_i], order_params[:"sSortDir_#{i}"] || :asc ]
end
end
def filter_conditions
return unless (query = params[:sSearch]).present?
- b_regex = Mongoid::Serialization.mongoize(params["bRegex"], Boolean)
+ b_regex = Mongoid::Serialization.mongoize(params[:bRegex], Boolean)
{
"$or" => klass.data_table_searchable_fields.map { |field|
@@ -165,15 +212,15 @@ def filter_conditions
end
def filter_field_conditions
- params.dup.select { |k,v| k =~ /sSearch_\d+/ }.inject({}) do |h,(k,v)|
- i = /sSearch_(\d+)/.match(k)[1]
+ params.dup.select { |k,v| k.to_s =~ /sSearch_\d+/ }.inject({}) do |h,(k,v)|
+ i = /sSearch_(\d+)/.match(k.to_s)[1]
field_name = fields[i.to_i]
#field = klass.fields.dup[field_name]
#field_type = field.respond_to?(:type) ? field.type : Object
- query = params["sSearch_#{i}"]
- b_regex = Mongoid::Serialization.mongoize(params["bRegex_#{i}"], Boolean)
+ query = params[:"sSearch_#{i}"]
+ b_regex = Mongoid::Serialization.mongoize(params[:"bRegex_#{i}"], Boolean)
h[field_name] = (b_regex === true) ? data_table_regex(query) : query if query.present?
h
@@ -215,7 +262,7 @@ def data_table_regex(query)
def method_missing(method, *args, &block) #:nodoc:
collection.send(method, *args, &block)
end
-1
+
end
end
end
@@ -1,5 +1,5 @@
module Mongoid
module DataTable
- VERSION = "1.3.2"
+ VERSION = "1.3.3"
end
end

0 comments on commit 23283e3

Please sign in to comment.