Skip to content

Commit

Permalink
Set up ActiveAdmin for user management with admin role access
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigore-George-Mihai committed Oct 22, 2024
1 parent 934832b commit 47f08a1
Show file tree
Hide file tree
Showing 23 changed files with 655 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .env.development.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
POSTGRES_USER=postgres
POSTGRES_PASSWORD=

# Default User
# Users
DEFAULT_USER_EMAIL=user@example.com
DEFAULT_USER_PASSWORD=Password123!
DEFAULT_ADMIN_EMAIL=admin@example.com
DEFAULT_ADMIN_PASSWORD=AdminPassword123!

# Redis
REDIS_URL=redis://localhost:6379/0
Expand Down
8 changes: 7 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-10-21 12:41:10 UTC using RuboCop version 1.67.0.
# on 2024-10-22 20:34:39 UTC using RuboCop version 1.67.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 43
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ gem "tzinfo-data", platforms: %i[windows jruby]
# Authentication
gem "devise"

# ActiveAdmin
gem "activeadmin"
gem "activeadmin_addons"

# Performance and Error Tracking
gem "rollbar"
gem "scout_apm"
Expand Down
65 changes: 63 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ GEM
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
active_material (2.1.2)
activeadmin (3.2.5)
arbre (~> 1.2, >= 1.2.1)
csv
formtastic (>= 3.1)
formtastic_i18n (>= 0.4)
inherited_resources (~> 1.7)
jquery-rails (>= 4.2)
kaminari (>= 1.2.1)
railties (>= 6.1)
ransack (>= 4.0)
activeadmin_addons (1.10.1)
active_material
railties
redcarpet
require_all
sassc
sassc-rails
xdan-datetimepicker-rails (~> 2.5.1)
activejob (7.2.1.1)
activesupport (= 7.2.1.1)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -77,6 +96,9 @@ GEM
tzinfo (~> 2.0, >= 2.0.5)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
arbre (1.7.0)
activesupport (>= 3.0.0)
ruby2_keywords (>= 0.0.2)
ast (2.4.2)
autoprefixer-rails (10.4.19.0)
execjs (~> 2)
Expand Down Expand Up @@ -111,6 +133,7 @@ GEM
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crass (1.0.6)
csv (3.3.0)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
Expand Down Expand Up @@ -152,22 +175,49 @@ GEM
ffi (1.17.0-x86_64-linux-gnu)
font-awesome-sass (6.5.2)
sassc (~> 2.0)
formtastic (5.0.0)
actionpack (>= 6.0.0)
formtastic_i18n (0.7.0)
globalid (1.2.1)
activesupport (>= 6.1)
has_scope (0.8.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.3)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
railties (>= 6.0.0)
inherited_resources (1.14.0)
actionpack (>= 6.0)
has_scope (>= 0.6)
railties (>= 6.0)
responders (>= 2)
io-console (0.7.2)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
jbuilder (2.13.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jquery-rails (4.6.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.7.2)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
language_server-protocol (3.17.0.3)
logger (1.6.1)
loofah (2.22.0)
Expand Down Expand Up @@ -259,8 +309,13 @@ GEM
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
ransack (4.2.1)
activerecord (>= 6.1.5)
activesupport (>= 6.1.5)
i18n
rdoc (6.7.0)
psych (>= 4.0.0)
redcarpet (3.6.0)
redis (5.3.0)
redis-client (>= 0.22.0)
redis-client (0.22.2)
Expand All @@ -270,6 +325,7 @@ GEM
io-console (~> 0.5)
request_store (1.7.0)
rack (>= 1.4)
require_all (3.0.0)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
Expand Down Expand Up @@ -311,9 +367,9 @@ GEM
parser (>= 3.3.1.0)
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-faker (1.1.0)
rubocop-faker (1.2.0)
faker (>= 2.12.0)
rubocop (>= 0.82.0)
rubocop (>= 1.13.0)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
Expand Down Expand Up @@ -401,6 +457,9 @@ GEM
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xdan-datetimepicker-rails (2.5.4)
jquery-rails
rails (>= 3.2.16)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.7.1)
Expand All @@ -414,6 +473,8 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
activeadmin
activeadmin_addons
bootsnap
bootstrap (~> 5.3.3)
brakeman
Expand Down
51 changes: 51 additions & 0 deletions app/admin/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

