Skip to content

Commit

Permalink
Logic changes based on review, missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasdrga committed Jun 23, 2024
1 parent 2d8d8bf commit ddbc1f4
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 226 deletions.
61 changes: 6 additions & 55 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,14 @@ def preview

def publish
if params['version'] == 'latest'
revision_id = @page.latest_revision.id
@page.update!(published_revision: @page.latest_revision)
revision = @page.latest_revision
@page.update!(published_revision: revision)
else
revision_id = @page.revisions.find_by!(version: params['version']).id
@page.update!(published_revision: @page.revisions.find_by!(version: params['version']))
revision = @page.revisions.find_by!(version: params['version'])
@page.update!(published_revision: revision)
end

related_revisions = PhaseRevision.where(revision_id: @page.revisions.ids).where.not(revision_id: revision_id)
related_revisions.update_all(published: false) if revision_id

new_revision = PhaseRevision.find_by(revision_id: revision_id)
new_revision.update!(published: true, was_published: true, published_at: Time.now) if new_revision

updates = [
{
column_names: {"Prípravná fáza" => "Príprava publikovaná?", "Fáza produkt" => "Produkt publikovaný?"},
page_type: @page.phase.phase_type.name,
published_value: "Áno"
},
{
column_names: {"Prípravná fáza" => "Dátum publikácie prípravy", "Fáza produkt" => "Dátum publikácie produktu"},
page_type: @page.phase.phase_type.name,
published_value: new_revision.published_at.in_time_zone('Europe/Bratislava').strftime('%H:%M %d.%m.%Y')
},
{
column_names: {"project" => "Dátum poslednej aktualizácie"},
page_type: "project",
published_value: new_revision.published_at.in_time_zone('Europe/Bratislava').strftime('%H:%M %d.%m.%Y')
},
{
column_names: {"Prípravná fáza" => "RF web príprava", "Fáza produkt" => "RF web produkt"},
page_type: @page.phase.phase_type.name,
published_value: %(=HYPERLINK("https://redflags.slovensko.digital/admin/pages/#{@page.id}"; "Admin link"))
}
]
UpdateMultipleSheetColumnsJob.perform_later(@page.id, updates)
ExportTopicIntoSheetJob.perform_later(new_revision)
@page.publish_and_enqueue_jobs(revision)

redirect_back fallback_location: { action: :index }
end
Expand All @@ -79,27 +50,7 @@ def unpublish
revision_id = @page.published_revision.id if @page.published_revision.present?

@page.update!(published_revision: nil)

PhaseRevision.where(revision_id: revision_id).update_all(published: false, published_at: nil) if revision_id

updates = [
{
column_names: {"Prípravná fáza" => "Príprava publikovaná?", "Fáza produkt" => "Produkt publikovaný?"},
page_type: @page.phase.phase_type.name,
published_value: "Nie"
},
{
column_names: {"Prípravná fáza" => "Dátum publikácie prípravy", "Fáza produkt" => "Dátum publikácie produktu"},
page_type: @page.phase.phase_type.name,
published_value: ""
},
{
column_names: {"Prípravná fáza" => "RF web príprava", "Fáza produkt" => "RF web produkt"},
page_type: @page.phase.phase_type.name,
published_value: ""
}
]
UpdateMultipleSheetColumnsJob.perform_later(@page.id, updates)
@page.unpublish_and_enqueue_jobs(revision_id)

redirect_back fallback_location: { action: :index }
end
Expand Down
11 changes: 1 addition & 10 deletions app/controllers/admin/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
class Admin::ProjectsController < AdminController
before_action :load_project, only: [:update]

def update
@project.update!(project_params)
redirect_to admin_page_path(@project.pages.first)
end
before_action :load_project

private

def load_project
@project = Project.find(params[:id])
end

def project_params
params.require(:project).permit(:category)
end
end
43 changes: 43 additions & 0 deletions app/controllers/phase_revision_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class PhaseRevisionController < ApplicationController
def show
@project = Project.find_by!(id: params[:project_id])
@phase_revision = PhaseRevision.find_published_revision(@project.id, params[:revision_type])

