implement /setup and /admin
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
class Admin::BaseController < ApplicationController
|
||||
before_action :require_admin
|
||||
layout "admin"
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
class Admin::DashboardController < Admin::BaseController
|
||||
def index
|
||||
@user_count = User.count
|
||||
@contributor_count = User.contributor.count
|
||||
@reviewer_count = User.reviewer.count
|
||||
@admin_count = User.admin.count
|
||||
|
||||
@entry_count = Entry.count
|
||||
@verified_count = Entry.where(verified: true).count
|
||||
@unverified_count = @entry_count - @verified_count
|
||||
|
||||
@pending_suggestions_count = SuggestedMeaning.pending.count
|
||||
@accepted_suggestions_count = SuggestedMeaning.accepted.count
|
||||
@rejected_suggestions_count = SuggestedMeaning.rejected.count
|
||||
|
||||
@comment_count = Comment.count
|
||||
|
||||
@recent_users = User.order(created_at: :desc).limit(5)
|
||||
@recent_entries = Entry.order(created_at: :desc).limit(5)
|
||||
@pending_invitations = User.where.not(invitation_token: nil)
|
||||
.where(invitation_accepted_at: nil)
|
||||
.where("invitation_sent_at > ?", 14.days.ago)
|
||||
.count
|
||||
|
||||
@supported_languages = SupportedLanguage.where(active: true).order(:sort_order)
|
||||
@language_completion = @supported_languages.index_with do |language|
|
||||
next 0 if @entry_count.zero?
|
||||
|
||||
(Entry.where.not(language.code => [ nil, "" ]).count * 100.0 / @entry_count).round
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,49 @@
|
||||
class Admin::InvitationsController < Admin::BaseController
|
||||
def index
|
||||
@pending_invitations = User.where.not(invitation_token: nil)
|
||||
.where(invitation_accepted_at: nil)
|
||||
.order(invitation_sent_at: :desc)
|
||||
@accepted_invitations = User.where.not(invitation_accepted_at: nil)
|
||||
.order(invitation_accepted_at: :desc)
|
||||
.limit(20)
|
||||
end
|
||||
|
||||
def new
|
||||
@invitation = User.new
|
||||
end
|
||||
|
||||
def create
|
||||
@invitation = User.new(invitation_params)
|
||||
@invitation.invitation_token = SecureRandom.urlsafe_base64(32)
|
||||
@invitation.invitation_sent_at = Time.current
|
||||
@invitation.invited_by = current_user
|
||||
@invitation.password = SecureRandom.urlsafe_base64(16)
|
||||
|
||||
if @invitation.save
|
||||
# TODO: Send invitation email
|
||||
# InvitationMailer.invite(@invitation).deliver_later
|
||||
|
||||
redirect_to admin_invitations_path, notice: "Invitation sent to #{@invitation.email}"
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@invitation = User.find(params[:id])
|
||||
|
||||
if @invitation.invitation_accepted_at.present?
|
||||
redirect_to admin_invitations_path, alert: "Cannot cancel an accepted invitation."
|
||||
return
|
||||
end
|
||||
|
||||
@invitation.destroy
|
||||
redirect_to admin_invitations_path, notice: "Invitation cancelled."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def invitation_params
|
||||
params.require(:user).permit(:email, :name, :role, :primary_language)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
class Admin::UsersController < Admin::BaseController
|
||||
before_action :set_user, only: [ :edit, :update, :destroy ]
|
||||
|
||||
def index
|
||||
@users = User.order(created_at: :desc)
|
||||
@users = @users.where(role: params[:role]) if params[:role].present?
|
||||
@users = @users.where("email LIKE ?", "%#{params[:q]}%") if params[:q].present?
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
if @user.update(user_params)
|
||||
redirect_to admin_users_path, notice: "User updated successfully."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @user == current_user
|
||||
redirect_to admin_users_path, alert: "You cannot delete your own account."
|
||||
return
|
||||
end
|
||||
|
||||
if @user == User.first
|
||||
redirect_to admin_users_path, alert: "Cannot delete the first admin user (system default contact)."
|
||||
return
|
||||
end
|
||||
|
||||
@user.destroy
|
||||
redirect_to admin_users_path, notice: "User deleted successfully."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_user
|
||||
@user = User.find(params[:id])
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit(:name, :email, :role, :primary_language)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user