Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

extended search functionality #2

Closed
wants to merge 5 commits into from

3 participants

István Demeter Lukas Oberhuber Peter Vandenberk
István Demeter

No description provided.

Istvan DEMETER added some commits
Lukas Oberhuber
Owner

@qw3r Sorry for lack of response. I've been waiting for this pull request to be merged into the upstream repo, and there's a pull request there that implements almost exactly the same things as you have. But given these pull requests are piling up, might be best to wait a bit. Here is the pull request: elia#17

Peter Vandenberk
Owner

@qw3r - as a general comment, I'd argue that renaming classes (and hence files that are under git control) should be done as a separate pull request, as it is now very hard to review the entire diff, forcing a commit-by-commit review.

A separate pull request for the renaming with no actual functional changes would be better, IMO... thanks!

István Demeter

Hello guys,

sorry for not replying until now.
thanks you for your notices, comments on the PR i very appreciate it. I have to work on other things, but i'll keep an eye on this great project, which helped me a lot. Thanks!

István Demeter qw3r closed this
Lukas Oberhuber
Owner

@qw3r No problem. Thanks for contributing. It really looks like the pull requests got stuck over at the original.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 27, 2012
  1. extending search

    Istvan DEMETER authored
  2. dynamic fields; rename adapter class; bump up the version

    Istvan DEMETER authored
  3. add operator $in

    Istvan DEMETER authored
  4. simplifications

    Istvan DEMETER authored
  5. clean up code

    Istvan DEMETER authored
This page is out of date. Refresh to see the latest.
1  .gitignore
View
@@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
+*.sublime*
2  lib/active_admin/mongoid.rb
View
@@ -11,7 +11,7 @@ class << self
def setup *args, &block
setup_without_mongoid *args, &block
- require 'active_admin/mongoid/adaptor'
+ require 'active_admin/mongoid/adapter'
require 'active_admin/mongoid/comments'
require 'active_admin/mongoid/filter_form_builder'
require 'active_admin/mongoid/resource'
53 lib/active_admin/mongoid/adapter.rb
View
@@ -0,0 +1,53 @@
+module ActiveAdmin
+ module Mongoid
+ module Adapter
+ class Search
+ attr_reader :base, :query, :query_hash, :search_params
+
+ def initialize(object, search_params = {})
+ @base = object
+ @search_params = search_params
+ @query = @base.where(build_query(search_params))
+ end
+
+ def respond_to?(method_id)
+ @query.send(:respond_to?, method_id)
+ end
+
+ def method_missing(method_id, *args, &block)
+ is_available?(method_id) ? @search_params[method_id.to_s] : @query.send(method_id, *args, &block)
+ end
+
+ private
+
+ def is_available?(method_id)
+ method_id.to_s =~ /_(gte?|lte?|eq|ne|in|contains)\Z/
+ end
+
+ def build_query(search_params)
+ query = {}
+ search_params.each do |key, value|
+ case key.to_s
+ when /\A(.*)_([^_]+)\Z/
+ field, operator = [$1, $2].map!(&:to_sym)
+
+ q = case operator
+ when :contains
+ {field => Regexp.new(Regexp.escape(value), Regexp::IGNORECASE)}
+ when :eq
+ {field => value}
+ when :lt, :lte, :gt, :gte, :in
+ {field.send(operator) => value}
+ else
+ raise "Unhandled conditional operator: #{operator}"
+ end
+ query.merge! q
+ end
+ end
+ query
+ end
+
+ end
+ end
+ end
+end
53 lib/active_admin/mongoid/adaptor.rb
View
@@ -1,53 +0,0 @@
-module ActiveAdmin
- module Mongoid
- module Adaptor
- class Search
- attr_reader :base, :query, :query_hash, :search_params
-
- def initialize(object, search_params = {})
- @base = object
- @search_params = search_params
- @query_hash = get_query_hash(search_params)
- @query = @base.where(@query_hash)
- end
-
- def respond_to?(method_id)
- @query.send(:respond_to?, method_id)
- end
-
- def method_missing(method_id, *args, &block)
- if is_query(method_id)
- @search_params[method_id.to_s]
- else
- @query.send(method_id, *args, &block)
- end
- end
-
- private
-
- def is_query(method_id)
- method_id.to_s =~ /_contains$/
- end
-
- def get_query_hash(search_params)
- searches = search_params.map do|k, v|
- mongoidify_search(k,v)
- end
- Hash[searches]
- end
-
- def mongoidify_search(k, v)
- if k =~ /_contains$/
- [get_attribute(k), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)]
- else
- [k, v]
- end
- end
-
- def get_attribute(k)
- k.match(/^(.*)_contains$/)[1]
- end
- end
- end
- end
-end
10 lib/active_admin/mongoid/filter_form_builder.rb
View
@@ -3,17 +3,17 @@ def default_input_type(method, options = {})
if column = column_for(method)
case column.type.name.downcase.to_sym
when :date, :datetime
- return :date_range
+ :date_range
when :string, :text
- return :string
+ :string
when :integer
return :select if reflection_for(method.to_s.gsub('_id','').to_sym)
- return :numeric
+ :numeric
when :float, :decimal
- return :numeric
+ :numeric
end
else # dirty but allows to create filters for hashes
- return :string
+ :string
end
end
2  lib/active_admin/mongoid/resource.rb
View
@@ -22,7 +22,7 @@ def sort_order(chain)
end
def search(chain)
- @search = ActiveAdmin::Mongoid::Adaptor::Search.new(chain, clean_search_params(params[:q]))
+ @search = ActiveAdmin::Mongoid::Adapter::Search.new(chain, clean_search_params(params[:q]))
end
end
2  lib/active_admin/mongoid/version.rb
View
@@ -1,5 +1,5 @@
module ActiveAdmin
module Mongoid
- VERSION = '0.0.1'
+ VERSION = '0.0.2'
end
end
Something went wrong with that request. Please try again.