if @phase_revision
@revision = @phase_revision.revision
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)
@metadata.og.title = @revision.title
@metadata.og.description = 'Kolaboratívne hodnotenie projektu metodikou Red Flags.'

render :show
end
end

def pdf
@project = Project.find_by!(id: params[:project_id])
@phase_revision = PhaseRevision.find_published_revision(@project.id, params[:revision_type])

if @phase_revision
@revision = @phase_revision.revision
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)
@metadata.og.title = @revision.title
@metadata.og.description = 'Kolaboratívne hodnotenie projektu metodikou Red Flags.'

render layout: "no_header_footer"
end
end

def show_history
@project = Project.find_by!(id: params[:project_id])
@phase_revision = PhaseRevision.find_revision_history(@project.id, params[:revision_type], params[:version])

if @phase_revision
@revision = @phase_revision.revision
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)
@metadata.og.title = @revision.title
@metadata.og.description = 'Kolaboratívne hodnotenie projektu metodikou Red Flags.'

render :show
end
end
end
127 changes: 1 addition & 126 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,132 +1,7 @@
class ProjectsController < ApplicationController
def show
@project = Project.find_by!(id: params[:project_id])
phase_type_map = { 'hodnotenie-pripravy' => 'Prípravná fáza', 'hodnotenie-produktu' => 'Fáza produkt' }
phase_name = phase_type_map[params[:revision_type]] || params[:revision_type]

@once_published_phase_revisions = PhaseRevision.once_published
.joins(phase: :project)
.where(projects: { id: @project.id })
.where(phases: { phase_type: PhaseType.find_by(name: phase_name) })
.joins(:revision)
.joins(revision: :page)
.order('phase_revisions.published_at DESC')

@phase_revision = PhaseRevision.joins(phase: { project: :phases })
.where(projects: { id: @project.id })
.where(phases: { phase_type: PhaseType.find_by(name: phase_name) })
.where(published: true)
.first
if @phase_revision
@revision = @phase_revision.revision
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)
@metadata.og.title = @revision.title
@metadata.og.description = 'Kolaboratívne hodnotenie projektu metodikou Red Flags.'
end
end

def pdf
@project = Project.find_by!(id: params[:project_id])
phase_type_map = { 'hodnotenie-pripravy' => 'Prípravná fáza', 'hodnotenie-produktu' => 'Fáza produkt' }
phase_name = phase_type_map[params[:revision_type]] || params[:revision_type]

@phase_revision = PhaseRevision.joins(phase: { project: :phases })
.where(projects: { id: @project.id })
.where(phases: { phase_type: PhaseType.find_by(name: phase_name) })
.where(published: true)
.first
if @phase_revision
@revision = @phase_revision.revision
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)
@metadata.og.title = @revision.title
@metadata.og.description = 'Kolaboratívne hodnotenie projektu metodikou Red Flags.'

respond_to do |format|
format.html do
render layout: "no_header_footer"
end
end
end
end

def show_history
@project = Project.find_by!(id: params[:project_id])
phase_type_map = { 'hodnotenie-pripravy' => 'Prípravná fáza', 'hodnotenie-produktu' => 'Fáza produkt' }
phase_name = phase_type_map[params[:revision_type]] || params[:revision_type]

@phase_revision = PhaseRevision.joins(phase: { project: :phases })
.joins(:revision)
.where(projects: { id: @project.id })
.where(phases: { phase_type: PhaseType.find_by(name: phase_name).id })
.where(revisions: { version: params[:version] })
.first

if @phase_revision
@revision = @phase_revision.revision
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)
@metadata.og.title = @revision.title
@metadata.og.description = 'Kolaboratívne hodnotenie projektu metodikou Red Flags.'

render :show
end
end

def index
@selected_tag = params[:tag]
@projects = Project.joins(phases: :published_revision).distinct

if ProjectsHelper::ALLOWED_TAGS.keys.include?(@selected_tag)
@projects = @projects.joins(phases: :published_revision)
.where(phase_revisions: { tags: @selected_tag })
end

