diff --git a/app/controllers/admin/communication/website/home_controller.rb b/app/controllers/admin/communication/website/home_controller.rb index abad549c17dec8c3a0deb45dd5a30051ffebf3df..55abf8429513cc95e48c23f871ec7b6afc5eff1f 100644 --- a/app/controllers/admin/communication/website/home_controller.rb +++ b/app/controllers/admin/communication/website/home_controller.rb @@ -4,10 +4,11 @@ class Admin::Communication::Website::HomeController < Admin::Communication::Webs def edit breadcrumb add_breadcrumb Communication::Website::Home.model_name.human + render 'admin/communication/website/homes/edit' end def update - if @home.update(home_params) + if @home.update_and_sync(home_params) redirect_to admin_communication_website_path(@website), notice: t('admin.successfully_updated_html', model: Communication::Website::Home.model_name.human) else breadcrumb diff --git a/app/controllers/admin/communication/website/menus_controller.rb b/app/controllers/admin/communication/website/menus_controller.rb index 5fa0505fcfe8fdc027a87f1a110803a0ab66cda7..499f917b3805cd34e629ad454a19d49532c8ad85 100644 --- a/app/controllers/admin/communication/website/menus_controller.rb +++ b/app/controllers/admin/communication/website/menus_controller.rb @@ -24,7 +24,7 @@ class Admin::Communication::Website::MenusController < Admin::Communication::Web def create @menu.website = @website - if @menu.save + if @menu.save_and_sync redirect_to admin_communication_website_menu_path(@menu), notice: t('admin.successfully_created_html', model: @menu.to_s) else breadcrumb @@ -33,7 +33,7 @@ class Admin::Communication::Website::MenusController < Admin::Communication::Web end def update - if @menu.update(menu_params) + if @menu.update_and_sync(menu_params) redirect_to admin_communication_website_menu_path(@menu), notice: t('admin.successfully_updated_html', model: @menu.to_s) else breadcrumb @@ -43,7 +43,7 @@ class Admin::Communication::Website::MenusController < Admin::Communication::Web end def destroy - @menu.destroy + @menu.destroy_and_sync redirect_to admin_communication_website_menus_url, notice: t('admin.successfully_destroyed_html', model: @menu.to_s) end diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb index c0a075a2ad818a1eae45f0e82421e338e8384663..540b9821d8e7d81dce8dc07f03fcdf1e011ab6ee 100644 --- a/app/controllers/admin/communication/websites_controller.rb +++ b/app/controllers/admin/communication/websites_controller.rb @@ -45,7 +45,7 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati def create @website.university = current_university - if @website.save + if @website.save_and_sync redirect_to [:admin, @website], notice: t('admin.successfully_created_html', model: @website.to_s) else breadcrumb @@ -54,7 +54,7 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati end def update - if @website.update(website_params) + if @website.update_and_sync(website_params) redirect_to [:admin, @website], notice: t('admin.successfully_updated_html', model: @website.to_s) else breadcrumb diff --git a/app/controllers/admin/education/programs_controller.rb b/app/controllers/admin/education/programs_controller.rb index bfe44a0bf666f8b48eeaab15357487309076b609..137c9dfe7a226b644b10b658f9515f8214a55ca2 100644 --- a/app/controllers/admin/education/programs_controller.rb +++ b/app/controllers/admin/education/programs_controller.rb @@ -53,7 +53,7 @@ class Admin::Education::ProgramsController < Admin::Education::ApplicationContro def create @program.university = current_university - if @program.save + if @program.save_and_sync redirect_to [:admin, @program], notice: t('admin.successfully_created_html', model: @program.to_s) else breadcrumb @@ -62,7 +62,7 @@ class Admin::Education::ProgramsController < Admin::Education::ApplicationContro end def update - if @program.update(program_params) + if @program.update_and_sync(program_params) redirect_to [:admin, @program], notice: t('admin.successfully_updated_html', model: @program.to_s) else breadcrumb @@ -72,7 +72,7 @@ class Admin::Education::ProgramsController < Admin::Education::ApplicationContro end def destroy - @program.destroy + @program.destroy_and_sync redirect_to admin_education_programs_url, notice: t('admin.successfully_destroyed_html', model: @program.to_s) end diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index 9fafe6d6602cc0e435ecb548de8d6220929443da..86dc769006126182703209e5c82f932904370180 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -28,68 +28,19 @@ # class Communication::Website < ApplicationRecord include WithGitRepository - include WithCategories + include WithGit + include WithHome + include WithAbouts + include WithImport belongs_to :university - belongs_to :about, - polymorphic: true, - optional: true - has_one :home, - class_name: 'Communication::Website::Home', - foreign_key: :communication_website_id, - dependent: :destroy - has_many :pages, - foreign_key: :communication_website_id, - dependent: :destroy - has_many :posts, - foreign_key: :communication_website_id, - dependent: :destroy - has_many :categories, - class_name: 'Communication::Website::Category', - foreign_key: :communication_website_id, - dependent: :destroy - has_many :menus, - class_name: 'Communication::Website::Menu', - foreign_key: :communication_website_id, - dependent: :destroy - has_one :imported_website, - class_name: 'Communication::Website::Imported::Website', - dependent: :destroy - has_many :git_files, - class_name: 'Communication::Website::GitFile', - dependent: :destroy - - after_create :create_home - after_save :publish_about_object, if: :saved_change_to_about_id? - after_save_commit :set_programs_categories!, if: -> (website) { website.about_school? } scope :ordered, -> { order(:name) } - def self.about_types - [nil, Education::School.name, Research::Journal.name] - end - def to_s "#{name}" end - def programs - about_school? ? about.programs : Education::Program.none - end - - def import! - imported_website = Communication::Website::Imported::Website.where( - website: self, university: university - ).first_or_create unless imported? - - imported_website.run! - imported_website - end - - def imported? - !imported_website.nil? - end - def list_of_pages all_pages = [] pages.root.ordered.each do |page| @@ -114,13 +65,11 @@ class Communication::Website < ApplicationRecord all_programs end - protected - - def create_home - build_home(university_id: university_id).save + def git_path_static + "data/website.yml" end - def about_school? - about_type == 'Education::School' + def git_dependencies_static + pages + posts + categories + menus + members + [home] end end diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb index 7a85e55323e14c49e5988ecdaa3c6bfb13ad05dd..2d9d3ed3b382a64489928ce3eb54170f3f170865 100644 --- a/app/models/communication/website/git_file.rb +++ b/app/models/communication/website/git_file.rb @@ -61,7 +61,10 @@ class Communication::Website::GitFile < ApplicationRecord @to_s ||= ApplicationController.render( template: "admin/#{about.class.name.underscore.pluralize}/#{identifier}", layout: false, - assigns: { about.class.name.demodulize.downcase => about } + assigns: { + about.class.name.demodulize.downcase => about, + website: website + } ) end @@ -70,72 +73,4 @@ class Communication::Website::GitFile < ApplicationRecord def git_sha @git_sha ||= website.git_repository.git_sha previous_path end - - # def add_media_to_batch(github) - # return unless manifest_data[:has_media] && about.respond_to?(:active_storage_blobs) - # about.active_storage_blobs.each { |blob| add_blob_to_batch(github, blob) } - # end - # - # def add_blob_to_batch(github, blob) - # github.add_to_batch github_blob_params(blob) - # end - # - # def remove_from_github - # return unless github.valid? - # github.remove github_path, github_remove_commit_message - # remove_media_from_github - # end - # - # def remove_media_from_github - # return unless manifest_data[:with_media] && about.respond_to?(:active_storage_blobs) - # about.active_storage_blobs.each { |blob| remove_blob_from_github(blob) } - # end - # - # def remove_blob_from_github(blob) - # github.remove github_blob_path(blob), github_blob_remove_commit_message - # end - # - # def github_params - # { - # path: manifest_data[:generated_path].call(self), - # previous_path: github_path, - # data: manifest_data[:data].call(self) - # } - # end - # - # def github_blob_params(blob) - # blob.analyze unless blob.analyzed? - # { - # path: github_blob_path(blob), - # data: ApplicationController.render( - # template: 'active_storage/blobs/static', - # layout: false, - # assigns: { blob: blob } - # ) - # } - # end - # - # def github_blob_path(blob) - # "data/media/#{blob.id[0..1]}/#{blob.id}.yml" - # end - # - # def github_commit_message - # "[#{about.class.name.demodulize} - #{manifest_identifier}] Save #{about.to_s}" - # end - # - # def github_remove_commit_message - # "[#{about.class.name.demodulize} - #{manifest_identifier}] Remove #{about.to_s}" - # end - # - # def github_blob_remove_commit_message(blob) - # "[Medium] Remove ##{blob.id}" - # end - # - # def valid_for_publication? - # if about.respond_to?(:published) - # about.published? - # else - # true - # end - # end end diff --git a/app/models/communication/website/home.rb b/app/models/communication/website/home.rb index 23370016c83075466c1f9878397cd279628560db..0228b9258d96c54e1c5f93e6d10f58aeb1865788 100644 --- a/app/models/communication/website/home.rb +++ b/app/models/communication/website/home.rb @@ -31,19 +31,10 @@ class Communication::Website::Home < ApplicationRecord has_one_attached_deletable :featured_image def to_s - '' - end - - def to_static(github_file) - ApplicationController.render( - template: "admin/communication/website/home/static", - layout: false, - assigns: { home: self, github_file: github_file } - ) + website.to_s end def git_path_static 'content/_index.html' end - end diff --git a/app/models/communication/website/with_abouts.rb b/app/models/communication/website/with_abouts.rb new file mode 100644 index 0000000000000000000000000000000000000000..a352fb8fc5bde6ec7edff4313d2dadaf249b2b74 --- /dev/null +++ b/app/models/communication/website/with_abouts.rb @@ -0,0 +1,76 @@ +module Communication::Website::WithAbouts + extend ActiveSupport::Concern + + included do + belongs_to :about, + polymorphic: true, + optional: true + + has_many :pages, + foreign_key: :communication_website_id, + dependent: :destroy + + has_many :menus, + class_name: 'Communication::Website::Menu', + foreign_key: :communication_website_id, + dependent: :destroy + + has_many :posts, + foreign_key: :communication_website_id, + dependent: :destroy + + has_many :categories, + class_name: 'Communication::Website::Category', + foreign_key: :communication_website_id, + dependent: :destroy + + def self.about_types + [nil, Education::School.name, Research::Journal.name] + end + + after_save_commit :set_programs_categories!, if: -> (website) { website.about_school? } + end + + def about_school? + about_type == 'Education::School' + end + + def programs + about_school? ? about.programs : Education::Program.none + end + + def members + @members ||= ( + posts.collect(&:author) + + programs.collect(&:members).flatten + # TODO researchers via articles + ).uniq.compact + end + + protected + + def set_programs_categories! + programs_root_category = categories.where(is_programs_root: true).first_or_create( + name: 'Offre de formation', + slug: 'offre-de-formation', + is_programs_root: true, + university_id: university.id + ) + set_programs_categories_at_level! programs_root_category, about.programs.root.ordered + end + + def set_programs_categories_at_level!(parent_category, programs) + programs.map.with_index do |program, index| + category = categories.where(program_id: program.id).first_or_initialize( + name: program.name, + slug: program.name.parameterize, + university_id: university.id + ) + category.parent = parent_category + category.position = index + 1 + category.save + children = about.programs.where(parent_id: program.id).ordered + set_programs_categories_at_level! category, children + end + end +end diff --git a/app/models/communication/website/with_categories.rb b/app/models/communication/website/with_categories.rb deleted file mode 100644 index 9548f6a337614142bd9d4d570eea934121ef1628..0000000000000000000000000000000000000000 --- a/app/models/communication/website/with_categories.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Communication::Website::WithCategories - extend ActiveSupport::Concern - - included do - - def set_programs_categories! - programs_root_category = categories.where(is_programs_root: true).first_or_create( - name: 'Offre de formation', - slug: 'offre-de-formation', - is_programs_root: true, - university_id: university.id, - skip_github_publication: true - ) - programs_categories = [programs_root_category, programs_categories_level(programs_root_category, about.programs.root.ordered)].flatten - github.send_batch_to_website(programs_categories, message: '[Category] Set programs categories.') - end - - protected - - def programs_categories_level(parent_category, programs) - programs.map.with_index do |program, index| - program_category = categories.where(program_id: program.id).first_or_initialize( - name: program.name, - slug: program.name.parameterize, - university_id: university.id - ) - program_category.parent = parent_category - program_category.position = index + 1 - program_category.skip_github_publication = true - program_category.save - program_children = about.programs.where(parent_id: program.id).ordered - [program_category, programs_categories_level(program_category, program_children)].flatten - end - end - - end -end diff --git a/app/models/communication/website/with_git_repository.rb b/app/models/communication/website/with_git_repository.rb index 9317c1ba5c4e915ebc5712a0bbd0a49b26dc7b36..c283b79495f4005fe578831c2385cb9aa1d85f35 100644 --- a/app/models/communication/website/with_git_repository.rb +++ b/app/models/communication/website/with_git_repository.rb @@ -1,6 +1,12 @@ module Communication::Website::WithGitRepository extend ActiveSupport::Concern + included do + has_many :git_files, + class_name: 'Communication::Website::GitFile', + dependent: :destroy + end + def git_repository @git_repository ||= Git::Repository.new self end diff --git a/app/models/communication/website/with_home.rb b/app/models/communication/website/with_home.rb new file mode 100644 index 0000000000000000000000000000000000000000..887713e891e4c9625d59d78a8e24267396db371b --- /dev/null +++ b/app/models/communication/website/with_home.rb @@ -0,0 +1,18 @@ +module Communication::Website::WithHome + extend ActiveSupport::Concern + + included do + has_one :home, + class_name: 'Communication::Website::Home', + foreign_key: :communication_website_id, + dependent: :destroy + + after_create :create_home + end + + protected + + def create_home + build_home(university_id: university_id).save + end +end diff --git a/app/models/communication/website/with_import.rb b/app/models/communication/website/with_import.rb new file mode 100644 index 0000000000000000000000000000000000000000..2d9f76b089c5da719f20a13d6c0f28ec941e913a --- /dev/null +++ b/app/models/communication/website/with_import.rb @@ -0,0 +1,22 @@ +module Communication::Website::WithImport + extend ActiveSupport::Concern + + included do + has_one :imported_website, + class_name: 'Communication::Website::Imported::Website', + dependent: :destroy + end + + def import! + imported_website = Communication::Website::Imported::Website.where( + website: self, university: university + ).first_or_create unless imported? + + imported_website.run! + imported_website + end + + def imported? + !imported_website.nil? + end +end diff --git a/app/models/communication/website/with_publishable_objects.rb b/app/models/communication/website/with_publishable_objects.rb deleted file mode 100644 index d2c019325fba29f37893bd2c4262af28c01540c7..0000000000000000000000000000000000000000 --- a/app/models/communication/website/with_publishable_objects.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Communication::Website::WithPublishableObjects - extend ActiveSupport::Concern - - included do - def publish_about_object - # TODO: Handle Research::Journal then use the commented version. - # about.force_publish! unless about.nil? - about.force_publish! if about.is_a?(Education::School) - end - - end -end diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb index 37f41564dcd6a654827d3442c743f23fcdcac298..2a495b996a79880d1a58941c921a205e20e30c84 100644 --- a/app/models/concerns/with_git.rb +++ b/app/models/concerns/with_git.rb @@ -13,11 +13,6 @@ module WithGit raise NotImplementedError end - # Overridden if websites relation exists - def websites - [website] - end - def save_and_sync if save sync_with_git @@ -42,7 +37,7 @@ module WithGit end def sync_with_git - websites.each do |website| + websites_with_fallback.each do |website| identifiers.each do |identifier| Communication::Website::GitFile.sync website, self, identifier dependencies = send "git_dependencies_#{identifier}" @@ -57,6 +52,18 @@ module WithGit protected + def websites_with_fallback + if is_a? Communication::Website + [self] + elsif respond_to?(:websites) + websites + elsif respond_to?(:website) + [website] + else + [] + end + end + # Overridden for multiple files generation def identifiers [:static] diff --git a/app/views/admin/communication/website/home/edit.html.erb b/app/views/admin/communication/website/homes/edit.html.erb similarity index 100% rename from app/views/admin/communication/website/home/edit.html.erb rename to app/views/admin/communication/website/homes/edit.html.erb diff --git a/app/views/admin/communication/website/home/static.html.erb b/app/views/admin/communication/website/homes/static.html.erb similarity index 100% rename from app/views/admin/communication/website/home/static.html.erb rename to app/views/admin/communication/website/homes/static.html.erb diff --git a/app/views/admin/communication/websites/static.html.erb b/app/views/admin/communication/websites/static.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..b35a0af7b690453e82bb7c08246805543c8d66d7 --- /dev/null +++ b/app/views/admin/communication/websites/static.html.erb @@ -0,0 +1,17 @@ +title: > + <%= @website.to_s %> +<% if @website.about_school? %> +school: + name: > + <%= @website.about.to_s %> + address: > + <%= @website.about.address %> + zipcode: > + <%= @website.about.zipcode %> + city: > + <%= @website.about.city %> + country: > + <%= @website.about.country %> + phone: > + <%= @website.about.phone %> +<% end %> diff --git a/app/views/admin/education/programs/static.html.erb b/app/views/admin/education/programs/static.html.erb index 7e735294c3db50522f63cca61106125fcc9f34d1..cc3f49921775c3efb082894172ea57311af2340c 100644 --- a/app/views/admin/education/programs/static.html.erb +++ b/app/views/admin/education/programs/static.html.erb @@ -1,7 +1,7 @@ --- title: > <%= @program.name %> -url: /<%= @github_file.website.programs_github_directory %><%= @program.path %> +url: /<%= @website.programs_github_directory %><%= @program.path %> description: > <%= prepare_for_github @program.description, @program.university %> position: <%= @program.position %> @@ -10,7 +10,7 @@ image: "<%= @program.best_featured_image.blob.id %>" image_alt: "<%= @program.featured_image_alt %>" <% end %> categories: - - "<%= @github_file.website.categories.find_by(program_id: @program.id)&.id %>" + - "<%= @website.categories.find_by(program_id: @program.id)&.id %>" teachers: - "marlene-dulaurans" - "arnaud-levy"