Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Routes/Controller/Directory Structure design question
    text
    copied!<p>I have a design question where I would appreciate a thoughtful response.</p> <p>Let's say you have a simple application (for example's sake) that has a User, Company and Theme model. A Company <code>has_one</code> Theme and <code>has_many</code> Users.</p> <p>Administrators (a User) can fully manage Companies, Users and Themes - the whole REST stack, in addition to a few other actions too. Administrators are expected to do things to all 3 of these resources that other user roles cannot do.</p> <p>We also have a Company role. This role can edit their own Company, as well as select a Theme from the ones the admin-user added as nice defaults, or they can just make their own theme.</p> <p>Companies can also add/edite/delete users, but only for their company. These pages will have different views and they'll have different behaviour from admins - some overlaps, but some things will be restricted while others will be added.</p> <p>Now, here we have some non-trivial design choices, and I would like to know what the best-practice is.</p> <p><strong>PART 1</strong></p> <p>In Rails, it makes sense to have <code>resources :users, :companies, :themes</code> for the administrators and probably <code>resource :company, :theme, :users</code> for the Company users.</p> <p>But of course, we run into some naming conflicts here - both singular and plural - so we might want to try something like <code>resource :my_company, :my_theme, :my_users</code> to separate them? Or is there a better solution?</p> <p>Furthermore, a theme is just a component of a company, so maybe we want to nest them?</p> <pre><code>:resource :my_company do :resource :theme :resources :users end </code></pre> <p>This works okay, but it could be confusing as to which UsersController we are referring to... no? This is really sticky and I would love to know how to deal with this. Do you have 1 controller, or 2? What do you name them?</p> <p>So this would be an example:</p> <pre><code>http://myapp.com/my_company/theme/edit http://myapp.com/my_company/users/1/delete </code></pre> <p>Company users also might want the list of themes via ajax, so is it correct for them to call:</p> <pre><code>http://myapp.com/themes.json </code></pre> <p>?</p> <p>Is this how to approach this situation, or is there a better way?</p> <p><strong>PART 2</strong></p> <p>Also, what should your directory structure look? Should you have controllers separated by user role?</p> <pre><code>/app/controllers/admin/companies_controller.rb /app/controllers/admin/themes_controller.rb /app/controllers/admin/users_controller.rb /app/controllers/company/my_company_controller.rb /app/controllers/company/theme_controller.rb /app/controllers/company/users_controller.rb </code></pre> <p>Or is there better ways to handle this?</p> <p>It seems weird that users_controller is duplicated 2x and that there is a minor difference between Theme and Themes.</p> <p>I would really appreciate a thoughtful response on this. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload