Not sure if you noticed, but we didn’t touch the admin panel in this module, even though we probably should have. There are three things that we should add:
/admin/contacts
.
The good news is that we’re not going to show you how to do it. We want you to try it on your own, using what you’ve learned in this chapter. Don’t worry though, we’re not going to let you swim in the deep end with no help; we’ll give you the steps to follow, but you’ll have to write the code yourself. We’ll even throw in a few screenshots!
Here are the steps:
Chapter-13
)
AdminController
decorator
Core
module) and its override
Good luck!
Because we know how good you are, we know that you would have noticed something funny happening with the user count: When clicking on the “Contacts” link, you would’ve noticed that the user count is blank, as shown in Figure 3 below:
This is because in the AdminController
, the user count is only updated in this index
action of this controller and not that of any other, like the ContactsController
. To fix this, we will have to move the .count
method into a before_action
, as show below:
engines/core/apps/controllers/blast/admin/admin_controller.rb
module Blast
module Admin
class AdminController < ApplicationController
before_action :set_user_count, only: :index
def index
authorize [:blast, :admin], :index?
@users = policy_scope(Blast::User).ordered.limit(3)
end
private
def set_user_count
@users_count = policy_scope(Blast::User).count
end
end
end
end
Don’t forget to push your changes to GitHub.
git status
git add .
git commit -m "Extending the Admin Panel"
git push origin Chapter-13
We finally reached the end of this module. Let’s go over everything we’ve learned!
In this chapter, we learned everything about extending anything! We used decorators to extend Models, Controllers and Views.
We built our first feature
module on top of the Core
module we created in the last module: the Contacts
module.
First, we created the Contact
model, its associated controller and its views. Then things got interesting: we extended the Core
views to add some info about the contacts, and we also had to extend the Core
models to achieve our desired result. Finally, to remove some logic from the views, we also extended the Core
controller.
Core
routes makes it easier to use them in all our engines (by calling blast.my_path
)
In the next module, we’re going to build a new feature module: Tasks
. With this module, a user will be able to create tasks and link them to a contact. We’ll re-use what we learned in this chapter, but also talk about how we can keep the Contacts
module and the Tasks
module independent.