ActiveAdmin.register User do
permit_params :first_name, :last_name, :email, :role, :password, :password_confirmation

filter :first_name_cont, as: :string
filter :last_name_cont, as: :string
filter :email
filter :created_at

index do
selectable_column
id_column
column :first_name
column :last_name
column :email
column :role
column :created_at
column :updated_at
actions
end

show do
attributes_table do
row :id
row :first_name
row :last_name
row :email
row :role
row :created_at
row :updated_at
end
end

form do |f|
f.inputs "User Details" do
f.input :first_name
f.input :last_name
f.input :email
f.input :role, as: :select, collection: User.roles.keys
if f.object.new_record?
f.input :password
f.input :password_confirmation
else
f.input :password, hint: I18n.t("active_admin.hints.password")
f.input :password_confirmation, hint: I18n.t("active_admin.hints.password")
end
end
f.actions
end
end
1 change: 1 addition & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link active_material/actions-toggle.js
2 changes: 2 additions & 0 deletions app/assets/javascripts/active_admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//= require active_admin/base
//= require activeadmin_addons/all
18 changes: 18 additions & 0 deletions app/assets/stylesheets/active_admin.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@import 'activeadmin_addons/all';
// Sass variable overrides must be declared before loading up Active Admin's styles.
//
// To view the variables that Active Admin provides, take a look at
// `app/assets/stylesheets/active_admin/mixins/_variables.scss` in the
// Active Admin source.
//
// For example, to change the sidebar width:
// $sidebar-width: 242px;

// Active Admin's got SASS!
@import "active_admin/mixins";
@import "active_admin/base";

// Overriding any non-variable Sass must be done after the fact.
// For example, to change the default status-tag color:
//
// .status_tag { background: #6090DB; }
1 change: 1 addition & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "@hotwired/turbo-rails"
import "controllers"

import "jquery"
import "@popperjs/core";
import "bootstrap"
import "jquery-ui"

Expand Down
12 changes: 10 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# frozen_string_literal: true

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable, and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

enum :role, { user: 0, admin: 1 }

validates :first_name, :last_name, :email, presence: true

def self.ransackable_attributes(_auth_object = nil)
%w[first_name last_name email role]
end

def password_required?
new_record? || password.present? || password_confirmation.present?
end
end
25 changes: 20 additions & 5 deletions app/views/layouts/partials/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
data-bs-target="#navbarHeader"
aria-controls="navbarHeader"
aria-expanded="false"
aria-label="Toggle navigation"
>
aria-label="Toggle navigation">
<i class="fas fa-bars"></i>
</button>

Expand All @@ -22,12 +21,28 @@

<% if user_signed_in? %>
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<%= button_to "Logout", destroy_user_session_path, method: :delete, class: "nav-link btn btn-link d-inline p-0", form: { "data-turbo": "true" } %>
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
href="#"
id="navbarDropdown"
role="button"
data-bs-toggle="dropdown"
aria-expanded="false">
Options
</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<% if current_user.admin? %>
<li><%= link_to "Admin", admin_root_path, class: "dropdown-item" %></li>
<% end %>
<li>
<%= button_to "Logout", destroy_user_session_path, method: :delete, class: "dropdown-item", form: { "data-turbo": "true" } %>
</li>
</ul>
</li>
</ul>
<% end %>
</div>
</div>
</nav>
</header>
</header>
Loading

0 comments on commit 47f08a1

Please sign in to comment.