Skip to content

Instantly share code, notes, and snippets.

@jnillo
Created April 11, 2012 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnillo/2357736 to your computer and use it in GitHub Desktop.
Save jnillo/2357736 to your computer and use it in GitHub Desktop.
Extract a list of available data model's entities on RoR projects
# This method reads all models that the project hash.
# @return [Array] The proyect models list.
def self.load_models
models = [] #All models will be in this list.
models_valids = [] #This list is only to models with database table associated.
mod = nil
#Get all models in Model's folder
Dir["#{RAILS_ROOT}/app/models/**/*.rb"].each do |file|
models << file.gsub(RAILS_ROOT+'/app/models/',"").gsub('.rb','').classify
end
# Here, get the correct model's name: Singular or Plural
models.each do |model|
begin
mod = eval model
mod.columns
rescue
begin
mod = eval model.pluralize
mod.columns
rescue
mod = nil
end
end
if mod
models_valids << mod.to_s
end
end
models_valids
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment