Skip to content

Commit

Permalink
Add sorting, fixes #42
Browse files Browse the repository at this point in the history
- no-collection case
- filtering for sorting
- conflict with simple_form
  • Loading branch information
Bartosz Kopinski authored and elia committed Aug 22, 2013
1 parent fffdcfb commit 0ab0890
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
24 changes: 11 additions & 13 deletions lib/active_admin/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 7 additions & 12 deletions lib/meta_search/searches/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand All @@ -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
Expand Down

0 comments on commit 0ab0890

Please sign in to comment.