From e728fce64010beac2bc97b160010cc8420ac961a Mon Sep 17 00:00:00 2001 From: Joao Felipe Pimentel Date: Wed, 13 Dec 2023 01:13:40 -0300 Subject: [PATCH] add status query and sort in admissions applications --- .../admission_applications_controller.rb | 59 ++++++++++++++++++- .../admission_applications_helper.rb | 13 +++- .../admissions/admission_application.rb | 1 + 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/app/controllers/admissions/admission_applications_controller.rb b/app/controllers/admissions/admission_applications_controller.rb index 8d00cb0f..1c19f457 100644 --- a/app/controllers/admissions/admission_applications_controller.rb +++ b/app/controllers/admissions/admission_applications_controller.rb @@ -5,6 +5,7 @@ class Admissions::AdmissionApplicationsController < ApplicationController authorize_resource + before_action :update_table_config I18N_BASE = "activerecord.attributes.admissions/admission_application" @@ -58,10 +59,15 @@ class Admissions::AdmissionApplicationsController < ApplicationController } config.columns[:admission_phase].actions_for_association_links = [:show] config.columns[:admission_phase].search_ui = :record_select + config.columns[:admission_phase].sort_by sql: [:admission_phase_id] + + config.columns[:status].search_sql = "" + config.columns[:status].search_ui = :select config.update.columns = [:custom_forms] config.update.multipart = true + config.actions.swap :search, :field_search config.field_search.columns = [ :admission_process, @@ -70,11 +76,39 @@ class Admissions::AdmissionApplicationsController < ApplicationController :email, :is_filled, :pendency, - :admission_phase + :admission_phase, + :status ] config.actions.exclude :deleted_records, :create end + def update_table_config + if current_user + active_scaffold_config.columns[:status].sort_by sql: Arel.sql(" + CASE + WHEN `admission_applications`.`status` IS NOT NULL THEN `admission_applications`.`status` + WHEN `admission_applications`.`id` IN ( + SELECT `admission_application_id` + FROM `admission_pendencies` + WHERE `admission_pendencies`.`status`=\"#{Admissions::AdmissionPendency::PENDENT}\" + AND ( + ( + `admission_pendencies`.`admission_phase_id` IS NULL AND + `admission_applications`.`admission_phase_id` IS NULL + ) OR ( + `admission_pendencies`.`admission_phase_id` = `admission_applications`.`admission_phase_id` + ) + ) + AND `admission_pendencies`.`user_id`=#{current_user.id} + ) THEN \"#{Admissions::AdmissionPendency::PENDENT}\" + ELSE \"-\" + END + ") + else + active_scaffold_config.columns[:status].sort_by sql: "status" + end + end + def self.condition_for_is_filled_column(column, value, like_pattern) filled_form = " select `ff`.`id` from filled_forms `ff` @@ -107,6 +141,29 @@ def self.condition_for_pendency_column(column, value, like_pattern) end end + def self.condition_for_status_column(column, value, like_pattern) + candidate_arel = Admissions::AdmissionApplication.arel_table + return "" if value.blank? + if value.to_i.to_s == value + value = value.to_i + user_id = value.abs + pendency_arel = Admissions::AdmissionPendency.arel_table + pendencies_query = pendency_arel.where( + pendency_arel[:status].eq(Admissions::AdmissionPendency::PENDENT) + .and(pendency_arel[:admission_phase_id].eq(candidate_arel[:admission_phase_id])) + .and(pendency_arel[:user_id].eq(user_id)) + ).project(pendency_arel[:admission_application_id]) + result = candidate_arel[:status].eq(nil) + if value < 0 + [result.and(candidate_arel[:id].not_in(pendencies_query)).to_sql] + else + [result.and(candidate_arel[:id].in(pendencies_query)).to_sql] + end + else + [candidate_arel[:status].eq(value).to_sql] + end + end + def update_authorized?(record = nil, column = nil) return super if record.nil? return true if can?(:override, record) && record.admission_process.staff_can_edit diff --git a/app/helpers/admissions/admission_applications_helper.rb b/app/helpers/admissions/admission_applications_helper.rb index 3e7f0d05..92bb2e2c 100644 --- a/app/helpers/admissions/admission_applications_helper.rb +++ b/app/helpers/admissions/admission_applications_helper.rb @@ -21,7 +21,8 @@ def status_column(record, column) result += record.status elsif current_user.present? && record.pendencies.where( user_id: current_user.id, - status: Admissions::AdmissionPendency::PENDENT + status: Admissions::AdmissionPendency::PENDENT, + admission_phase_id: record.admission_phase_id ).first.present? result += Admissions::AdmissionPendency::PENDENT else @@ -63,4 +64,14 @@ def evaluations_show_column(record, column) association: "evaluations" } end + + def status_search_column(record, options) + select(record, :status, [ + [I18n.t("active_scaffold._select_"), nil], + ["-", -current_user.id], + [Admissions::AdmissionPendency::PENDENT, current_user.id] + ] +Admissions::AdmissionApplication::STATUSES.map do |status| + [status, status] + end, options, options) + end end diff --git a/app/models/admissions/admission_application.rb b/app/models/admissions/admission_application.rb index f8051ec6..cae3b703 100644 --- a/app/models/admissions/admission_application.rb +++ b/app/models/admissions/admission_application.rb @@ -11,6 +11,7 @@ class Admissions::AdmissionApplication < ActiveRecord::Base ERROR = record_i18n_attr("statuses.error") END_OF_PHASE_STATUSES = [APPROVED, REPROVED] + STATUSES = [APPROVED, REPROVED, ERROR] scope :non_consolidated, -> { where(