Created
January 25, 2013 09:57
-
-
Save cblavier/4633211 to your computer and use it in GitHub Desktop.
Get all mongoid Models in your project. Optionally filter them by superclass.
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to remove concerns
sanitized_model_paths.reject! {|path| path.start_with?('concerns')}