Permalink
Browse files

extending search

  • Loading branch information...
1 parent 60d7fe0 commit 88bd49b9bb99d99614de6696f1293144d82c4ece Istvan DEMETER committed Jul 27, 2012
Showing with 22 additions and 19 deletions.
  1. +1 −0 .gitignore
  2. +21 −19 lib/active_admin/mongoid/adaptor.rb
View
@@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
+*.sublime*
@@ -6,9 +6,7 @@ class Search
def initialize(object, search_params = {})
@base = object
- @search_params = search_params
- @query_hash = get_query_hash(search_params)
- @query = @base.where(@query_hash)
+ @query = @base.where(build_query(search_params))
end
def respond_to?(method_id)
@@ -25,28 +23,32 @@ def method_missing(method_id, *args, &block)
private
- def is_query(method_id)
- method_id.to_s =~ /_contains$/
+ def available_methods
+ %w(gte lte gt lt eq ne contains)
end
- def get_query_hash(search_params)
- searches = search_params.map do|k, v|
- mongoidify_search(k,v)
- end
- Hash[searches]
- end
+ def build_query(search_params)
+ query = {}
+ search_params.each do |k, v|
+ case k.to_s
+ when /\A(.*)_([^_]+)\Z/
+ method = $1.to_sym
- def mongoidify_search(k, v)
- if k =~ /_contains$/
- [get_attribute(k), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)]
- else
- [k, v]
+ case $2
+ when 'contains'
+ query.merge!(method => Regexp.new(Regexp.escape("#{value}"), Regexp::IGNORECASE))
+
+ when /\A(lte?|gte?|eq)\Z/
+ query.merge!(method.send($1) => value)
+
+ else
+ raise "unhandled conditional operator: #{$2}"
+ end
+ end
end
+ query
end
- def get_attribute(k)
- k.match(/^(.*)_contains$/)[1]
- end
end
end
end

0 comments on commit 88bd49b

Please sign in to comment.