Created
April 11, 2012 08:00
-
-
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 file contains hidden or 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
# 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