Skip to content

Commit

Permalink
Fixed admin editing, FE stuff, filters for evaluations, misc stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasdrga committed Sep 24, 2024
1 parent 7ede0ae commit 9cecb36
Show file tree
Hide file tree
Showing 15 changed files with 537 additions and 468 deletions.
34 changes: 0 additions & 34 deletions app/assets/javascripts/metais/projects.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
document.addEventListener('DOMContentLoaded', function() {
let sortDirectionField = document.getElementById('sort_direction');
let sortButton = document.getElementById('ascdesctoggle');
let upIcon = document.getElementById('up-icon');
let downIcon = document.getElementById('down-icon');

let toggleState = sortDirectionField.value || 'desc';

if (toggleState === 'desc') {
upIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
downIcon.style.fill = 'rgb(56, 94, 255)';
} else {
upIcon.style.fill = 'rgb(56, 94, 255)';
downIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
}

sortButton.addEventListener('click', function(event) {
event.preventDefault();

toggleState = (toggleState === 'asc') ? 'desc' : 'asc';
sortDirectionField.value = toggleState;

if (toggleState === 'desc') {
upIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
downIcon.style.fill = 'rgb(56, 94, 255)';
} else {
upIcon.style.fill = 'rgb(56, 94, 255)';
downIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
}

document.getElementById('form').submit();
});
});

