Skip to content

Commit

Permalink
User management
Browse files Browse the repository at this point in the history
  • Loading branch information
northeastprince committed Nov 10, 2023
1 parent 084b4ae commit 7ecda3c
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/components/_user.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.user-snippet__title {
margin: 0;
color: var(--red);
}
14 changes: 14 additions & 0 deletions app/controllers/admin/hackathons/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Admin::Hackathons::SubscriptionsController < Admin::BaseController
before_action :set_subscription

def destroy
@subscription.unsubscribe
redirect_to admin_user_path(@subscription.subscriber)
end

private

def set_subscription
@subscription = Hackathon::Subscription.find(params[:id])
end
end
6 changes: 6 additions & 0 deletions app/controllers/admin/users/email_addresses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Admin::Users::EmailAddressesController < Admin::BaseController
include UserScoped

def edit
end
end
6 changes: 6 additions & 0 deletions app/controllers/admin/users/names_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Admin::Users::NamesController < Admin::BaseController
include UserScoped

def edit
end
end
34 changes: 34 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Admin::UsersController < Admin::BaseController
before_action :set_user, except: :index

def index
@pagy, @users = pagy User.all.order(created_at: :desc)
end

def show
end

def update
if @user.update(user_params)
redirect_to admin_user_path(@user)
else
flash.now[:notice] = @user.errors.full_messages.to_sentence

respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("flash", partial: "shared/flash") }

format.html { render :edit, status: :unprocessable_entity }
end
end
end

private

def set_user
@user = User.find(params[:id])
end

def user_params
params.require(:user).permit(:name, :email_address)
end
end
13 changes: 13 additions & 0 deletions app/controllers/concerns/user_scoped.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module UserScoped
extend ActiveSupport::Concern

included do
before_action :set_user
end

private

def set_user
@user = User.find(params[:user_id])
end
end
4 changes: 4 additions & 0 deletions app/models/user/named.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module User::Named
encrypts :name
end

def display_name
name.presence || email_address
end

def first_name
name&.split(" ")&.first
end
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/header/_nav.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div style="display: flex; gap: var(--spacing-2) var(--spacing-4); flex-wrap: wrap">
<%= render "admin/header/nav/link", title: "Hackathons", link: admin_hackathons_path %>
<%= render "admin/header/nav/link", title: "Users", link: admin_users_path %>
<%= render "admin/header/nav/link", title: "New Submission", link: new_hackathons_submission_path %>
</div>
6 changes: 6 additions & 0 deletions app/views/admin/users/_snippet.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= link_to admin_user_path(user), style: "text-decoration: none;" do %>
<article id="<%= dom_id(user) %>" class="card interactive">
<h2 class="user-snippet__title"><%= user.display_name %></h2>
<p><%= user.email_address %></p>
</article>
<% end %>
8 changes: 8 additions & 0 deletions app/views/admin/users/email_addresses/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<turbo-frame id="email_address">
<%= form_with model: @user, url: admin_user_path(@user), data: {controller: "form"} do |form| %>
<h2>
<%= form.text_field :email_address, required: true, data:
{action: "click@document->form#submitOnClickOutside keydown->form#submitOnEnter"} %>
</h2>
<% end %>
</turbo-frame>
7 changes: 7 additions & 0 deletions app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% page :narrow %>
<% @nav_active_item = admin_users_path %>
<h1 class="title">Users</h1>

<%= render partial: "snippet", collection: @users, as: :user %>

<%== pagy_nav(@pagy) %>
8 changes: 8 additions & 0 deletions app/views/admin/users/names/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<turbo-frame id="name">
<%= form_with model: @user, url: admin_user_path(@user), data: {controller: "form"} do |form| %>
<h1>
<%= form.text_field :name, data:
{action: "click@document->form#submitOnClickOutside keydown->form#submitOnEnter"} %>
</h1>
<% end %>
</turbo-frame>
39 changes: 39 additions & 0 deletions app/views/admin/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<% content_for :title, (@user.display_name) %>

<article id="<%= dom_id(@user) %>">
<turbo-frame id="name">
<h1>
🪪
<%= @user.display_name %>
<%= link_to "✏️", edit_admin_user_name_path(@user), class: "hidden-link text--small" %>
</h1>
</turbo-frame>

<turbo-frame id="email_address">
<h2>
📧
<%= @user.email_address %>
<%= link_to "✏️", edit_admin_user_email_address_path(@user), class: "hidden-link text--small" %>
</h2>
</turbo-frame>

<turbo-frame id="subscriptions">
<% if @user.subscriptions.active.any? %>
<div class="mt3">
<h3>Active Subscriptions</h3>
<ul>
<% @user.subscriptions.active.each do |subscription| %>
<li>
<%= subscription.location %>
<%= link_to "⛔", admin_subscription_path(subscription),
style: "text-decoration: none;", "data-turbo-method": "delete",
"data-turbo-confirm": "Remove this subscription for #{subscription.location}?" %>
</li>
<% end %>
</ul>
</div>
<% end %>
</turbo-frame>

<%= render "events/timeline", {eventable: @user} %>
</article>
10 changes: 10 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@

resource :name, :website, :times,
:expected_attendees, only: :edit

collection do
resources :subscriptions, only: :destroy
end
end
end

resources :users, only: [:index, :show, :update] do
scope module: :users do
resource :name, :email_address, only: :edit
end
end
end
Expand Down

0 comments on commit 7ecda3c

Please sign in to comment.