case params[:sort]
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')
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'
@projects = Project.joins(phases: :published_revision)
.select('DISTINCT ON (projects.id) projects.*, phase_revisions.title AS alpha_title')
.order('projects.id, alpha_title')
.sort_by(&:alpha_title)
when 'alpha_reverse'
@projects = Project.joins(phases: :published_revision)
.select('DISTINCT ON (projects.id) projects.*, phase_revisions.title AS alpha_title')
.order('projects.id, alpha_title')
.sort_by(&:alpha_title).reverse
when 'preparation_lowest'
@projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Prípravná fáza') })
.select('projects.*, phase_revisions.total_score')
.order('phase_revisions.total_score ASC NULLS LAST')
.distinct
when 'preparation_highest'
@projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Prípravná fáza') })
.select('projects.*, phase_revisions.total_score')
.order('phase_revisions.total_score DESC NULLS LAST')
.distinct
when 'product_lowest'
@projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Fáza produkt') })
.select('projects.*, phase_revisions.total_score')
.order('phase_revisions.total_score ASC NULLS LAST')
.distinct
when 'product_highest'
@projects = Project.joins(phases: :published_revision)
.where(phases: { phase_type: PhaseType.find_by(name: 'Fáza produkt') })
.select('projects.*, phase_revisions.total_score')
.order('phase_revisions.total_score DESC NULLS LAST')
.distinct
else
@projects = @projects.order(updated_at: :desc)
end
@projects = Project.filtered_projects(@selected_tag, params[:sort])
end
end
5 changes: 0 additions & 5 deletions app/helpers/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@ def substitue_iframes(body)
end
doc.to_html
end

def map_page_type_to_route(page_type)
route_map = { 'preparation' => 'hodnotenie-pripravy', 'product' => 'hodnotenie-produktu', 'Prípravná fáza' => 'hodnotenie-pripravy', 'Fáza produkt' => 'hodnotenie-produktu'}
route_map[page_type] || page_type
end
end
5 changes: 3 additions & 2 deletions app/jobs/initialization_of_topics_to_sheets_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class InitializationOfTopicsToSheetsJob < ApplicationJob
queue_as :default

COLUMN_NAMES = ['Projekt', 'Projekt ID', 'Platforma', 'Draft prípravy', 'ID draft prípravy', 'ID prípravy', 'Príprava publikovaná?', 'Dátum publikácie prípravy', 'RF web príprava'].freeze
COLUMN_NAMES = ['Projekt', 'Projekt ID', 'Platforma', 'Dátum poslednej aktualizácie', 'Draft prípravy', 'ID draft prípravy', 'ID prípravy', 'Príprava publikovaná?', 'Dátum publikácie prípravy', 'RF web príprava'].freeze

def perform(topic_id, found_page, project)
uri = URI(ENV['GOOGLE_SHEET_SCRIPT_URL'])
Expand All @@ -15,7 +15,7 @@ def perform(topic_id, found_page, project)

update_sheet_cells(sheets_service, column_indices, response_values.count, values)

ExportTopicIntoSheetJob.set(wait: 15.seconds).perform_later(found_page.revision.phase_revision)
ExportTopicIntoSheetJob.set(wait: 15.seconds).perform_later(found_page.published_revision.phase_revision) if found_page.published_revision.present?
end

private
Expand All @@ -35,6 +35,7 @@ def construct_values(topic_id, found_page, project)
found_page.latest_revision.title,
project.id,
%(=HYPERLINK("https://platforma.slovensko.digital/t/#{title_parametrized}/#{topic_id}"; "Platforma link")),
found_page.published_revision&.updated_at&.in_time_zone('Europe/Bratislava')&.strftime('%H:%M %d.%m.%Y') || '',
'',
'',
topic_id.to_s,
Expand Down
3 changes: 1 addition & 2 deletions app/jobs/sync_topic_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ def perform(project_id, topic_id)

page.latest_revision = revision
page.save!

end

# For initial import of current topics into Google Sheets
InitializationOfTopicsToSheetsJob.set(wait: 15.seconds).perform_later(topic_id, page, project)
#InitializationOfTopicsToSheetsJob.set(wait: 15.seconds).perform_later(topic_id, page, project)
end
end
Loading

0 comments on commit ddbc1f4

Please sign in to comment.