Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hack search capabilities in.

1. detect LIKE condition generated by the main_controller
2. extract the search string and rebuild Mongoid query with it
  • Loading branch information...
commit 789bcbb7095aaed35cf3884c2f2f477d9c8b90cd 1 parent 1cea49e
Marius Seritan authored committed
Showing with 34 additions and 20 deletions.
  1. +34 −20 lib/rails_admin/adapters/mongoid.rb
View
54 lib/rails_admin/adapters/mongoid.rb
@@ -43,7 +43,7 @@ def first(options = {})
end
def all(options = {})
- Array(criteria_for_options(options))
+ criteria_for_options(options)
end
def paginated(options = {})
@@ -127,22 +127,24 @@ def associations
end
def properties
- model.fields.map do |name,field|
- ar_type = case field.type.to_s
- when "String"
- :string
- when "Integer"
- :integer
- when "Time"
- :datetime
- when "Float"
- :float
- when "Array"
- :string
- else
- # this will likely bomb, will fix as it does
- field.type
- end
+ @properties if @properties
+ @properties = model.fields.map do |name,field|
+ ar_type = case field.type.to_s
+ when "String"
+ :string
+ when "Integer"
+ :integer
+ when "DateTime"
+ :datetime
+ when "Time"
+ :datetime
+ when "Float"
+ :float
+ when "Array"
+ :string
+ else
+ raise "Need to map field #{field.type.to_s} for field name #{name} in #{model.inspect}"
+ end
{
:name => field.name.to_sym,
@@ -205,16 +207,28 @@ def association_child_key_lookup(association)
end
private
+
+ def criteria_for_search(options)
+ # get the wanted string as the last argument and remove the percent signs
+ wanted = Regexp.new(options[:conditions][1][1..-2])
+ model.any_of(properties.select{|p| p[:type]==:string}.map{|p| {p[:name].to_sym=>wanted}})
+ end
def criteria_for_options(options)
sort = options.delete(:sort)
sort_reverse = options.delete(:sort_reverse)
+ # detect a LIKE condition
+ criteria = if options[:conditions][0][' LIKE ']
+ criteria_for_search(options)
+ else
+ model.where(options)
+ end
criteria = if sort && sort_reverse
- model.where(options).desc(sort)
+ criteria.desc(sort)
elsif sort
- model.where(options).asc(sort)
+ criteria.asc(sort)
else
- model.where(options)
+ criteria
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.