Skip to content

Commit

Permalink
[#7] Implements ability to create, update and delete users.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabionl committed Nov 7, 2021
1 parent b8dafd3 commit ca35b1a
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 15 deletions.
8 changes: 6 additions & 2 deletions app/components/shared/card_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ def classes
end

class ButtonComponent < ViewComponent::Base
def initialize(title:, path:, icon_class: "fa-pen")
def initialize(title:, path:, method: :get, icon_class: "fa-pen")
super()

@title = title
@path = path
@method = method
@icon_class = icon_class
end

Expand All @@ -53,7 +54,10 @@ def render?
end

def call
link_to @path, class: "button" do
params = { class: "button" }
params[:method] = @method if @method.present? && @method != :get

link_to @path, params do
button_content
end
end
Expand Down
20 changes: 10 additions & 10 deletions app/components/users/show_component/show_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@
<div class='column'>
<%= render Shared::CardComponent.new(title: 'User Details') do |card| %>
<% card.button(title: 'Edit', path: edit_admin_user_path(user)) %>
<% card.button(title: 'Delete', path: admin_user_path(user.id), icon_class: 'fa-trash-alt') %>
<% card.button(title: 'Delete', path: admin_user_path(user.id), method: :delete, icon_class: 'fa-trash-alt') %>

<table class='table'>
<tr>
<th>Status</th>
<td>
<%= status_icon %>
<%= status_title %>
</td>
</tr>

<tr>
<th>Name:</th>
<td><%= user.name %></td>
Expand All @@ -25,7 +17,7 @@

<tr>
<th>Organization:</th>
<td><%#= user.organization %></td>
<td><%= user.organization %></td>
</tr>

<tr>
Expand All @@ -40,6 +32,14 @@
<!-- Descriptions -->
<%= render Shared::CardComponent.new(title: 'Info') do |card| %>
<table class='table'>
<tr>
<th>Status</th>
<td>
<%= status_icon %>
<%= status_title %>
</td>
</tr>

<tr>
<th>Admin:</th>
<td><%= user.admin.to_s.titleize %></td>
Expand Down
40 changes: 37 additions & 3 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,47 @@ class Admin::UsersController < Admin::BaseController

def index; end

def new
@user = User.new(admin: false, verified: false)
end

def show; end

def edit; end

def update; end
def create
@user = User.new(user_params)
if @user.save
redirect_to [:admin, @user], notice: "Successfully created user (id: #{@user.id}, email: #{@user.email})"
else
flash.now[:alert] = "Failed to create user. Errors: #{@user.errors.full_messages.join('; ')}"

render action: :new, status: :unprocessable_entity
end
end

def update
if @user.update(user_params)
redirect_to [:admin, @user], notice: "Successfully updated user (id: #{@user.id})"
else
flash.now[:alert] = "Failed to update user (id: #{@user.id}). Errors: #{@user.errors.full_messages.join('; ')}"

def destroy; end
render action: :edit, status: :unprocessable_entity
end
end

def destroy
if @user.destroy
flash[:notice] = "Successfully deleted User #{@user.name} (id: #{@user.id}, email: #{@user.email})"

redirect_to action: :index
else
# Error when turning Welcome on.
flash[:error] = "Failed to delete User #{@user.name} (id: #{@user.id}, email: #{@user.email}). Errors: #{@user.errors.full_messages.join('; ')}"

render action: :show, status: :unprocessable_entity
end
end

private

Expand All @@ -27,6 +61,6 @@ def load_user
end

def user_params
params.require(:user).permit()
params.require(:user).permit(:name, :email, :phone_number, :organization, :verified, :admin, :password, :password_confirmation)
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class User < ApplicationRecord
format: /\A\S+@\S+\z/,
uniqueness: { case_sensitive: false }

scope :verified, -> { where(verified: true) }
scope :not_verified, -> { where(verified: false) }
scope :super_admins, -> { verified.where(admin: true) }

# def self.authenticate(email, password)
# user = User.find_by(email: email)
# user&.authenticate(password)
Expand Down
73 changes: 73 additions & 0 deletions app/views/admin/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<div class="columns">
<div class="column">
<%= render Shared::CardComponent.new(title: 'Details') do |card| %>
<div class="field">
<%= form.label :name, "Name:", class: "label" %>
<div class="control">
<%= form.text_field :name, class: "input" %>
</div>
</div>

<div class="field">
<%= form.label :email, "Email:", class: "label" %>
<div class="control">
<%= form.email_field :email, class: "input" %>
</div>
</div>

<div class="field">
<%= form.label :phone_number, "Phone Number:", class: "label" %>
<div class="control">
<%= form.telephone_field :phone_number, class: "input" %>
</div>
</div>

<div class="field">
<%= form.label :organization, "Organization:", class: "label" %>
<div class="control">
<%= form.text_field :organization, class: "input" %>
</div>
</div>
<% end %>
</div>

<div class="column">
<%= render Shared::CardComponent.new(title: 'Config') do |card| %>
<div class="field">
<div class="control">
<%= form.label :verified, class: "checkbox" do %>
Verified:
<%= form.check_box :verified, class: "checkbox" %>
<% end %>
</div>
</div>

<div class="field">
<div class="control">
<%= form.label :admin, class: "checkbox" do %>
Admin:
<%= form.check_box :admin, class: "checkbox" %>
<% end %>
</div>
</div>
<% end %>
<% if @user.new_record? %>
<%= render Shared::CardComponent.new(title: 'Password') do |card| %>
<div class="field">
<%= form.label :password, "Password:", class: "label" %>
<div class="control">
<%= form.password_field :password, class: "input" %>
</div>
</div>

<div class="field">
<%= form.label :password_confirmation, "Password Confirmation:", class: "label" %>
<div class="control">
<%= form.password_field :password_confirmation, class: "input" %>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
17 changes: 17 additions & 0 deletions app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="container">
<%= form_with model: [:admin, @user] do |form| %>
<!-- Action Buttons -->
<div class="field is-grouped is-grouped-right">
<p class="control">
<%= form.submit class: "button is-primary" %>
</p>

<p class="control">
<%= link_to "Back", admin_user_path(@user), class: "button is-light" %>
</p>
</div>

<%= render 'form', form: form %>
<% end %>
</div>

9 changes: 9 additions & 0 deletions app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<div class="container">
<div class="level">
<div class="level-left">
</div>

<div class="level-right">
<%= link_to "New User", new_admin_user_path, class: "button is-light level-item" %>
</div>
</div>

<%= render Users::TableComponent.new(users: @users) %>
</div>

Expand Down
17 changes: 17 additions & 0 deletions app/views/admin/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="container">
<%= form_with model: [:admin, @user] do |form| %>
<!-- Action Buttons -->
<div class="field is-grouped is-grouped-right">
<p class="control">
<%= form.submit class: "button is-primary" %>
</p>

<p class="control">
<%= link_to "Back", admin_users_path, class: "button is-light" %>
</p>
</div>

<%= render 'form', form: form %>
<% end %>
</div>

0 comments on commit ca35b1a

Please sign in to comment.