-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Ostrzy/filter_support
Improved filter support
- Loading branch information
Showing
6 changed files
with
112 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module ActiveAdmin | ||
module Mongoid | ||
module Adaptor | ||
class Search | ||
attr_reader :base, :query, :query_hash, :search_params | ||
|
||
def initialize(object, search_params = {}, per_page = 30, page = 1) | ||
@base = object | ||
@search_params = search_params | ||
@query_hash = get_query_hash(search_params) | ||
vpage = page.to_i > 0 ? page.to_i : 1 | ||
@query = @base.where(@query_hash).limit(per_page).skip(per_page * (vpage - 1)) | ||
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|eq|in|gt|lt|gte|lte)$/ | ||
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) | ||
case k | ||
when /_contains$/ | ||
[get_attribute(k, '_contains'), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)] | ||
when /_eq$/ | ||
[get_attribute(k, '_eq'), v] | ||
when /_in$/ | ||
[get_attribute(k, '_in').to_sym.in, v] | ||
when /_gt$/ | ||
[get_attribute(k, "_gt").to_sym.gt, v] | ||
when /_lt$/ | ||
[get_attribute(k, "_lt").to_sym.lt, v] | ||
when /_gte$/ | ||
[get_attribute(k, "_gte").to_sym.gte, v] | ||
when /_lte$/ | ||
[get_attribute(k, "_lte").to_sym.lte, v] | ||
else | ||
[k, v] | ||
end | ||
end | ||
|
||
def get_attribute(k, postfix) | ||
k.match(/^(.*)#{postfix}$/)[1] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class ActiveAdmin::FilterFormBuilder | ||
def default_input_type(method, options = {}) | ||
if column = column_for(method) | ||
case column.type.name.downcase.to_sym | ||
when :date, :datetime, :time | ||
return :date_range | ||
when :string, :text, :object | ||
return :string | ||
when :integer | ||
return :select if reflection_for(method.to_s.gsub('_id','').to_sym) | ||
return :numeric | ||
when :float, :decimal | ||
return :numeric | ||
end | ||
elsif is_association?(method) | ||
return :select | ||
else # dirty but allows to create filters for hashes | ||
return :string | ||
end | ||
end | ||
|
||
def is_association?(method) | ||
@object.klass.associations.to_a.map(&:first).include?(method.to_s) | ||
end | ||
|
||
def column_for(method) | ||
@object.klass.fields[method.to_s] if @object.klass.respond_to?(:fields) | ||
end | ||
|
||
def reflection_for(method) | ||
@object.klass.reflect_on_association(method) if @object.klass.respond_to?(:reflect_on_association) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters