Skip to content

Instantly share code, notes, and snippets.

@mrichie
Created May 30, 2012 08:24
Show Gist options
  • Save mrichie/2834520 to your computer and use it in GitHub Desktop.
Save mrichie/2834520 to your computer and use it in GitHub Desktop.
hook active_admin for support mongoid
require "active_admin"
require 'inherited_resources'
require "active_admin/resource_controller"
require 'mongoid'
module ActiveAdmin
module Mongoid
end
class Namespace
# Disable comments
def comments?
false
end
end
class Resource
def resource_table_name
resource_class.collection_name
end
end
ActiveAdmin::ResourceController
class ResourceController
# Use #desc and #asc for sorting.
def sort_order(chain)
params[:order] ||= active_admin_config.sort_order
table_name = active_admin_config.resource_table_name
if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
chain.send($2, $1)
else
chain # just return the chain
end
end
# Change filters
def search(chain)
q = clean_search_params(params[:q])
@search = active_admin_config.resource_class.new(q)
q.blank? ? chain : chain.mongo_search(q)
end
end
class FilterFormBuilder < FormBuilder
#Returns the default filter type for a given attribute
def default_input_type(method, options = {})
if column = column_for(method)
case column.type.to_s.downcase.to_sym
when :time, :date, :datetime
return :date_range
when :string
return :string
when :integer
return :select if reflection_for(method.to_s.gsub('_id','').to_sym)
return :numeric
when :float, :bigdecimal
return :numeric
when :object
return :select if reflection_for(method.to_s.gsub('_id','').to_sym)
end
end
if reflection = reflection_for(method)
return :select if reflection.macro == :referenced_in && !reflection.options[:polymorphic]
end
end
end
module Inputs
class FilterStringInput
def input_name
"#{super}"
end
end
class FilterDateRangeInput
def input_html_options(input_name = gt_input_name)
date_limit = input_name == gt_input_name ? "maxDate:'#F{$dp.$D(\\'q_#{lt_input_name}\\')||\\'2020-10-01\\'}'" :
"minDate:'#F{$dp.$D(\\'q_#{gt_input_name}\\')}',maxDate:'2020-10-01'"
current_value = @object.send(input_name)
{ :size => 12,
:class => "datepicker",
:max => 10,
:onclick =>"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm', #{date_limit}})",
:value => current_value.respond_to?(:strftime) ? current_value.strftime("%Y-%m-%d") : "" }
end
end
end
module Views
module Pages
class Base < Arbre::HTML::Document
# Renders the content for the footer
def build_footer
div :id => "footer" do
para "Powered by #{link_to("Edoctor", "http://www.edoctor.cn")}".html_safe
end
end
end
end
end
end
module ActiveAdmin::Mongoid::Document
extend ActiveSupport::Concern
module ClassMethods
MATCHERS = [
"all",
"and",
"exists",
"in",
"eq",
"gt",
"gte",
"in",
"lt",
"lte",
"ne",
"nin",
]
def mongo_search(query)
q = {}
query.each_pair do |k, v|
ext = k.to_s.split('_')
if MATCHERS.include?(ext.last)
e = ext.pop
e = e == "eq" ? "in" : e
field = ext.join('_')
if columns_hash[field].type == Object
if v.class == String
v = [BSON::ObjectId.from_string(v)]
elsif v.class == Array
v = v.collcect{|x| BSON::ObjectId.from_string(x) }
end
end
v = ["all", "in", "nin"].include?(e) ? v.to_a : v
q[field] = {"$#{e}" => v}
query.delete(k)
end
end
query.merge!(q)
where(query)
end
def msearch(*fields)
fields.each do |field|
col = columns_hash[field.to_s]
next if col.blank?
case col.type.to_s.downcase.to_sym
when :integer, :date, :datetime, :time
exts = [:gte, :lte]
when :string
exts = [:all]
when :object
exts = [:eq]
else
exts = [:all]
end
exts.each do |e|
self.class_eval %Q{
attr_accessor '#{col.name}_#{e}'
}
end
end
end
def content_columns
@content_columns ||= fields.map(&:second).select {|f| f.name !~ /(^_|^(created|updated)_at)/}
end
def columns_hash
fields
end
def columns
@columns ||= fields.map(&:second)
end
def reorder *args
scoped
end
end
def base
self.class
end
end
Mongoid::Document.send :include, ActiveAdmin::Mongoid::Document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment