Skip to content

Commit

Permalink
User controller refactor
Browse files Browse the repository at this point in the history
* Allow Admins to delete users

* Add user page

* Allow admins to edit User profile

* Fix User list ordering error + validate presence of username

* Add responsive class to tables
  • Loading branch information
bbucsy authored Aug 23, 2021
1 parent 156424d commit 94c3c42
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 248 deletions.
27 changes: 21 additions & 6 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[show edit update destroy promote demote]
before_action :check_admin, only: %i[index promote demote]
before_action :set_user, only: %i[show edit update destroy promote demote force_edit force_update]
before_action :check_admin, only: %i[index promote demote force_edit force_update]
before_action :authenticate_user!, except: %i[new create]

# GET /users
Expand All @@ -11,6 +11,18 @@ def index
# GET /users/1
def show; end

# GET /users/1/force_edit (Admin edit)
def force_edit; end

# PATCH/PUT /users/1/force_update (Admin edit)
def force_update
if @user.update(user_params)
redirect_to @user, notice: 'User was successfully updated.'
else
render :edit, status: :unprocessable_entity
end
end

# GET /users/new --- Managed by Devise
def new
redirect_to new_user_registration_path
Expand Down Expand Up @@ -43,11 +55,14 @@ def update
# end
end

# DELETE /users/1 --- Managed by Devise
# DELETE /users/1
def destroy
redirect_to cancel_user_registration_path
# @user.destroy
# redirect_to users_url, notice: "User was successfully destroyed."
if user_signed_in? && current_user.admin?
@user.destroy
redirect_to users_url, notice: 'User was successfully destroyed.'
else
redirect_to cancel_user_registration_path
end
end

# POST /users/1/promote
Expand Down
4 changes: 4 additions & 0 deletions app/models/participation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ class Participation < ApplicationRecord
belongs_to :event

scope :for_event, ->(event_id) { where(event_id: event_id) }

def can_edit?(user)
event.can_participate? && (event.participants.include?(user) || user.admin?)
end
end
4 changes: 3 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class User < ApplicationRecord
has_many :events, through: :participations
alias rentals items
alias participated_events events
attribute :admin, default: false
validates :name, presence: true, length: { minimum: 3 }

def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider

data = auth.extra.raw_info
data = auth.extra.raw_info
user.uid = data.internal_id
user.email = data.mail
user.name = data.displayName
Expand Down
34 changes: 17 additions & 17 deletions app/views/event_types/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 class="text-center my-4">Űrlapok</h1>

<div class="row justify-content-center">
<div class="col col-md-10 col-lg-8 col-xl-6 col-12">
<div class="col col-md-10 col-lg-8 col-xl-6 col-12 table-responsive">
<table class="table table-sm">
<thead>
<tr>
Expand All @@ -16,29 +16,29 @@
<td class="py-2"><%= link_to event_type.name, event_type %></td>
<td class="text-end">
<div class="btn-group" role="group" aria-label="Műveletek">
<%= link_to edit_event_type_path(event_type),
class: "btn btn-outline-primary btn-sm",
title: "Űrlap módosítása" do %>
<%= link_to edit_event_type_path(event_type),
class: "btn btn-outline-primary btn-sm",
title: "Űrlap módosítása" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
</svg>
<% end %>
<%= link_to copy_event_type_path(event_type),
method: :post,
data: { confirm: 'Lemásolni készülsz a kiválasztott űrlapot. Folytatás?' },
title: "Űrlap másolása",
class: "btn btn-outline-primary btn-sm" do %>
<%= link_to copy_event_type_path(event_type),
method: :post,
data: { confirm: 'Lemásolni készülsz a kiválasztott űrlapot. Folytatás?' },
title: "Űrlap másolása",
class: "btn btn-outline-primary btn-sm" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
</svg>
<% end %>
<%= link_to event_type,
method: :delete,
data: { confirm: 'Biztosan törlöd?' },
title: "Űrlap törlése",
class: "btn btn-outline-danger btn-sm" do %>
<%= link_to event_type,
method: :delete,
data: { confirm: 'Biztosan törlöd?' },
title: "Űrlap törlése",
class: "btn btn-outline-danger btn-sm" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
</svg>
<% end %>
</div>
Expand Down
115 changes: 59 additions & 56 deletions app/views/events/list.html.erb
Original file line number Diff line number Diff line change
@@ -1,65 +1,68 @@
<h1 class="my-5">Események</h1>

<table class="table">
<thead>
<tr>
<th>Név</th>
<th>Kezdete</th>
<th>Vége</th>
<th>Jelentkezési határidő</th>
<th>Jelentkező űrlap</th>
<th>Jelentkezők</th>
<th></th>
</tr>
</thead>

<tbody>
<% @events.each do |event| %>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<td><%= link_to event.name, event %></td>
<td><%= event.start %></td>
<td><%= event.end %>
<td><%= event.event_type ? event.deadline : "-" %></td>
<td>
<% if event.event_type %>
<%= link_to event.event_type.name, event.event_type %>
<% else %>
-
<% end %>
</td>
<td>
<% if event.event_type %>
<% if event.participants.empty? %>
Még nincs jelentkező
<th>Név</th>
<th>Kezdete</th>
<th>Vége</th>
<th>Jelentkezési határidő</th>
<th>Jelentkező űrlap</th>
<th>Jelentkezők</th>
<th></th>
</tr>
</thead>

<tbody>
<% @events.each do |event| %>
<tr>
<td><%= link_to event.name, event %></td>
<td><%= event.start %></td>
<td><%= event.end %>
<td><%= event.event_type ? event.deadline : "-" %></td>
<td>
<% if event.event_type %>
<%= link_to event.event_type.name, event.event_type %>
<% else %>
<%= link_to "#{event.participants.count} db jelentkező", event_registrations_path(event) %>
<% end %>
<% else %>
-
<% end %>
</td>
<td>
<div class="btn-group" role="group" aria-label="Műveletek">
<%= link_to edit_event_path(event), class: "btn btn-outline-primary btn-sm", title: "Módosítás" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
-
<% end %>
<%= link_to event,
method: :delete,
data: { confirm: 'Biztosan törlöd?' },
class: "btn btn-outline-danger btn-sm",
title: "Törlés" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</td>
<td>
<% if event.event_type %>
<% if event.participants.empty? %>
Még nincs jelentkező
<% else %>
<%= link_to "#{event.participants.count} db jelentkező", event_registrations_path(event) %>
<% end %>
<% else %>
-
<% end %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</td>
<td>
<div class="btn-group" role="group" aria-label="Műveletek">
<%= link_to edit_event_path(event), class: "btn btn-outline-primary btn-sm", title: "Módosítás" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
</svg>
<% end %>
<%= link_to event,
method: :delete,
data: { confirm: 'Biztosan törlöd?' },
class: "btn btn-outline-danger btn-sm",
title: "Törlés" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
</svg>
<% end %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>


<div class="my-3">
<%= link_to 'Új esemény', new_event_path, class: "btn btn-primary" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/participations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

<div class="d-flex justify-content-between my-3">
<%= link_to 'Vissza', event_path(@participation.event), class: "btn btn-outline-primary" %>
<%= link_to 'Módosítás', edit_participation_path(@participation), class: "btn btn-primary" %>
<%= link_to 'Módosítás', edit_participation_path(@participation), class: "btn btn-primary" if @participation.can_edit?(current_user) %>
</div>
87 changes: 45 additions & 42 deletions app/views/posts/list.html.erb
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
<h1 class="my-5">Bejegyzések</h1>

<table class="table">
<thead>
<tr>
<th>Cím</th>
<th>Létrehozva</th>
<th>Indexkép</th>
<th></th>
</tr>
</thead>

<tbody>
<% @posts.each do |post| %>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<td><%= link_to post.title, post %></td>
<td><%= post.created_at %></td>
<td>
<% if post.image.attached? %>
van
<% else %>
nincs
<% end %>
</td>
<td>
<div class="btn-group" role="group" aria-label="Műveletek">
<%= link_to edit_post_path(post), class: "btn btn-outline-primary btn-sm", title: "Módosítás" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
<% end %>
<%= link_to post,
method: :delete,
data: { confirm: 'Biztosan törlöd?' },
class: "btn btn-outline-danger btn-sm",
title: "Törlés" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
<% end %>
</div>
</td>
<th>Cím</th>
<th>Létrehozva</th>
<th>Indexkép</th>
<th></th>
</tr>
<% end %>
</tbody>
</table>
</thead>

<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= link_to post.title, post %></td>
<td><%= post.created_at %></td>
<td>
<% if post.image.attached? %>
van
<% else %>
nincs
<% end %>
</td>
<td>
<div class="btn-group" role="group" aria-label="Műveletek">
<%= link_to edit_post_path(post), class: "btn btn-outline-primary btn-sm", title: "Módosítás" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
</svg>
<% end %>
<%= link_to post,
method: :delete,
data: { confirm: 'Biztosan törlöd?' },
class: "btn btn-outline-danger btn-sm",
title: "Törlés" do %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
</svg>
<% end %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>

<div class="my-3">
<%= link_to 'Új Poszt', new_post_path, class: "btn btn-primary" %>
</div>
Loading

0 comments on commit 94c3c42

Please sign in to comment.