Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to alter the folder structure in a Rails project?

I'm coming from the .Net MVC background looking to do a project in RoR. Specifically I'm develop a REST api.

I will describe my thinking and hopefully you could steer me in the right direction. My api must support versions and the most robust way to version an api is to duplicate the code for every version. That way fixing issues in one version doesn't effect other versions. When it comes to doing that in .NET MVC, areas are your best friend because source files for each version can nicely be segmented through using areas.

So my question is: In RoR is it possible to change the directory structure so this hierarchy

app/
  controllers
    /v1
      c1_controller.rb
      c2_controller.rb
    /v2
      c1_controller.rb
      c2_controller.rb
  models/
    /v1
      m1.rb
      m2.rb
    /v2
      m1.rb
      m2.rb
  views/
    /v1
      view1.html.erb
      view2.html.erb
    /v3
      view1.html.erb
      view2.html.erb

can be rearranged to this?

app/
  v1/
    controllers/
      c1_controller.rb
      c2_controller.rb
    models/
      m1.rb
      m2.rb
    views/
      view1.html.erb
      view2.html.erb
  v2/
    controllers/
      c1_controller.rb
      c2_controller.rb
    models/
      m1.rb
      m2.rb
    views/
      view1.html.erb
      view2.html.erb
like image 217
Roman Avatar asked Dec 16 '22 00:12

Roman


2 Answers

Checkout this page, it will give you some insights on directory structure for Rails 3+ projects:

http://edgeapi.rubyonrails.org/classes/Rails/Engine.html

Since Rails 3.0, applications and engines have more flexible path configuration (as opposed to the previous hardcoded path configuration). This means that you are not required to place your controllers at app/controllers, but in any place which you find convenient.

Don't be scared with the fact that it's about Engines, it states in the very beginning: every Rails App is an Engine.

UPDATE: Never had an opportunity to do that myself, but according to that page and this, you should add the following thing into your config/application.rb within class Application < Rails::Application:

config.paths["app/controllers"] << "app/v1/controllers"
config.paths["app/controllers"] << "app/v2/controllers"
config.paths["app/controllers"] << "app/v3/controllers"

config.paths["app/models"] << "app/v1/models"
config.paths["app/models"] << "app/v2/models"
config.paths["app/models"] << "app/v3/models"

config.paths["app/views"] << "app/v1/views"
config.paths["app/views"] << "app/v2/views"
config.paths["app/views"] << "app/v3/views"

Check this project as an example: https://github.com/forker/multiversion

like image 63
forker Avatar answered Mar 23 '23 00:03

forker


I think you may be trying to solve your problem in slightly the wrong place. If you need to support several versions of your application simultaneously, and be able to make fixes to them independently etc etc, using git for your version control (if you're not already) and creating a separate branch for each version sounds like the way to go to me. (I'm sure you could do similar with Mercurial, SVN etc but Git does seem to be the Rails de facto standard).

Here's a link to some info about branching: http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging

like image 35
Russell Avatar answered Mar 23 '23 00:03

Russell