Can models be put in subfolders?

I’m looking for ways to make my project file hierarchy look a bit
neater, by joining what can be joined and removing what can be
removed. I know now how to put controllers into subfolders (see my
admin), but what about models?

Here’s what I got so far:

Rakefile
app
app/controllers
app/controllers/about_controller.rb
app/controllers/admin
app/controllers/admin/ads_controller.rb
app/controllers/admin/application_controller.rb
app/controllers/admin/forums_controller.rb
app/controllers/admin/moderatorships_controller.rb
app/controllers/admin/sites_controller.rb
app/controllers/admin/users_controller.rb
app/controllers/ads_controller.rb
app/controllers/application.rb
app/controllers/forums_controller.rb
app/controllers/main_controller.rb
app/controllers/posts_controller.rb
app/controllers/radio_controller.rb
app/controllers/sessions_controller.rb
app/controllers/sites_controller.rb
app/controllers/topics_controller.rb
app/controllers/users_controller.rb
app/helpers
app/helpers/about_helper.rb
app/helpers/account_helper.rb
app/helpers/ads_helper.rb
app/helpers/application_helper.rb
app/helpers/forums_helper.rb
app/helpers/main_helper.rb
app/helpers/moderatorships_helper.rb
app/helpers/posts_helper.rb
app/helpers/radio_helper.rb
app/helpers/sessions_helper.rb
app/helpers/sites_helper.rb
app/helpers/topics_helper.rb
app/helpers/users_helper.rb
app/models
app/models/ad.rb
app/models/author.rb
app/models/category.rb
app/models/forum.rb
app/models/mail.rb
app/models/moderatorship.rb
app/models/monitorship.rb
app/models/parent_category.rb
app/models/post.rb
app/models/site.rb
app/models/topic.rb
app/models/user
app/models/user.rb
app/models/user/activation.rb
app/models/user/editable.rb
app/models/user/posting.rb
app/models/user/states.rb
app/models/user/validation.rb
app/models/user_observer.rb
app/views
app/views/about
app/views/about/main.html.erb
app/views/about/privacy.html.erb
app/views/about/safety.html.erb
app/views/about/scams.html.erb
app/views/about/terms.html.erb
app/views/admin
app/views/admin/ads
app/views/admin/ads/categories.html.erb
app/views/admin/ads/new_category.html.erb
app/views/admin/ads/show.html.erb
app/views/admin/forums
app/views/admin/forums/_form.html.erb
app/views/admin/forums/edit.html.erb
app/views/admin/forums/new.html.erb
app/views/admin/forums/show.html.erb
app/views/admin/main
app/views/admin/main/index.html.erb
app/views/admin/sites
app/views/admin/sites/_form.html.erb
app/views/admin/sites/edit.html.erb
app/views/admin/sites/new.html.erb
app/views/admin/sites/show.html.erb
app/views/admin/users
app/views/admin/users/show.html.erb
app/views/ads
app/views/ads/_ad.html.erb
app/views/ads/_category.html.erb
app/views/ads/_form.html.erb
app/views/ads/_form_header.html.erb
app/views/ads/_parent.html.erb
app/views/ads/_show_parent.html.erb
app/views/ads/edit.html.erb
app/views/ads/feed.atom.builder
app/views/ads/feed.rss.builder
app/views/ads/list.html.erb
app/views/ads/manage.html.erb
app/views/ads/new.html.erb
app/views/ads/post.html.erb
app/views/ads/select_category.rjs
app/views/ads/show.html.erb
app/views/ads/show_form.rjs
app/views/forums
app/views/forums/show.html.erb
app/views/layouts
app/views/layouts/_footer_1.html.erb
app/views/layouts/_footer_2.html.erb
app/views/layouts/_header_1.html.erb
app/views/layouts/_header_2.html.erb
app/views/layouts/application.html.erb
app/views/mail
app/views/mail/ad_activated.html.erb
app/views/mail/ad_activation.html.erb
app/views/mail/ad_forward.html.erb
app/views/mail/forum_activated.html.erb
app/views/mail/forum_activation.html.erb
app/views/main
app/views/main/index.html.erb
app/views/posts
app/views/posts/_edit.html.erb
app/views/posts/edit.html.erb
app/views/posts/index.html.erb
app/views/radio
app/views/radio/index.html.erb
app/views/sessions
app/views/sessions/new.rhtml
app/views/sites
app/views/sites/_form.html.erb
app/views/sites/new.html.erb
app/views/topics
app/views/topics/_form.html.erb
app/views/topics/edit.html.erb
app/views/topics/new.html.erb
app/views/topics/show.html.erb
app/views/users
app/views/users/_contact_info.html.erb
app/views/users/_settings.html.erb
app/views/users/edit.html.erb
app/views/users/index.html.erb
app/views/users/new.rhtml
app/views/users/show.html.erb
config
config/boot.rb
config/database.yml
config/environment.rb
config/environments
config/environments/development.rb
config/environments/production.rb
config/initializers
config/initializers/active_record.rb
config/initializers/concerns.rb
config/initializers/inflections.rb
config/initializers/mime_types.rb
config/routes.rb
db
db/migrate
db/migrate/001_create_users.rb
db/migrate/002_create_parent_categories.rb
db/migrate/003_create_categories.rb
db/migrate/004_create_ads.rb
db/migrate/005_add_header_column_to_category_table.rb
db/migrate/006_add_fields_to_ads_model.rb
db/migrate/007_create_authors.rb
db/migrate/008_create_permalinks.rb
db/migrate/009_add_author_ip_and_author_ban_flag.rb
db/schema.rb
lib
lib/authenticated_system.rb
lib/authenticated_test_helper.rb
lib/html_formatting.rb
lib/tasks
lib/tasks/load_fixtures.rake
log
log/development.log
project.txt
public
public/404.html
public/422.html
public/500.html
public/favicon.ico
public/images
public/images/logo.gif
public/images/spinner.gif
public/javascripts
public/javascripts/application.js
public/javascripts/controls.js
public/javascripts/dragdrop.js
public/javascripts/effects.js
public/javascripts/lowpro.js
public/javascripts/prototype.js
public/javascripts/time.js
public/robots.txt
public/stylesheets
public/stylesheets/display.css

