From 0ab0890b234d8b1fbfa0dfbef13039cfddbf72c6 Mon Sep 17 00:00:00 2001 From: Bartosz Kopinski Date: Mon, 22 Jul 2013 17:40:53 +0200 Subject: [PATCH] Add sorting, fixes #42 - no-collection case - filtering for sorting - conflict with simple_form --- lib/active_admin/mongoid/document.rb | 24 +++++++++++------------- lib/meta_search/searches/mongoid.rb | 19 +++++++------------ 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/active_admin/mongoid/document.rb b/lib/active_admin/mongoid/document.rb index f9eda40..5b4f6a4 100644 --- a/lib/active_admin/mongoid/document.rb +++ b/lib/active_admin/mongoid/document.rb @@ -5,14 +5,6 @@ module ActiveAdmin::Mongoid::Document extend ActiveSupport::Concern - # INSTANCE METHODS - - # Returns the column object for the named attribute. - def column_for_attribute(name) - self.class.columns_hash[name.to_s] - end - - # PROXY CLASSES @@ -45,6 +37,8 @@ def quote_column_name name # CLASS METHODS included do + include MetaSearch::Searches::Mongoid + unless respond_to? :primary_key class << self attr_accessor :primary_key @@ -60,11 +54,11 @@ module ClassMethods # Metasearch def joins_values *args - scoped + criteria end - def group_by *args - scoped + def group_by *args, &block + criteria end def ransack *args @@ -119,8 +113,12 @@ def columns_hash - def reorder *args - scoped + def reorder sorting + return unscoped if sorting.blank? + options = sorting.split(/ |\./) + options.shift if options.count == 3 + field, order = *options + unscoped.order_by(field => order) end def connection diff --git a/lib/meta_search/searches/mongoid.rb b/lib/meta_search/searches/mongoid.rb index a03e433..7e659b7 100644 --- a/lib/meta_search/searches/mongoid.rb +++ b/lib/meta_search/searches/mongoid.rb @@ -22,17 +22,17 @@ def build when :does_not_equal, :ne, :not_eq @relation = relation.where(field.to_sym.ne => value) when :contains, :like, :matches - @relation = relation.where(field => /#{value}/) + @relation = relation.where(field => /#{value}/i) when :does_not_contain, :nlike, :not_matches - @relation = relation.where(field.to_sym.not => /#{value}/) + @relation = relation.where(field.to_sym.not => /#{value}/i) when :starts_with, :sw - @relation = relation.where(field.to_sym => /\A#{Regexp.quote(value)}/) + @relation = relation.where(field.to_sym => /\A#{Regexp.quote(value)}/i) when :does_not_start_with, :dnsw - @relation = relation.where(field.to_sym.not => /\A#{Regexp.quote(value)}/) + @relation = relation.where(field.to_sym.not => /\A#{Regexp.quote(value)}/i) when :ends_with, :ew - @relation = relation.where(field.to_sym => /#{Regexp.quote(value)}\z/) + @relation = relation.where(field.to_sym => /#{Regexp.quote(value)}\z/i) when :does_not_end_with, :dnew - @relation = relation.where(field.to_sym.not => /#{Regexp.quote(value)}\z/) + @relation = relation.where(field.to_sym.not => /#{Regexp.quote(value)}\z/i) when :greater_than, :gt @relation = relation.where(field.to_sym.gt => value) when :less_than, :lt @@ -92,9 +92,7 @@ def method_missing name, *args, &block end def metasearch_regexp - # field_names = klass.content_columns.map(&:name) field_names = klass.fields.map(&:second).map(&:name) - conditions = MetaSearch::DEFAULT_WHERES.map {|condition| condition[0...-1]} # pop tail options /\A(#{field_names.join('|')})_(#{conditions.join('|')})\z/ @@ -109,10 +107,7 @@ module ClassMethods def metasearch(params = nil, options = nil) options ||= {} params ||= {} - MongoidSearchBuilder.new(self, params, options).build - # @metasearch_query - # raise [params, options].inspect unless [options, params].all?(&:empty?) - # scoped + MongoidSearchBuilder.new(criteria, params, options).build end alias_method :search, :metasearch unless respond_to?(:search) end