Skip to content
Browse files

Added proper support for associations and probably for mongoid 2.4

  • Loading branch information...
1 parent 00ac376 commit e93155cd1bc7b7fcb252e5a1d208a7c818c83542 @Ostrzy committed Jul 28, 2012
Showing with 17 additions and 8 deletions.
  1. +8 −5 lib/active_admin/mongoid/adaptor.rb
  2. +9 −3 lib/active_admin/mongoid/filter_form_builder.rb
View
13 lib/active_admin/mongoid/adaptor.rb
@@ -26,7 +26,8 @@ def method_missing(method_id, *args, &block)
private
def is_query(method_id)
- method_id.to_s =~ /_contains$/
+ method_id.to_s =~ /_contains$/ ||
+ method_id.to_s =~ /_eq$/
end
def get_query_hash(search_params)
@@ -38,16 +39,18 @@ def get_query_hash(search_params)
def mongoidify_search(k, v)
if k =~ /_contains$/
- [get_attribute(k), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)]
+ [get_attribute(k, '_contains'), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)]
+ elsif k =~ /_eq$/
+ [get_attribute(k, '_eq'), v]
else
[k, v]
end
end
- def get_attribute(k)
- k.match(/^(.*)_contains$/)[1]
+ def get_attribute(k, postfix)
+ k.match(/^(.*)#{postfix}$/)[1]
end
end
end
end
-end
+end
View
12 lib/active_admin/mongoid/filter_form_builder.rb
@@ -12,16 +12,22 @@ def default_input_type(method, options = {})
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.fields[method.to_s] if @object.respond_to?(:fields)
+ @object.klass.fields[method.to_s] if @object.klass.respond_to?(:fields)
end
def reflection_for(method)
- @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association)
+ @object.klass.reflect_on_association(method) if @object.klass.respond_to?(:reflect_on_association)
end
-end
+end

0 comments on commit e93155c

Please sign in to comment.
Something went wrong with that request. Please try again.