Folowing some advices in Intermediate Ruby on Rails trail, i did it some changes to a personal dashboard controller putting the logic in a custom model.
But I think the model doesn’t smell well.
I have the same dashboard view for an user and admin.
The main difference is the admin can see all the data in the view and normal user just see his data.
##Controller
class DashboardController < ApplicationController
layout "dashboard/dashboard"
def show
@dashboard = Dashboard.new(current_user)
end
end
##Model
class Dashboard
attr_reader :user
def initialize(user)
@user = user
end
def total_patient
if user.has_role? :admin
Patient.count
else
user.total_patient
end
end
def total_consultations
if user.has_role? :admin
Consultation.count
else
user.total_consultations
end
end
def consultations
if user.has_role? :admin
Consultation.all
else
Consultation.last_consultations(user)
end
end
end
I think I am repeating the same if user.has_role? :admin
in all methods and maybe I can apply some pattern for this behavior.