Skip to content

Instantly share code, notes, and snippets.

@cblavier
Created January 25, 2013 09:57
Show Gist options
  • Save cblavier/4633211 to your computer and use it in GitHub Desktop.
Save cblavier/4633211 to your computer and use it in GitHub Desktop.
Get all mongoid Models in your project. Optionally filter them by superclass.
module Jobbr
module Mongoid
include ::Mongoid
extend self
# Return all Mongoid models.
# You can also pass a parent class to get all Mongoid childrens
#
# @example Return *all* models.
# Rails::Mongoid.models
#
# @example Return Job children models.
# Rails::Mongoid.models(Job)
#
# @param [ Class ] parent The parent model class.
#
# @return [ Array<Class> ] The models.
#
def models(parent = nil)
model_paths = Dir["#{Rails.root}/app/models/**/*.rb"]
sanitized_model_paths = model_paths.map { |path| path.gsub(/.*\/app\/models\//, '').gsub('.rb', '') }
model_constants = sanitized_model_paths.map do |path|
path.split('/').map { |token| token.camelize }.join('::').constantize
end
model_constants = model_constants.select { |constant| constant.include?(Mongoid::Document) }
if parent
model_constants.select { |model| superclasses(model).include?(parent) }
else
model_constants
end
end
# Return all superclasses for a given class.
#
# @param [ Class ] parent The class you want to get superclasses from.
#
# @return [ Array<Class> ] The superclasses.
#
def superclasses(klass)
super_classes = []
while klass != Object
klass = klass.superclass
super_classes << klass
end
super_classes
end
end
end
@tobob
Copy link

tobob commented Oct 25, 2016

to remove concerns

sanitized_model_paths.reject! {|path| path.start_with?('concerns')}

@anilmaurya
Copy link

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment