Skip to content

Commit 0ab0890

Browse files
Bartosz Kopinskielia
authored andcommitted
Add sorting, fixes #42
- no-collection case - filtering for sorting - conflict with simple_form
1 parent fffdcfb commit 0ab0890

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

lib/active_admin/mongoid/document.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ module ActiveAdmin::Mongoid::Document
55
extend ActiveSupport::Concern
66

77

8-
# INSTANCE METHODS
9-
10-
# Returns the column object for the named attribute.
11-
def column_for_attribute(name)
12-
self.class.columns_hash[name.to_s]
13-
end
14-
15-
168

179

1810
# PROXY CLASSES
@@ -45,6 +37,8 @@ def quote_column_name name
4537
# CLASS METHODS
4638

4739
included do
40+
include MetaSearch::Searches::Mongoid
41+
4842
unless respond_to? :primary_key
4943
class << self
5044
attr_accessor :primary_key
@@ -60,11 +54,11 @@ module ClassMethods
6054
# Metasearch
6155

6256
def joins_values *args
63-
scoped
57+
criteria
6458
end
6559

66-
def group_by *args
67-
scoped
60+
def group_by *args, &block
61+
criteria
6862
end
6963

7064
def ransack *args
@@ -119,8 +113,12 @@ def columns_hash
119113

120114

121115

122-
def reorder *args
123-
scoped
116+
def reorder sorting
117+
return unscoped if sorting.blank?
118+
options = sorting.split(/ |\./)
119+
options.shift if options.count == 3
120+
field, order = *options
121+
unscoped.order_by(field => order)
124122
end
125123

126124
def connection

lib/meta_search/searches/mongoid.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ def build
2222
when :does_not_equal, :ne, :not_eq
2323
@relation = relation.where(field.to_sym.ne => value)
2424
when :contains, :like, :matches
25-
@relation = relation.where(field => /#{value}/)
25+
@relation = relation.where(field => /#{value}/i)
2626
when :does_not_contain, :nlike, :not_matches
27-
@relation = relation.where(field.to_sym.not => /#{value}/)
27+
@relation = relation.where(field.to_sym.not => /#{value}/i)
2828
when :starts_with, :sw
29-
@relation = relation.where(field.to_sym => /\A#{Regexp.quote(value)}/)
29+
@relation = relation.where(field.to_sym => /\A#{Regexp.quote(value)}/i)
3030
when :does_not_start_with, :dnsw
31-
@relation = relation.where(field.to_sym.not => /\A#{Regexp.quote(value)}/)
31+
@relation = relation.where(field.to_sym.not => /\A#{Regexp.quote(value)}/i)
3232
when :ends_with, :ew
33-
@relation = relation.where(field.to_sym => /#{Regexp.quote(value)}\z/)
33+
@relation = relation.where(field.to_sym => /#{Regexp.quote(value)}\z/i)
3434
when :does_not_end_with, :dnew
35-
@relation = relation.where(field.to_sym.not => /#{Regexp.quote(value)}\z/)
35+
@relation = relation.where(field.to_sym.not => /#{Regexp.quote(value)}\z/i)
3636
when :greater_than, :gt
3737
@relation = relation.where(field.to_sym.gt => value)
3838
when :less_than, :lt
@@ -92,9 +92,7 @@ def method_missing name, *args, &block
9292
end
9393

9494
def metasearch_regexp
95-
# field_names = klass.content_columns.map(&:name)
9695
field_names = klass.fields.map(&:second).map(&:name)
97-
9896
conditions = MetaSearch::DEFAULT_WHERES.map {|condition| condition[0...-1]} # pop tail options
9997

10098
/\A(#{field_names.join('|')})_(#{conditions.join('|')})\z/
@@ -109,10 +107,7 @@ module ClassMethods
109107
def metasearch(params = nil, options = nil)
110108
options ||= {}
111109
params ||= {}
112-
MongoidSearchBuilder.new(self, params, options).build
113-
# @metasearch_query
114-
# raise [params, options].inspect unless [options, params].all?(&:empty?)
115-
# scoped
110+
MongoidSearchBuilder.new(criteria, params, options).build
116111
end
117112
alias_method :search, :metasearch unless respond_to?(:search)
118113
end

0 commit comments

Comments
 (0)