Commit b152d655 by Thanh Hung Pham

Merge branch 'Admin' into 'master'

Admin

See merge request !13
parents 76e39781 7baa7a73
Pipeline #1500 failed with stages
in 0 seconds
# frozen_string_literal: true
class Admins::SessionsController < Devise::SessionsController
include Accessible
skip_before_action :check_user, only: :destroy
# before_action :configure_sign_in_params, only: [:create]
# GET /resource/sign_in
# def new
# super
# end
# POST /resource/sign_in
# def create
# super
# end
# DELETE /resource/sign_out
# def destroy
# super
# end
# protected
# def after_sign_in_path_for(resource)
# if session[:user_return_to] == nil
# admin_applies_path
# else
# super
# end
# end
# def after_sign_in_path_for(resource)
# admin_applies_path(resource)
# end
def after_sign_out_path_for(resource)
new_admin_session_path
end
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_in_params
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
# end
end
class AdminsController < ApplicationController
before_action :authenticate_admin!
def index
session.delete(:apply_job_ids) if session[:apply_job_ids].present?
hash_condition = { email: params[:email],
cities: { id: params[:city_id] },
industries: { id: params[:industry_id] } }
remove_blank_value = deep_compact(hash_condition)
@applied_jobs = applied_jobs_query.query_date(apply_params).where(remove_blank_value).page(params[:page])
session[:apply_job_ids] = @applied_jobs.map(&:id)
end
def export
apply_job = ApplyJob.find(session[:apply_job_ids])
csv = ExportCsvService.new(apply_job, Admin::CSV_ATTRIBUTES)
respond_to do |format|
format.html
format.csv { send_data csv.perform, filename: "Applies_Jobs-#{Date.today}.csv" }
end
end
private
def apply_params
params.permit(:email, :city_id, :industry_id, :start_date, :end_date)
end
def applied_jobs_query
@applied_jobs_query ||= AppliedQuery.new
end
def deep_compact(hash_condition)
hash_condition.each { |_, v| deep_compact(v) if v.is_a? Hash }.reject! { |_, v| v.nil? || v.blank? }
end
end
\ No newline at end of file
module Accessible
extend ActiveSupport::Concern
included do
before_action :check_user
end
protected
def check_user
if current_admin
flash.clear
# if you have rails_admin. You can redirect anywhere really
redirect_to(admin_applies_path) and return
elsif current_user
flash.clear
# The authenticated root path can be defined in your routes.rb in: devise_scope :user do...
redirect_to(authenticated_user_root_path) and return
end
end
end
\ No newline at end of file
class UsersController < ApplicationController
before_action :authenticate_user!
def show
@user = current_user
@user = User.find(user_params[:user_id])
end
private
def user_params
params.permit(:user_id)
end
end
\ No newline at end of file
end
......@@ -28,4 +28,12 @@ module ApplicationHelper
def favorite_text(job)
FavoriteJob.find_by(job: job, user: current_user).present? ? 'Unfavorite' : 'Favorite'
end
def cities_for_select
City.all.map { |city| [city.name, city.id] }
end
def industries_for_select
Industry.all.map { |industry| [industry.name, industry.id]}
end
end
class Admin < ApplicationRecord
CSV_ATTRIBUTES = %w[id name email created_at].freeze
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
......@@ -7,4 +7,4 @@ class ApplyJob < ApplicationRecord
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }
validates :cv, presence: true, content_type: { in: 'application/pdf', message: 'must be a valid pdf format' },
size: { less_than: 5.megabytes, message: 'should be less than 5MB' }
end
end
\ No newline at end of file
class AppliedQuery
def initialize(applied_jobs = ApplyJob)
@applied_jobs = applied_jobs.includes(job: %i[cities cities_jobs])
@applied_jobs = applied_jobs.includes(job: %i[cities cities_jobs industries])
end
def order_applied_jobs(current_user)
@applied_jobs.where(user: current_user).order('apply_jobs.created_at DESC')
end
def query_date(params)
if params[:start_date].present? && params[:end_date].present?
@applied_jobs.where(created_at: params[:start_date]..params[:end_date])
elsif params[:start_date].present?
@applied_jobs.where('apply_jobs.created_at >= ?', params[:start_date].to_date.beginning_of_day)
elsif params[:end_date].present?
@applied_jobs.where('apply_jobs.created_at <= ?', params[:end_date].to_date.end_of_day)
else
@applied_jobs
end
end
end
\ No newline at end of file
require 'csv'
class ExportCsvService
def initialize(objects, attributes)
@attributes = attributes
@objects = objects
@header = attributes.map { |attr| I18n.t("header_csv.#{attr}") }
end
def perform
CSV.generate do |csv|
csv << header
objects.each do |object|
csv << attributes.map { |attr| object[attr] }
end
end
end
private
attr_reader :attributes, :objects, :header
end
\ No newline at end of file
.d-flex.justify-content-center
= paginate @applied_jobs, window: 1
- @applied_jobs.each do |applied_job|
.row.bg-white
.col-11.p-3.align-self-center
h5.mb-1
= link_to applied_job.job.title, detail_path(applied_job.job.slug)
p.mb-1
= link_to applied_job.name, my_path(user_id: applied_job.user_id)
p.mb-1
= applied_job.email
p.mb-1.text-end
| Applied at:
= format_datetime(applied_job.created_at)
.d-flex.justify-content-center.p-2
= page_entries_info @applied_jobs
.d-flex.justify-content-center
= paginate @applied_jobs, window: 1
- provide :title, 'Applies Jobs'
h2.p-3.text-center
| APPLIED JOBS
.form-apply
= form_with(url: admin_applies_path, method: :get, local: true) do |f|
.row.p-2
= f.label :email, class:"col-sm-1"
.col-sm-10
= f.email_field :email, class: 'form-control'
.row.p-2
.col
= f.select :city_id, cities_for_select, { prompt: 'Select a City' }, { class: 'form-control col-sm-10' }
.col
= f.select :industry_id, industries_for_select, { prompt: 'Select a Industry' }, { class: 'form-control' }
.row.p-2
.col
= f.date_field :start_date, class: 'form-control'
.col
= f.date_field :end_date, class: 'form-control'
.span.p-3.text-center
= f.submit "Search", class: "btn btn-primary btn-space"
= link_to "Download CSV", export_csv_path(format: :csv), class: "btn btn-primary btn-space"
= render 'result_search'
- provide :title, 'Login Admin'
h2.p-3.text-center
| Log in
.row
.col-md-6.offset-md-3.form-apply
= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
.card
.card-body
.field
= f.label :email
br
= f.email_field :email, autofocus: true, autocomplete: "email", class: 'form-control'
.field
= f.label :password
br
= f.password_field :password, autocomplete: "current-password", class: 'form-control'
.actions.p-2.text-center
= f.submit "Log in", class: "btn btn-primary btn-space"
header
nav.navbar.navbar-expand-lg.navbar-light.bg-dark
.container-fluid
= link_to (image_tag "logo.png", alt: "logo"), root_path, id: "logo"
- if current_admin
= image_tag "logo.png", alt: "logo", id: "logo"
- else
= link_to (image_tag "logo.png", alt: "logo"), root_path, id: "logo"
#navbarExample01.collapse.navbar-collapse
ul.navbar-nav.ms-auto.mb-2.mb-lg-0
- if user_signed_in?
......@@ -13,9 +16,12 @@ header
= link_to "History", history_path
li
= link_to('Logout', destroy_user_session_path, method: :delete)
- else
- elsif !admin_signed_in?
li
= link_to('Login', new_user_session_path)
li.nav-item
= link_to "Register", new_user_registration_path
- if admin_signed_in?
li
= link_to('Logout', destroy_admin_session_path, method: :delete)
......@@ -45,4 +45,13 @@ en:
one: "Displaying <b>1</b> %{entry_name}"
other: "Displaying <b>all %{count}</b> %{entry_name}"
more_pages:
display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
\ No newline at end of file
display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
home:
index:
link_export: Export Applies Jobs
title: Export CSV
header_csv:
id: id
name: Full Name
email: Email
created_at: Date
\ No newline at end of file
......@@ -14,7 +14,7 @@ Rails.application.routes.draw do
get 'history', to: 'history_jobs#index', as: 'history'
get '/my/jobs', to: 'apply_jobs#index', as: 'applied_job'
get 'jobs', to: 'jobs#search', as: 'search'
devise_for :users, skip: %i[sessions registrations passwords], controllers: { confirmations: 'users/confirmations' }
devise_for :users, path: 'users', skip: %i[sessions registrations passwords], controllers: { confirmations: 'users/confirmations' }
get '/my', to: 'users#show'
devise_scope :user do
get 'register/1', to: 'users/registrations#new', as: :new_user_registration
......@@ -30,4 +30,12 @@ Rails.application.routes.draw do
post 'login', to: 'users/sessions#create', as: :user_session
delete 'logout', to: 'users/sessions#destroy', as: :destroy_user_session, via: Devise.mappings[:user].sign_out_via
end
get 'admin/applies', to: 'admins#index', as: 'admin_applies'
devise_for :admins, path: 'admins', skip: :sessions
devise_scope :admin do
get 'admin/login', to: 'admins/sessions#new', as: :new_admin_session
post 'admin/login', to: 'admins/sessions#create', as: :admin_session
delete 'admin/logout', to: 'admins/sessions#destroy', as: :destroy_admin_session, via: Devise.mappings[:admin].sign_out_via
end
get 'admin/export', to: 'admins#export', as: 'export_csv'
end
\ No newline at end of file
# frozen_string_literal: true
class DeviseCreateAdmins < ActiveRecord::Migration[6.1]
def change
create_table :admins do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.string :current_sign_in_ip
# t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :admins, :email, unique: true
add_index :admins, :reset_password_token, unique: true
# add_index :admins, :confirmation_token, unique: true
# add_index :admins, :unlock_token, unique: true
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_08_18_152915) do
ActiveRecord::Schema.define(version: 2021_10_25_040229) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false
......@@ -40,6 +40,18 @@ ActiveRecord::Schema.define(version: 2021_08_18_152915) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
create_table "admins", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_admins_on_email", unique: true
t.index ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true
end
create_table "apply_jobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "job_id", null: false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment