Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Searches working in ActiveAdmin now.

  • Loading branch information...
commit 530fbb3bfb50c4fc5cb12d86808b190f4cdfa879 1 parent 1bff52f
@lukaso lukaso authored
View
8 lib/active_admin/mongoid.rb
@@ -3,16 +3,18 @@
module ActiveAdmin
module Mongoid
end
-
+
class << self
alias setup_without_mongoid setup
-
+
# Load monkey patches *after* the setup process
def setup *args, &block
setup_without_mongoid *args, &block
-
+
+ require 'active_admin/mongoid/adaptor'
require 'active_admin/mongoid/comments'
require 'active_admin/mongoid/form_builder'
+ require 'active_admin/mongoid/filter_form_builder'
require 'active_admin/mongoid/resource'
require 'active_admin/mongoid/document'
end
View
54 lib/active_admin/mongoid/adaptor.rb
@@ -0,0 +1,54 @@
+module ActiveAdmin
+ module Mongoid
+ module Adaptor
+ class Search
+ # include Naming
+
+ attr_reader :base, :query, :query_hash, :search_params
+
+ def initialize(object, search_params = {})
+ Rails.logger.debug "********search new: #{search_params}, get_query_hash: #{get_query_hash(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)
+ Rails.logger.debug "********method_missing: #{method_id}::#{args}"
+
+ if is_query(method_id)
+ return @search_params[method_id.to_s]
+ end
+ query.send(method_id, *args, &block)
+ 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)
+ return [get_attribute(k), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)] if k =~ /_contains$/
+ [k, v]
+ end
+
+ def get_attribute(k)
+ k.match(/^(.*)_contains$/)[1]
+ end
+ end
+ end
+ end
+end
View
26 lib/active_admin/mongoid/filter_form_builder.rb
@@ -0,0 +1,26 @@
+class ActiveAdmin::FilterFormBuilder
+ def default_input_type(method, options = {})
+ if column = column_for(method)
+ case column.type.name.downcase.to_sym
+ when :date, :datetime
+ return :date_range
+ when :string, :text
+ return :string
+ when :integer
+ return :select if reflection_for(method.to_s.gsub('_id','').to_sym)
+ return :numeric
+ when :float, :decimal
+ return :numeric
+ end
+ end
+ end
+
+ def column_for(method)
+ @object.fields[method.to_s] if @object.respond_to?(:fields)
+ end
+
+ def reflection_for(method)
+ Rails.logger.debug "*********** reflects: #{@object.class.respond_to?(:reflect_on_association)}"
+ @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association)
+ end
+end
View
10 lib/active_admin/mongoid/resource.rb
@@ -4,12 +4,12 @@
ActiveAdmin::Resource # autoload
class ActiveAdmin::Resource
def resource_table_name
- resource.collection_name
+ resource_class.collection_name
end
# Disable filters
- def add_default_sidebar_sections
- end
+ # def add_default_sidebar_sections
+ # end
end
ActiveAdmin::ResourceController # autoload
@@ -25,8 +25,8 @@ def sort_order(chain)
end
end
- # Disable filters
def search(chain)
- chain
+ @search = ActiveAdmin::Mongoid::Adaptor::Search.new(chain, clean_search_params(params[:q]))
end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.