$(document).ready(function() {
function openFilterSection(sectionId) {
var $filterSection = $('#' + sectionId);
Expand Down
34 changes: 34 additions & 0 deletions app/assets/javascripts/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,37 @@ document.addEventListener("turbolinks:load", function() {
document.addEventListener("turbolinks:before-render", function() {
window.printCalled = false;
});

document.addEventListener('DOMContentLoaded', function() {
let sortDirectionField = document.getElementById('sort_direction');
let sortButton = document.getElementById('ascdesctoggle');
let upIcon = document.getElementById('up-icon');
let downIcon = document.getElementById('down-icon');

let toggleState = sortDirectionField.value || 'desc';

if (toggleState === 'desc') {
upIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
downIcon.style.fill = 'rgb(56, 94, 255)';
} else {
upIcon.style.fill = 'rgb(56, 94, 255)';
downIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
}

sortButton.addEventListener('click', function(event) {
event.preventDefault();

toggleState = (toggleState === 'asc') ? 'desc' : 'asc';
sortDirectionField.value = toggleState;

if (toggleState === 'desc') {
upIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
downIcon.style.fill = 'rgb(56, 94, 255)';
} else {
upIcon.style.fill = 'rgb(56, 94, 255)';
downIcon.style.fill = 'rgb(100, 100, 100, 0.4)';
}

document.getElementById('form').submit();
});
});
2 changes: 1 addition & 1 deletion app/assets/stylesheets/projects.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#project-show {
@include media-breakpoint-up(md) {
padding-top: 6rem;
padding-top: 3rem;
}

.full-description {
Expand Down
40 changes: 32 additions & 8 deletions app/controllers/admin/metais/project_origins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def create
def edit
@project = Metais::Project.find(params[:project_id])
@project_info = @project.get_project_origin_info
@project_origin = Metais::ProjectOrigin.find(params[:id])
@project_origins = @project.project_origins
@project_origin = Metais::ProjectOrigin.find(params[:id])
@ai_project_origin = @project_origins.joins(:origin_type).find_by(origin_types: { name: 'AI' })

@assumption_events = []
@real_events = []
Expand Down Expand Up @@ -47,17 +48,40 @@ def edit
def update
@project = Metais::Project.find(params[:project_id])
@project_origin = Metais::ProjectOrigin.find(params[:id])

if @project_origin.update(project_origin_params.except(:project_events))
if project_origin_params[:project_events]
event_params = project_origin_params[:project_events]
unless event_params.values.all?(&:blank?)
@project_origin.events.create!(name: event_params[:name], value: event_params[:value], date: event_params[:date], origin_type: Metais::OriginType.find_by(name: 'Human'))

finance_source_mappings = {"Medzirezortný program 0EK Informačné technológie financované zo štátneho rozpočtu" => "Štátny rozpočet"}

current_project_info = @project.get_project_origin_info

new_params = project_origin_params.to_h
changed_params = {}

new_params.each do |field, new_value|
current_value = current_project_info.send(field)

if field == 'finance_source'
current_value_mapped = finance_source_mappings[current_value] || current_value
new_value_mapped = finance_source_mappings[new_value] || new_value

if new_value.present? && new_value_mapped.to_s.strip != current_value_mapped.to_s.strip
changed_params[field] = new_value
end
else
if field == 'end_date' || field == 'start_date'
new_value = new_value.present? ? Date.parse(new_value.to_s) : nil
current_value = current_value.present? ? current_value.to_date : nil
end

if new_value.present? && new_value.to_s.strip != current_value.to_s.strip
changed_params[field] = new_value
end
end
end

if changed_params.any? && @project_origin.update(changed_params)
redirect_to edit_admin_metais_project_project_origin_path(@project, @project_origin), notice: 'Projekt bol úspešne aktualizovaný.'
else
render :edit
redirect_to edit_admin_metais_project_project_origin_path(@project, @project_origin), notice: 'Žiadne nové informácie pre projekt.'
end
end

Expand Down
31 changes: 0 additions & 31 deletions app/controllers/admin/metais/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ def index
@projects = Metais::Project.filtered_and_sorted_projects(params)
end

def show
@project = Metais::Project.find(params[:id])
end
def edit
@project = Metais::Project.find(params[:id])
end

def update
@project = Metais::Project.find(params[:id])
if @project.update(project_params)
redirect_to admin_metais_project_path(@project), notice: 'Project was successfully updated.'
else
render :edit
end
end

def create_human_origin
@project = Metais::Project.find(params[:id])
origin_type = Metais::OriginType.find_by(name: 'Human')
Expand All @@ -44,19 +28,4 @@ def run_ai_extraction

redirect_to admin_metais_project_path @project
end

private

def filtering_params_present?
params[:guarantor].present? ||
params[:title].present? ||
params[:status].present? ||
params[:code].present? ||
params[:min_price].present? ||
params[:max_price].present?
end

def project_params
params.require(:project).permit(:guarantor, :finance_source, :status)
end
end
6 changes: 3 additions & 3 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ProjectsController < ApplicationController

def index
@selected_tag = params[:tag]
@projects = Project.filtered_projects(@selected_tag, params[:sort])
@projects = Project.filtered_and_sorted_projects(params)
end

end
11 changes: 9 additions & 2 deletions app/models/metais/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def evaluations
end

def get_project_origin_info
finance_source_mappings = {"Medzirezortný program 0EK Informačné technológie financované zo štátneho rozpočtu" => "Štátny rozpočet"}

fields = %w[title status phase description guarantor project_manager start_date end_date
finance_source investment operation approved_investment approved_operation
supplier supplier_cin targets_text events_text documents_text links_text updated_at]
Expand All @@ -32,6 +34,10 @@ def get_project_origin_info
fields.each do |field|
origin = origins.detect { |origin| !origin.send(field).nil? }
value = origin&.send(field)

if field == 'finance_source' && value
value = finance_source_mappings[value] || value
end
if value
project_info.send("#{field}=", Metais::ValueWithOrigin.new(value, origin.origin_type_id))
end
Expand Down Expand Up @@ -91,9 +97,10 @@ def self.filtered_and_sorted_projects(params)
projects.order("metais.projects.updated_at #{sort_direction}")
when 'price'
projects.order("project_origins.final_investment #{sort_direction} NULLS #{sort_direction == 'ASC' ? 'FIRST' : 'LAST'}")
else
projects.order("metais.projects.updated_at #{sort_direction}")
end
else
sort_direction = params[:sort_direction]&.upcase == 'ASC' ? 'ASC' : 'DESC'
projects.order("metais.projects.updated_at #{sort_direction}")
end

projects.page(page).per(per_page)
Expand Down
122 changes: 71 additions & 51 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,81 @@ class Project < ApplicationRecord
def has_published_phases?
phases.any? { |phase| phase.published_revision.present? }
end

def self.filtered_and_sorted_projects(params)
per_page = 25
page = params[:page] || 1

projects = Project.joins(phases: :published_revision)

def self.filtered_projects(selected_tag, sort_param)
if params[:title].present?
projects = projects.where('phase_revisions.title ILIKE ?', "%#{params[:title]}%")
end

if params[:sort].present?
sort_direction = params[:sort_direction]&.upcase == 'ASC' ? 'ASC' : 'DESC'

case sort_param
when 'oldest'
projects = Project.joins(phases: :published_revision)
.select('projects.*, MIN(phase_revisions.published_at) AS oldest_published_at')
.group('projects.id')
.order('oldest_published_at')
when 'alpha', 'alpha_reverse'
projects = Project.joins(phases: :published_revision)
.select('DISTINCT ON (projects.id) projects.*, LOWER(phase_revisions.title) AS alpha_title')
.order('projects.id, alpha_title')
projects = projects.sort_by(&:alpha_title)
projects = projects.reverse if sort_param == 'alpha_reverse'
when 'preparation_lowest'
projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Prípravná fáza') })
.distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Prípravná fáza' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end.reverse
when 'preparation_highest'
projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Prípravná fáza') })
.distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Prípravná fáza' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end
when 'product_lowest'
projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Fáza produkt') })
.distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Fáza produkt' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end.reverse
when 'product_highest'
projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Fáza produkt') })
.distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Fáza produkt' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end
projects = case params[:sort]
when 'alpha'
projects = projects.select('DISTINCT ON (projects.id) projects.*, LOWER(phase_revisions.title) AS alpha_title')
.order("projects.id, alpha_title")
projects = projects.sort_by(&:alpha_title)
projects = projects.reverse if sort_direction == 'DESC'
projects
when 'date'
if sort_direction == 'ASC'
projects = projects.select("projects.*, MIN(phase_revisions.published_at) AS oldest_published_at").group("projects.id")
.order("oldest_published_at")
elsif sort_direction == 'DESC'
projects = projects.select('projects.*, MAX(phase_revisions.published_at) AS newest_published_at').group('projects.id')
.order('newest_published_at DESC')
end
when 'preparation'
if sort_direction == 'ASC'
projects = projects.where(phases: { phase_type: PhaseType.find_by(name: 'Prípravná fáza') }).distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Prípravná fáza' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end.reverse
elsif sort_direction == 'DESC'
projects = projects.where(phases: { phase_type: PhaseType.find_by(name: 'Prípravná fáza') }).distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Prípravná fáza' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end
end
when 'product'
if sort_direction == 'ASC'
projects = projects.where(phases: { phase_type: PhaseType.find_by(name: 'Fáza produkt') }).distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Fáza produkt' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end.reverse

elsif sort_direction == 'DESC'
projects = projects.where(phases: { phase_type: PhaseType.find_by(name: 'Fáza produkt') }).distinct
projects = projects.sort_by do |project|
phase_prep = project.phases.find { |p| p.phase_type.name == 'Fáza produkt' }
phase_prep ? (phase_prep.published_revision.aggregated_rating) : 0
end
end
end
else
# when 'newest'
projects = Project.joins(phases: :published_revision)
.select('projects.*, MAX(phase_revisions.published_at) AS newest_published_at')
.group('projects.id')
.order('newest_published_at DESC')
sort_direction = params[:sort_direction]&.upcase == 'ASC' ? 'ASC' : 'DESC'
if sort_direction == 'DESC'
projects = projects.sort_by do |project|
max_rating = project.phases.map { |p| p.published_revision&.aggregated_rating }.compact.max
max_rating
end
else
projects = projects.sort_by do |project|
max_rating = project.phases.map { |p| p.published_revision&.aggregated_rating }.compact.max
max_rating
end.reverse
end
end


projects = Kaminari.paginate_array(projects).page(page).per(per_page)
projects
end
end
Loading

0 comments on commit 9cecb36

Please sign in to comment.