gestión de usuaries
This commit is contained in:
parent
abc02aef8b
commit
6e0860b462
6 changed files with 83 additions and 2 deletions
31
app/controllers/users_controller.rb
Normal file
31
app/controllers/users_controller.rb
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class UsersController < ApplicationController
|
||||||
|
def index
|
||||||
|
@users = User.all.order(created_at: :desc)
|
||||||
|
@user = User.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
user = User.create(password: SecureRandom.hex, created_by: current_user, **user_params)
|
||||||
|
|
||||||
|
user.send_reset_password_instructions if user.persisted?
|
||||||
|
|
||||||
|
redirect_to users_path
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
user = User.find(params[:id])
|
||||||
|
user.destroy
|
||||||
|
|
||||||
|
sign_out(current_user) if user == current_user
|
||||||
|
|
||||||
|
redirect_to users_path
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def user_params
|
||||||
|
@user_params ||= params.require(:user).permit(:email)
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,6 +1,12 @@
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
# Include default devise modules. Others available are:
|
# Include default devise modules. Others available are:
|
||||||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
||||||
devise :database_authenticatable,
|
devise :database_authenticatable,
|
||||||
:recoverable, :rememberable, :validatable, :lockable, :timeoutable
|
:recoverable, :rememberable, :validatable, :lockable, :timeoutable
|
||||||
|
|
||||||
|
# Trazabilidad
|
||||||
|
#
|
||||||
|
# @see {https://guides.rubyonrails.org/association_basics.html#self-joins}
|
||||||
|
belongs_to :created_by, class_name: 'User', optional: true
|
||||||
|
has_many :created, class_name: 'User', foreign_key: 'created_by_id'
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
<li><%= link_to t('.new_dashboard'), new_dashboard_path %></li>
|
<li><%= link_to t('.new_dashboard'), new_dashboard_path %></li>
|
||||||
<li><%= link_to t('.new_check'), new_check_path %></li>
|
<li><%= link_to t('.new_check'), new_check_path %></li>
|
||||||
<li><%= link_to t('.site'), Rails.application.routes.url_helpers.site_path(Site.first || Site.new) %></li>
|
<li><%= link_to t('.site'), Rails.application.routes.url_helpers.site_path(Site.first || Site.new) %></li>
|
||||||
|
<li><%= link_to t('.users'), Rails.application.routes.url_helpers.users_path %></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
27
app/views/users/index.haml
Normal file
27
app/views/users/index.haml
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
%section.w-100.min-vh-100.d-flex.align-items-center.justify-content-center
|
||||||
|
%div
|
||||||
|
%nav
|
||||||
|
= link_to t('.back'), root_path, class: 'btn btn-info mb-3'
|
||||||
|
|
||||||
|
%table.table
|
||||||
|
%thead
|
||||||
|
-# Esto no es HTML5 estándar pero al parecer funciona
|
||||||
|
= form_for @user do |f|
|
||||||
|
%tr
|
||||||
|
%td{ colspan: 3 }
|
||||||
|
= f.label :email, class: 'visually-hidden'
|
||||||
|
= f.email_field :email, class: 'form-control', placeholder: t('.email'), required: true
|
||||||
|
%td= f.submit t('.create'), class: 'btn btn-success'
|
||||||
|
%tr
|
||||||
|
%th= t('.email')
|
||||||
|
%th= t('.created_by')
|
||||||
|
%th= t('.created')
|
||||||
|
%th= t('.actions')
|
||||||
|
%tbody
|
||||||
|
|
||||||
|
- @users.each do |user|
|
||||||
|
%tr
|
||||||
|
%th= user.email
|
||||||
|
%td= user.created_by.try :email
|
||||||
|
%td.text-center= user.created.count
|
||||||
|
%td= link_to t('.destroy'), user_path(user), method: :delete, class: 'btn btn-danger', data: { confirm: t('.confirm') }
|
|
@ -39,4 +39,13 @@ es:
|
||||||
create: Guardar
|
create: Guardar
|
||||||
part_of: Es parte de %{words}. Cualquier cambio puede afectarlas.
|
part_of: Es parte de %{words}. Cualquier cambio puede afectarlas.
|
||||||
loading: Cargando...
|
loading: Cargando...
|
||||||
|
users:
|
||||||
|
index:
|
||||||
|
email: Correo electrónico
|
||||||
|
create: Crear usuarix
|
||||||
|
destroy: Quitar acceso
|
||||||
|
confirm: ¿Confirmas?
|
||||||
|
back: Volver
|
||||||
|
actions: Acciones
|
||||||
|
created_by: Invitadx por
|
||||||
|
created: Invitadxs
|
||||||
|
|
7
db/migrate/20220611152100_add_created_by_to_users.rb
Normal file
7
db/migrate/20220611152100_add_created_by_to_users.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class AddCreatedByToUsers < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
add_belongs_to :users, :created_by, index: true
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue