-
-
Save oddlyzen/8367743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/my_model.rb | |
module MyApp | |
module Model | |
def self.included(base) | |
base.send :include, Mongoid::Document | |
base.send :include, Mongoid::Timestamps | |
base.send :include, ActiveAdmin::Mongoid::Patches | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/company.rb | |
class Company | |
include MyApp::Model | |
field ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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