Skip to content

Commit

Permalink
Merge pull request #17 from Ostrzy/filter_support
Browse files Browse the repository at this point in the history
Improved filter support
  • Loading branch information
elia committed Feb 16, 2013
2 parents 3e974fb + c46d451 commit da8a664
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 14 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ ActiveAdmin hacks to support Mongoid.
Some ActiveAdmin features are disabled:

- comments
- sidebar filters

For more on Mongoid support in ActiveAdmin see [this issue](https://github.com/gregbell/active_admin/issues/26).

Expand Down
5 changes: 2 additions & 3 deletions activeadmin-mongoid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.version = ActiveAdmin::Mongoid::VERSION
gem.license = 'MIT'
gem.add_runtime_dependency 'mongoid', '>= 2.0'

gem.add_runtime_dependency 'mongoid', '>= 2.4'
gem.add_runtime_dependency 'activeadmin', '~> 0.4'
gem.add_runtime_dependency 'meta_search', '>= 1.1.0.pre'
gem.add_runtime_dependency 'sass-rails', ['~> 3.1', '>= 3.1.4']
end
8 changes: 5 additions & 3 deletions lib/active_admin/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
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/filter_form_builder'
require 'active_admin/mongoid/resource'
require 'active_admin/mongoid/document'
require 'active_admin/mongoid/helpers/collection'
Expand Down
67 changes: 67 additions & 0 deletions lib/active_admin/mongoid/adaptor.rb
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
33 changes: 33 additions & 0 deletions lib/active_admin/mongoid/filter_form_builder.rb
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
12 changes: 5 additions & 7 deletions lib/active_admin/mongoid/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ class ActiveAdmin::Resource
def resource_table_name
resource_class.collection_name
end

def mongoid_per_page
per_page
end
end

ActiveAdmin::ResourceController # autoload
class ActiveAdmin::ResourceController
before_filter :skip_sidebar!

protected

def skip_sidebar!
@skip_sidebar = true
end

# Use #desc and #asc for sorting.
def sort_order(chain)
params[:order] ||= active_admin_config.sort_order
Expand All @@ -29,8 +28,7 @@ def sort_order(chain)
end
end

# Disable filters
def search(chain)
chain
@search = ActiveAdmin::Mongoid::Adaptor::Search.new(chain, clean_search_params(params[:q]), active_admin_config.mongoid_per_page, params[:page])
end
end

0 comments on commit da8a664

Please sign in to comment.