Build Robust & Production Quality Applications - Lesson 6: Continuous Delivery
05 Jun 2015Actors
Actors are different types of users, most likely referred to as admins, becasue they act differently within your application.
In some instances, you could:
def index
if current_user.admin?
Todo.all
else ....
end
But dependeing on the size, this could be quite cumbersome.
Another alternative, is we can create a seperate action:
def admin_index
@todos = Todo.all
end
The advatange is that everything the admin can do is seperate from the other actors. But this is also cumbersome b/c if we have multiple actors, before each action, we have to check the users role.
Namespace
So instead, we are going to work with Namespaces.
#routes.rb
resources :todos, only: [:index, :create, :new, :show]
namespace :admin do
resources :todos, only: [:index]
end
The above route would give us:
http://localhost:3000/admin/todos
So, therefore your controller would be in app/controllers/admin/todoscontroller, rather than app/controllers/todoscontroller
Add when created, our class name is prefixed with the module Admin
class Admin::TodosController < ApplicationController
end
We also need to run a migration to add the admin column to the users table
rails g migration add_admin_to_users
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean
end
end
Our viewv template in this case would be in:
app/views/admin/index.html.haml
This layout allows us to add things like an admin dashboard