Created — forked from ivanyv/base_model.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View active_admin_mongoid_patch.rb
1 2 3 4 5 6 7 8
# app/models/company.rb
 
class Company
include Mongoid::Document
include ActiveAdmin::Mongoid::Patches
 
field ...
end
View active_admin_mongoid_patch.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# app/models/active_admin/mongoid/patches.rb
 
require 'ostruct'
 
module ActiveAdmin
module Mongoid
COLUMN_TYPES = { Bignum => :integer, Array => :string }
 
module Patches
def self.included(base)
raise 'Include ActiveAdmin::Mongoid::Patches after Mongoid::Document' unless base.respond_to?(:collection_name)
base.extend ClassPatches
end
 
def column_for_attribute(attr)
self.class.columns.detect { |c| c.name == attr.to_s }
end
 
module ClassPatches
HIDDEN_COLUMNS = %w(_id _type)
 
def content_columns
fields.map do |name, field|
next if HIDDEN_COLUMNS.include?(name)
OpenStruct.new.tap do |c|
c.name = field.name
c.type = ActiveAdmin::Mongoid::COLUMN_TYPES[field.type] || field.type.to_s.downcase.to_sym
end
end.compact
end
 
def columns
content_columns
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.