Is it possible to change the name of the "app" folder in a Rails application? - ruby-on-rails

When using Rails to only write an API, the term "app" seems less appropriate than something like "api". Is it possible to configure Rails to use a different folder besides "app" to load controllers, models, etc.? Preferably, I'd like my frontend code(outside the Rails asset pipeline) to live in "app", so creating a symlink isn't a preferable solution.

You can probably get it working by adding some hacks and fixes here and there. And then it will break again with the next gem/tool/IDE/plugin/...
Rails is strongly based on conventions, the app directory is one of them. Leaving as it is will save you lots of troubles.

Related

Is it possible to keep different folders for different applications in the root level of RoR project

This is what I'm trying to achieve. I want to build an application with an admin and client portal. So I want to build a single RoR application for both these portals and I would like to keep the code of each portal separated at the root level. So basically following is the folder structure I want to have in my project.
/admin
/client
/modals
/config
/db
...
...
So I want to keep controllers, views, and other stuff relevant to each project in each folder separately.
So I want to move the modal folder to the root level as well. I read about the namespaces in RoR, but as I understand I'm unable to do it with namespaces alone. I guess I need to do some configuration level changers, don't I?
Please let me know if there is a way to do this.
Thank You.
What you're suggesting sounds like 5 Rails apps running independently. Does each one of those directories have its own Gemfile, database?
It would be possible to do what you want, but it will almost certainly be more trouble than it's worth. You'll have to override some path configurations, make adjustments to connect the models/views/controllers directories for each entity (https://guides.rubyonrails.org/action_view_overview.html#view-paths), and probably much more. Rails is an opinionated framework and it doesn't do things like this gracefully.
The 'Rails' way to do it would be to:
Add some namespace routes for each of your directories
Add app/models/admin app/views/admin app/controllers/admin etc. directories
Namespace your model/view/controller files. e.g. class Admin::SomeController < ApplicationController
If each of those is really its own app, then you would probably want to create a repository for each of them so that they're in version control separately.

Ruby On Rails Package By Feature

Is there any way to package your code by feature in rails? I would like to structure my project so that each feature has its controllers, jobs, mailers, layouts and helpers in its own folder. e.g.
app
----user
--------jobs
--------controllers
--------mailers
--------layouts
--------helpers
----company
--------jobs
--------controllers
--------mailers
--------layouts
--------helpers
where user and company are seperate features.
I've done this in my app and its pretty easy.
You create your file structure the way you want it and then you just put config.autoload_paths += %W(#{config.root}/app/user/jobs) or whatever other subdirectory you want into your application.rb folder.
People might say this is not the rails way but I feel your pain when your project gets really big and hard to navigate.
Checkout Trailblazer gem. It is an extension on the basic MVC pattern.

Rails Spree internals

I am trying to use Spree with my RoR application. Ok, I do follow all those guides and FAQs on official website when I want to customize something. That's ok and no problem with it. One question, to which I could not find a clue -- how is that possible, that there is nothing in apps/view, apps/models folders, but it's still working? I mean, yes, I can create something in these folders and redefine the behavior of my views (actually, this is one of the ways of customization), but I really want to understand the internals. I am pretty new to Rails and got used to classic app folder structure.
what you are wondering about is the magic of Rails Engines.
Ruby on Rails allows you to define Engines (your app is one too) and when it looks for views/controllers/etc.. all mounted engines are part of the search path.
So the view is inside the Spree gem, not visible to you - but it still looks in there.
If you put something in your view folder with the same name, it will take precedence over the views in the Rails engine you have in the Gem.
Here is a good guide on how Engines work in Rails:
http://edgeguides.rubyonrails.org/engines.html
One good example of these Engines is the jQuery-rails Gem you probably use inside your Application.
It has no code at all (except for some fallbacks for Rails 3.0 and below that don't have an asset pipeline), but the jQuery.js file in the app/assets/javascripts folder. And since the engine is in the load path you can require the asset that's in there..
The engine itself has the same folder structure as your app (app/views, app/controllers ...)
You can look at the internal structure of Spree here: https://github.com/spree/spree/tree/master/core/app

Rails - Changing the config path

I copied the 'config' directory and renamed it to 'config_dev' so that I don't alter the current settings. How do I configure Rails to use the config directory of 'config_dev'?
Well, i'm not sure whether you can rename that and still make it work or not. However, i would highly not recommend that approach. If you must do something like that, better rename the files inside the folder, like environment.rb.bak or the likes.
Generally speaking a config folder is where important settings initiate from and i think that changing that convention can lead to more problems. I could be wrong, but i would just change the files (that's what the rails 2 to rails 3 conversion plugin does as well).

Change Rails Scaffold Naming Scheme

I'm a happy user of RoR but have one complaint. When I do script/generate scaffold it automatically generates all my files and places them in their proper folders. However, all the different scaffolds I've created name their view files the same.
I have a bunch of index.html.erb view files and when I have them open in my text editor, it's almost impossible to tell what controller they're related to.
I'd like to change the default naming scheme of the scaffold command to name the individual files to contain their view folder name. So, instead of index.html.erb, use index.home.html.
Is there a way to do this, or am I stuck? What solutions to the multiple files with the same name problem have you Rails developers discovered?
Thanks!
You're going to be fighting the Rails' conventions by going down that path and Rails works best when you work with it rather than against it. A core part of the philosophy of Rails is that there are a set of conventions that once learned make it easy to find your way around any Rails application.
Instead of trying to redefine how Rails works, I would recommend taking advantage of the features offered by your text editor or IDE for quickly navigating to the correct view template. For example, the Rails bundle within TextMate on the Mac lets you quickly open the view file associated with a particular model and there's a plugin for Vim that provides an equivalent feature.

Resources