Any other suggestions to simplifying my app would be greatly
appreciated.

Kyrre

I’m looking for ways to make my project file hierarchy look a bit
neater, by joining what can be joined and removing what can be
removed. I know now how to put controllers into subfolders (see my
admin), but what about models?

I think you can add a sub directory to the load path, but I haven’t
done it, so this is untested. Put this in environment.rb.

config.load_paths += %W( #{RAILS_ROOT}/app/models/newdirectory )

Hope that helps.
Cheers,
Jordan

I have this line in my environments.rb file (within the initializer
block):

Dir.glob("#{RAILS_ROOT}/app/models/*[^(.rb|.ignore)]").each{|dir|
config.load_paths << dir }

So now I can organize models into subdirectories. And if there is a
subdir I don’t want loaded, I add “.ignore” to the end of it. Works
like a charm for me.

-Danimal

Danimal,
Would you mind using a few simple words to describe what is going on
with this (Dir.glob) you show below? What is the significance of
Dir.glob and how is it used? Could you throw in some comments to show
how this works?
How is Dir.glob then used in your application?
Thank you,
Kathleen

http://ruby-doc.org/core/classes/Dir.html#M002347

Maybe will help. Lists all the filenames under the specified directory.

That is truly awesome Danimal, much obliged!

Kyrre

On Apr 23, 8:47 am, Nathan E. <rails-mailing-l…@andreas-

Kathleen,

There could possibly be a cleaner way of doing this… i.e. grabbing
only the directories. The reason I used Dir.glob is two-fold:

  1. this was the example I found
  2. I can create a directory called “maybetoss.ignore” and put model
    files that I may be deleting but not sure.

It’s mostly just the way I work. As an application gets built out,
I’ll sometimes have models that I refactor or toss but I want to keep
the code around just in case.

So another way would be to use Dir.glob to get an array of everything
in app/models/ and then before adding it to the path, use
File.directory?(dir) to check if it’s a directory. The glob I have
just ignores “.rb” files.

So it’s really just a matter of how you want to arrange things in the
app/models/ directory. In my case, I only have .rb files, directories
and possibly a “something.ignore” directory, so that’s why I use the
glob I did.

Perhaps a cleaner one would be:

Dir.glob(“#{RAILS_ROOT}/app/models/*”).each{|dir| config.load_paths
<< dir if File.directory?(dir) }

This will then just add all subdirs to the load path.

-Danimal

On Apr 22, 6:40 pm, “[email protected][email protected]

Here’s my little contribution this problem, this add recursively all
subfolders :

Rails::Initializer.run do |config|
##…
def add_all_models_subfolders_recursively(config, path)
Dir.glob(path).each do |dir|
if File.directory?(dir)
p dir
config.load_paths << dir
add_all_models_subfolders_recursively(config, “#{dir}/")
end
end
end
add_all_models_subfolders_recursively(config, "#{RAILS_ROOT}/app/
models/
”)
##…
end

++

Hi,

I like Danimal’s approach.

Mine is recusive:

Dir.glob("#{RAILS_ROOT}/app/models/**/*").each {|d| config.load_paths
<< d if File.directory?(d) }

Kind regards
Peter

Kabnot,

That looks good to me… except that if I’m needing more than one
level of subfolders for my models, then either I’m over-organizing or
it is one big-ass project! The biggest Rails project I’ve done to date
only needed about 5-6 subfolders in the models directory. But, each to
their own. :slight_smile:

-Danimal

Hey Danimals and Pater ! :slight_smile:

Finally I’ve choose Peter’s solution, witch is concise and beautifull.
Mine is just strong-typing like and not in the ruby way :slight_smile:

Thanks.

Danimal wrote:

Kabnot,

That looks good to me… except that if I’m needing more than one
level of subfolders for my models, then either I’m over-organizing or
it is one big-ass project! The biggest Rails project I’ve done to date
only needed about 5-6 subfolders in the models directory. But, each to
their own. :slight_smile:

-Danimal

Feels like, if you needed that many subfolders,
you should probably have been namespacing your models.

namely;

Users::Facebook, Users::Web
should live in /models/users

but, if we add all subfolders, we lose the possibility of this.