diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 4932e18..6d2c907 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -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 @@ -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 diff --git a/app/controllers/admin/projects_controller.rb b/app/controllers/admin/projects_controller.rb index f54d0d9..a7403a2 100644 --- a/app/controllers/admin/projects_controller.rb +++ b/app/controllers/admin/projects_controller.rb @@ -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 diff --git a/app/controllers/phase_revision_controller.rb b/app/controllers/phase_revision_controller.rb new file mode 100644 index 0000000..82a4b62 --- /dev/null +++ b/app/controllers/phase_revision_controller.rb @@ -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 diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 44d2a38..83b39cd 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -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 diff --git a/app/helpers/pages_helper.rb b/app/helpers/pages_helper.rb index 3b048e0..0cbc64b 100644 --- a/app/helpers/pages_helper.rb +++ b/app/helpers/pages_helper.rb @@ -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 \ No newline at end of file diff --git a/app/jobs/initialization_of_topics_to_sheets_job.rb b/app/jobs/initialization_of_topics_to_sheets_job.rb index ecf5fc0..dbc78d0 100644 --- a/app/jobs/initialization_of_topics_to_sheets_job.rb +++ b/app/jobs/initialization_of_topics_to_sheets_job.rb @@ -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']) @@ -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 @@ -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, diff --git a/app/jobs/sync_topic_job.rb b/app/jobs/sync_topic_job.rb index 86c8044..ac32ef9 100644 --- a/app/jobs/sync_topic_job.rb +++ b/app/jobs/sync_topic_job.rb @@ -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 diff --git a/app/models/page.rb b/app/models/page.rb index 94636e7..1e2aef7 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -35,4 +35,77 @@ def published? def synced? published_revision == latest_revision end + + def publish_and_enqueue_jobs(revision) + new_revision = update_associated_phase_revision(revision) + + if new_revision + updates = build_publish_updates(new_revision) + UpdateMultipleSheetColumnsJob.perform_later(id, updates) + ExportTopicIntoSheetJob.perform_later(new_revision) + end + end + + def update_associated_phase_revision(revision) + related_revisions = PhaseRevision.where(revision_id: revisions.ids).where.not(revision_id: revision.id) + related_revisions.update_all(published: false) if revision + + new_revision = PhaseRevision.find_by(revision_id: revision.id) + if new_revision + new_revision.update!(published: true, was_published: true, published_at: Time.now) + new_revision + end + end + + def build_publish_updates(revision) + [ + { + column_names: { "Prípravná fáza" => "Príprava publikovaná?", "Fáza produkt" => "Produkt publikovaný?" }, + page_type: revision.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: revision.phase.phase_type.name, + published_value: 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: 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: revision.phase.phase_type.name, + published_value: %(=HYPERLINK("https://redflags.slovensko.digital/admin/pages/#{id}"; "Admin link")) + } + ] + end + + def unpublish_and_enqueue_jobs(revision_id) + PhaseRevision.where(revision_id: revision_id).update_all(published: false, published_at: nil) if revision_id + + updates = build_unpublish_updates + UpdateMultipleSheetColumnsJob.perform_later(id, updates) + end + + def build_unpublish_updates + [ + { + column_names: { "Prípravná fáza" => "Príprava publikovaná?", "Fáza produkt" => "Produkt publikovaný?" }, + page_type: 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: phase.phase_type.name, + published_value: "" + }, + { + column_names: { "Prípravná fáza" => "RF web príprava", "Fáza produkt" => "RF web produkt" }, + page_type: phase.phase_type.name, + published_value: "" + } + ] + end end diff --git a/app/models/phase_revision.rb b/app/models/phase_revision.rb index 45b86f7..b062e4a 100644 --- a/app/models/phase_revision.rb +++ b/app/models/phase_revision.rb @@ -52,7 +52,11 @@ class PhaseRevision < ApplicationRecord scope :published, -> { where(published: true) } scope :once_published, -> { where(was_published: true, published: false) } - # TODO move elsewhere? + ROUTE_MAP = { + 'Prípravná fáza' => 'hodnotenie-pripravy', + 'Fáza produkt' => 'hodnotenie-produktu' + }.freeze + def load_from_data(raw) self.title = raw['title'].gsub('Red Flags:', '').strip @@ -177,4 +181,37 @@ def load_ratings(raw) self.total_score = total_score self.maximum_score = maximum_score end + + def self.map_phase_type_to_route(phase_type) + ROUTE_MAP[phase_type] || phase_type + end + + def self.find_published_revision(project_id, revision_type) + phase_name = map_revision_type_to_phase_name(revision_type) + joins(phase: { project: :phases }) + .where(projects: { id: project_id }) + .where(phases: { phase_type: PhaseType.find_by(name: phase_name) }) + .where(published: true) + .first + end + + def self.find_revision_history(project_id, revision_type, version) + phase_name = map_revision_type_to_phase_name(revision_type) + joins(phase: { project: :phases }) + .joins(:revision) + .where(projects: { id: project_id }) + .where(phases: { phase_type: PhaseType.find_by(name: phase_name) }) + .where(revisions: { version: version }) + .first + end + + private + + def self.map_revision_type_to_phase_name(revision_type) + phase_type_map = { + 'hodnotenie-pripravy' => 'Prípravná fáza', + 'hodnotenie-produktu' => 'Fáza produkt' + } + phase_type_map[revision_type] || revision_type + end end diff --git a/app/models/phase_revision_rating.rb b/app/models/phase_revision_rating.rb index 058edea..48ce546 100644 --- a/app/models/phase_revision_rating.rb +++ b/app/models/phase_revision_rating.rb @@ -17,7 +17,7 @@ # Foreign Keys # # fk_rails_... (rating_type_id => rating_types.id) -# fk_rails_... (phase_revision_id => phase_revisions.id) +# fk_rails_... (phase_revision_id => phase_revision.id) # class PhaseRevisionRating < ApplicationRecord diff --git a/app/models/project.rb b/app/models/project.rb index 5cb385b..9ede6c4 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -14,12 +14,58 @@ def has_published_phases? phases.any? { |phase| phase.published_revision.present? } end -=begin - has_many :revisions, class_name: 'PhaseRevision' + def self.filtered_projects(selected_tag, sort_param) - has_many :published_revisions, -> { where(published: true) }, class_name: 'PhaseRevision' + case sort_param + 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', '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') + 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') }) + .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 = Project.joins(phases: :published_revision).distinct - scope :published, -> { where.not(projects: { published_revision_id: nil }) } - scope :with_tag, -> (tag) { joins(published_revision: :revision).where("? = ANY(revisions.tags)", tag) } -=end + if ProjectsHelper::ALLOWED_TAGS.keys.include?(selected_tag) + projects = Project.joins(phases: :published_revision) + .where(phase_revisions: { tags: selected_tag }) + end + end + + projects + end end diff --git a/app/views/admin/pages/show.html.erb b/app/views/admin/pages/show.html.erb index 7cf253e..7b16cdf 100644 --- a/app/views/admin/pages/show.html.erb +++ b/app/views/admin/pages/show.html.erb @@ -44,7 +44,7 @@
<%= link_to projects_path(tag: tag) do%> @@ -74,7 +74,7 @@
@@ -102,7 +102,7 @@