Skip to content

Commit

Permalink
better support of users (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
epugh authored Aug 23, 2023
1 parent 1be60d6 commit 1f8648e
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 7 deletions.
15 changes: 15 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@ def show
@pulse_chart_start_date = @pulse_chart_start_date.strftime('%Y-%m-%d')
end

def new
@user = User.new
end

# GET /admin/users/1/edit
def edit; end

def create
@user = User.new user_params
if @user.save
redirect_to admin_user_path(@user)
else
render action: :new
end
end

# PATCH/PUT /admin/users/1
# PATCH/PUT /admin/users/1.json
def update
Expand Down Expand Up @@ -75,6 +88,8 @@ def user_params
params.require(:user).permit(
:administrator,
:email,
:name,
:company,
:password,
:password_confirmation
)
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ def unlock
self.locked_at = nil
end

def fullname
name.blank? ? email : name.titleize
end

def after_database_authentication
# required by devise_invitable
end
Expand Down
34 changes: 32 additions & 2 deletions app/views/admin/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<legend>
Account
</legend>
<%= form_for(@user, url: admin_user_path(@user), class: 'form-horizontal') do |f| %>
<%= form_for(@user, url: {action: passed_in_action}, class: 'form-horizontal') do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
Expand All @@ -21,6 +21,20 @@
<%= f.text_field :email, class: 'form-control' %>
</div>
</div>

<div class="form-group clearfix">
<%= f.label :name, class: 'col-sm-2 control-label' %>
<div class="col-sm-10">
<%= f.text_field :name, class: 'form-control' %>
</div>
</div>

<div class="form-group clearfix">
<%= f.label :company, class: 'col-sm-2 control-label' %>
<div class="col-sm-10">
<%= f.text_field :company, class: 'form-control' %>
</div>
</div>

<p/>
<div class="form-group clearfix">
Expand All @@ -36,6 +50,22 @@

<p/>

<% # specially handling for passwords %>
<% if passed_in_action == 'create' %>
<div class="form-group">
<%= f.label :password, class: 'control-label' %>
<%= f.password_field :password, class: 'form-control input-lg', placeholder: 'New Password' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, class: 'control-label' %>
<%= f.password_field :password_confirmation, class: 'form-control input-lg', placeholder: 'Confirm New Password' %>
</div>
<p/>
<% end %>
<div class="form-group clearfix">
<%= f.submit class: 'btn btn-primary btn-lg' %>
</div>
Expand All @@ -45,7 +75,7 @@
<div class="col-xs-12 col-sm-6 col-sm-offset-3">
<%= form_for(@user, url: admin_user_path(@user), class: 'form-horizontal') do |f| %>
<%= form_for(@user, url: {action: passed_in_action}, class: 'form-horizontal') do |f| %>
<legend>
Password
</legend>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1>Editing User</h1>

<div class="clearfix">
<%= render 'form' %>
<%= render partial: 'form', locals: {passed_in_action: 'update'} %>
</div>

<%= link_to 'Show', admin_user_path(@user) %> |
Expand Down
9 changes: 8 additions & 1 deletion app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<h2>Users Listing</h2>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Users</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group me-2">
<%= link_to 'New User', new_admin_user_path, class: "btn btn-sm btn-outline-secondary" %>
</div>
</div>
</div>

<table class="table table-striped table-hover table-sm">
<thead>
Expand Down
5 changes: 5 additions & 0 deletions app/views/admin/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New User</h1>

<%= render partial: 'form', locals: {passed_in_action: 'create'} %>
<%= link_to 'Back', admin_users_path %>
9 changes: 8 additions & 1 deletion app/views/admin/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= image_tag @user.avatar_url(:big), size:'96x96', class:'img-rounded' %>

<div class="form-group">
<label class="col-sm-2 control-label">Username</label>
<label class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<p class="form-control-static"><%= @user.email %></p>
</div>
Expand All @@ -21,6 +21,13 @@
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label">Company</label>
<div class="col-sm-10">
<p class="form-control-static"><%= @user.company %></p>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label">Signed Up</label>
<div class="col-sm-10">
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ https://getbootstrap.com/docs/5.2/examples/navbars-offcanvas/# for example of of
<div class="text-center">
<p/>
<h5 class="card-title"><%= Time.now.strftime("%A, %B %d") %></h5>
<h2 class="card-title"><%= greeting() %>, <%= @current_user.name.titleize %></h2>
<h2 class="card-title"><%= greeting() %>, <%= @current_user.fullname %></h2>
</div>

<div class="row my-4">
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

namespace :admin do
get '/' => 'home#index'
resources :users, except: [ :new, :create ] do
resources :users do
resource :lock, only: [ :update ], module: :users
resource :pulse, only: [ :show ], module: :users
end
Expand Down

0 comments on commit 1f8648e

Please sign in to comment.