Skip to content

Commit

Permalink
add status query and sort in admissions applications
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFelipe committed Dec 13, 2023
1 parent 54ff2b5 commit e728fce
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
59 changes: 58 additions & 1 deletion app/controllers/admissions/admission_applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class Admissions::AdmissionApplicationsController < ApplicationController
authorize_resource
before_action :update_table_config

I18N_BASE = "activerecord.attributes.admissions/admission_application"

Expand Down Expand Up @@ -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,
Expand All @@ -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`
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion app/helpers/admissions/admission_applications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions app/models/admissions/admission_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e728fce

Please sign in to comment.