Sha256: d520ddbd8c19eab5dc8cdb007d17a180608dd71eeecce6d1e3e1a16ba4df6174

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module DataTable
  module Mongoid
    module ClassMethods
      def _find_objects params, fields, search_fields
        self.where(_where_conditions params[:sSearch], search_fields).
             order_by(_order_by_fields params, fields).
             page(_page params).
             per(_per_page params)
      end

      def _matching_count params, search_fields
        self.where(_where_conditions params[:sSearch], search_fields).count
      end

      def _where_conditions raw_query, search_fields
        query = _sanitize raw_query
        return if (query = _sanitize raw_query).blank?

        if search_fields.size == 1
          terms = query.split(/\s+/)

          if terms.size == 1
            {search_fields.first => /#{terms.first}/i}
          else
            {search_fields.first => {"$all" => terms.map {|term| /#{term}/i }}}
          end
        else
          {"$or" => search_fields.map {|field| {field => /#{query}/i} }}
        end
      end

      def _order_by_fields params, fields
        [fields[params[:iSortCol_0].to_i], params[:sSortDir_0]]
      end

      def _sanitize string
        string.strip.gsub(/([\^\\\/\.\+\*\?\|\[\]\(\)\$])/) { "\\#{$1}" }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_table-0.3.5 lib/data_table/mongoid.rb