Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sorting #51

Merged
merged 1 commit into from
Aug 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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