Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[38] Create Rails User model #46

Merged
merged 13 commits into from
Jul 26, 2024
70 changes: 70 additions & 0 deletions app/controllers/users_controller.rb
bilalhankins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show edit update destroy ]

# GET /users or /users.json
def index
@users = User.all
end

# GET /users/1 or /users/1.json
def show
end

# GET /users/new
def new
@user = User.new
end

# GET /users/1/edit
def edit
end

# POST /users or /users.json
def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to user_url(@user), notice: "User was successfully created." }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /users/1 or /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# DELETE /users/1 or /users/1.json
def destroy
@user.destroy!

respond_to do |format|
format.html { redirect_to users_url, notice: "User was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:role, :status, :finalized, :display, :email, :email_confirmation, :password_hash, :password, :password_confirmation, :token, :jwt_token, :email_verification_token, :email_verified_at, :password_reset_token, :password_reset_expires_at, :first_name, :last_name, :phone_number, :avatar_key, :avatar_extension, :terms_of_use, :privacy_guidelines, :agency_id, :last_active, :recertification_expired_at, :active_session, :renewal_request)
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ApplicationRecord
end
152 changes: 152 additions & 0 deletions app/views/users/_form.html.erb
bilalhankins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<%= form_with(model: user) do |form| %>
<% if user.errors.any? %>
<div style="color: red">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% user.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :role, style: "display: block" %>
<%= form.text_field :role %>
</div>

<div>
<%= form.label :status, style: "display: block" %>
<%= form.text_field :status %>
</div>

<div>
<%= form.label :finalized, style: "display: block" %>
<%= form.check_box :finalized %>
</div>

<div>
<%= form.label :display, style: "display: block" %>
<%= form.check_box :display %>
</div>

<div>
<%= form.label :email, style: "display: block" %>
<%= form.text_field :email %>
</div>

<div>
<%= form.label :email_confirmation, style: "display: block" %>
<%= form.text_field :email_confirmation %>
</div>

<div>
<%= form.label :password_hash, style: "display: block" %>
<%= form.text_field :password_hash %>
</div>

<div>
<%= form.label :password, style: "display: block" %>
<%= form.text_field :password %>
</div>

<div>
<%= form.label :password_confirmation, style: "display: block" %>
<%= form.text_field :password_confirmation %>
</div>

<div>
<%= form.label :token, style: "display: block" %>
<%= form.text_field :token %>
</div>

<div>
<%= form.label :jwt_token, style: "display: block" %>
<%= form.text_field :jwt_token %>
</div>

<div>
<%= form.label :email_verification_token, style: "display: block" %>
<%= form.text_field :email_verification_token %>
</div>

<div>
<%= form.label :email_verified_at, style: "display: block" %>
<%= form.datetime_field :email_verified_at %>
</div>

<div>
<%= form.label :password_reset_token, style: "display: block" %>
<%= form.text_field :password_reset_token %>
</div>

<div>
<%= form.label :password_reset_expires_at, style: "display: block" %>
<%= form.datetime_field :password_reset_expires_at %>
</div>

<div>
<%= form.label :first_name, style: "display: block" %>
<%= form.text_field :first_name %>
</div>

<div>
<%= form.label :last_name, style: "display: block" %>
<%= form.text_field :last_name %>
</div>

<div>
<%= form.label :phone_number, style: "display: block" %>
<%= form.text_field :phone_number %>
</div>

<div>
<%= form.label :avatar_key, style: "display: block" %>
<%= form.text_field :avatar_key %>
</div>

<div>
<%= form.label :avatar_extension, style: "display: block" %>
<%= form.text_field :avatar_extension %>
</div>

<div>
<%= form.label :terms_of_use, style: "display: block" %>
<%= form.datetime_field :terms_of_use %>
</div>

<div>
<%= form.label :privacy_guidelines, style: "display: block" %>
<%= form.datetime_field :privacy_guidelines %>
</div>

<div>
<%= form.label :agency_id, style: "display: block" %>
<%= form.number_field :agency_id %>
</div>

<div>
<%= form.label :last_active, style: "display: block" %>
<%= form.datetime_field :last_active %>
</div>

<div>
<%= form.label :recertification_expired_at, style: "display: block" %>
<%= form.datetime_field :recertification_expired_at %>
</div>

<div>
<%= form.label :active_session, style: "display: block" %>
<%= form.check_box :active_session %>
</div>

<div>
<%= form.label :renewal_request, style: "display: block" %>
<%= form.text_field :renewal_request %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
137 changes: 137 additions & 0 deletions app/views/users/_user.html.erb
bilalhankins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<div id="<%= dom_id user %>">
<p>
<strong>Role:</strong>
<%= user.role %>
</p>

<p>
<strong>Status:</strong>
<%= user.status %>
</p>

<p>
<strong>Finalized:</strong>
<%= user.finalized %>
</p>

<p>
<strong>Display:</strong>
<%= user.display %>
</p>

<p>
<strong>Email:</strong>
<%= user.email %>
</p>

<p>
<strong>Email confirmation:</strong>
<%= user.email_confirmation %>
</p>

<p>
<strong>Password hash:</strong>
<%= user.password_hash %>
</p>

<p>
<strong>Password:</strong>
<%= user.password %>
</p>

<p>
<strong>Password confirmation:</strong>
<%= user.password_confirmation %>
</p>

<p>
<strong>Token:</strong>
<%= user.token %>
</p>

<p>
<strong>Jwt token:</strong>
<%= user.jwt_token %>
</p>

<p>
<strong>Email verification token:</strong>
<%= user.email_verification_token %>
</p>

<p>
<strong>Email verified at:</strong>
<%= user.email_verified_at %>
</p>

<p>
<strong>Password reset token:</strong>
<%= user.password_reset_token %>
</p>

<p>
<strong>Password reset expires at:</strong>
<%= user.password_reset_expires_at %>
</p>

<p>
<strong>First name:</strong>
<%= user.first_name %>
</p>

<p>
<strong>Last name:</strong>
<%= user.last_name %>
</p>

<p>
<strong>Phone number:</strong>
<%= user.phone_number %>
</p>

<p>
<strong>Avatar key:</strong>
<%= user.avatar_key %>
</p>

<p>
<strong>Avatar extension:</strong>
<%= user.avatar_extension %>
</p>

<p>
<strong>Terms of use:</strong>
<%= user.terms_of_use %>
</p>

<p>
<strong>Privacy guidelines:</strong>
<%= user.privacy_guidelines %>
</p>

<p>
<strong>Agency:</strong>
<%= user.agency_id %>
</p>

<p>
<strong>Last active:</strong>
<%= user.last_active %>
</p>

<p>
<strong>Recertification expired at:</strong>
<%= user.recertification_expired_at %>
</p>

<p>
<strong>Active session:</strong>
<%= user.active_session %>
</p>

<p>
<strong>Renewal request:</strong>
<%= user.renewal_request %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/users/_user.json.jbuilder
bilalhankins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! user, :id, :role, :status, :finalized, :display, :email, :email_confirmation, :password_hash, :password, :password_confirmation, :token, :jwt_token, :email_verification_token, :email_verified_at, :password_reset_token, :password_reset_expires_at, :first_name, :last_name, :phone_number, :avatar_key, :avatar_extension, :terms_of_use, :privacy_guidelines, :agency_id, :last_active, :recertification_expired_at, :active_session, :renewal_request, :created_at, :updated_at
json.url user_url(user, format: :json)
10 changes: 10 additions & 0 deletions app/views/users/edit.html.erb
bilalhankins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing user</h1>

<%= render "form", user: @user %>

<br>

<div>
<%= link_to "Show this user", @user %> |
<%= link_to "Back to users", users_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Users</h1>

<div id="users">
<% @users.each do |user| %>
<%= render user %>
<p>
<%= link_to "Show this user", user %>
</p>
<% end %>
</div>

<%= link_to "New user", new_user_path %>
1 change: 1 addition & 0 deletions app/views/users/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @users, partial: "users/user", as: :user
Loading