What Is the Preferred Way (Better Style) to Name a Namespace in Ruby? Singular or Plural

Table Naming: Underscore vs Camelcase? namespaces? Singular vs Plural?

Being consistent is far more important than what particular scheme you use.

i18n file location and format for Named Spaced Form Object

If the form base object is an ActiveModel one, the yaml namespace to override names derived from the objects and attributes would be like this:

en:
activemodel:
models:
car_registration/basics:
one: BasicsSingular
other: BasicsPlural
attributes:
car_registration/basics:
name: NameAttributeTranslation

If they are ActiveRecord objects, replace activemodel with activerecord in the yaml file.

How to avoid Rails scaffold to place model into namespace

rails generate model Portfolio

rails generate controller Admin::Portfolios

Rails routing and Rest : make resources alias

You're correct. Your approach is not very RESTful or conventional.

If you need to separate parts of your application that are public facing and a dashboard or admin section backend you would normally set it up like so:

Rails.application.routes.draw do

# public facing routes
resources :activities, only: [:show, :index]
resources :tasks, only: [:show, :index]

namespace :management do
resources :activities
resources :tasks
end
end

Note that you should be using the plural form for your routes and controllers!
This would create "public" routes at:

GET  /activities       ActivitiesController#index
GET /activities/:id ActivitiesController#show

And also namespaced routes:

GET|POST          /management/activities            
GET|PATCH|DELETE /management/activities/:id
GET /management/activities/new
GET /management/activities/:id/edit

These routes would go to Management::ActivitiesController. This lets you provide different "representations" of a resource while still sticking to the rails conventions.

Updated.

If you want to do resource scoped roles you would do it like so:

# routes rb
Rails.application.routes.draw do
resources :roles, except: [:new, :create]

[:articles, :tasks].each do |resource_name|
resources resource_name do
resources :roles, only: [:create, :index], module: resource_name.to_s
end
end
end

       Prefix Verb   URI Pattern                           Controller#Action
roles GET /roles(.:format) roles#index
edit_role GET /roles/:id/edit(.:format) roles#edit
role GET /roles/:id(.:format) roles#show
PATCH /roles/:id(.:format) roles#update
PUT /roles/:id(.:format) roles#update
DELETE /roles/:id(.:format) roles#destroy
article_roles GET /articles/:article_id/roles(.:format) articles/roles#index
POST /articles/:article_id/roles(.:format) articles/roles#create
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
task_roles GET /tasks/:task_id/roles(.:format) tasks/roles#index
POST /tasks/:task_id/roles(.:format) tasks/roles#create
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy

This means to add a manager for a task you would do:

POST tasks/1/roles
{
name: "manager",
user_id: 666
}

# - resource_id   [integer]
# - resource_type [string]
# - user_id [integer, foreign key]
# - name [string]
class Role < ActiveRecord::Base
belongs_to :resource, polymorpic: true
belings_to :user
end

class Article < ActiveRecord::Base
has_many :roles, as: :resource
# ...
end

class Task < ActiveRecord::Base
has_many :roles, as: :resource
# ...
end
class User < ActiveRecord::Base
has_many :roles
end

# do the normal index, delete, update here
class RolesController < ApplicationController
# ...
end

# abstract controller class for reuse
class NestedRolesController < ApplicationController

before_filter :set_resource

def create
@role = @resource.roles.create(role_params)
respond_with(@role)
end

def role_params
params.require(:role).permit(:user_id, :name)
end
end

class Articles::RolesController < NestedRolesController
def set_resource
@resource = Article.find(params[:article_id])
end
end

class Tasks::RolesController < NestedRolesController
def set_resource
@resource = Task.find(params[:task_id])
end
end


Related Topics



Leave a reply



Submit