diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index eddce3c46d84d9db90397544b256592a7b67863b..7054222601774df410ade3cf77ef99518470d602 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -62,7 +62,7 @@ class Communication::Website < ApplicationRecord 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_type == 'Education::School' } + after_save_commit :set_programs_categories!, if: -> (website) { website.about_school? } scope :ordered, -> { order(:name) } @@ -74,12 +74,15 @@ class Communication::Website < ApplicationRecord "#{name}" end + def programs + about_school? ? about.programs : Education::Program.none + end + def import! - unless imported? - self.imported_website = Communication::Website::Imported::Website.where(website: self, university: university) - .first_or_create + imported_website = Communication::Website::Imported::Website.where( + website: self, university: university + ).first_or_create unless imported? - end imported_website.run! imported_website end @@ -104,6 +107,14 @@ class Communication::Website < ApplicationRecord all_categories end + def list_of_programs + all_programs = [] + programs.root.ordered.each do |program| + all_programs.concat(program.self_and_children(0)) + end + all_programs + end + protected def create_home @@ -113,4 +124,8 @@ class Communication::Website < ApplicationRecord def github @github ||= Github.with_website self end + + def about_school? + about_type == 'Education::School' + end end diff --git a/app/models/communication/website/menu/item.rb b/app/models/communication/website/menu/item.rb index 9cb253e4101ac3753b1d5a00c0160d8e670bfb9f..243c6e1a42a586becb7c48c6c818e4d0768253a5 100644 --- a/app/models/communication/website/menu/item.rb +++ b/app/models/communication/website/menu/item.rb @@ -81,7 +81,7 @@ class Communication::Website::Menu::Item < ApplicationRecord end def has_about? - kind_page? || kind_program? || kind_news_category? || kind_news_article + kind_page? || kind_program? || kind_news_category? || kind_news_article? end protected diff --git a/app/views/admin/communication/website/menu/items/_form.html.erb b/app/views/admin/communication/website/menu/items/_form.html.erb index 2beebc68f03e41eabb75da8bfe1defbdf6b19527..97329593860078584a8ecea4830150806ce0a05d 100644 --- a/app/views/admin/communication/website/menu/items/_form.html.erb +++ b/app/views/admin/communication/website/menu/items/_form.html.erb @@ -24,11 +24,14 @@ if item.kind_page? about_collection = @website.list_of_pages elsif item.kind_program? + about_collection = @website.list_of_programs elsif item.kind_news_category? + about_collection = @website.list_of_categories elsif item.kind_news_article? + about_collection = @website.posts.ordered.map { |e| { label: e.to_s, id: e.id } } end else - about_collection = nil + about_collection = [] end %> <%= f.input :about_type, as: :hidden, input_html: { class: 'js-about-type' } %> diff --git a/app/views/admin/communication/website/menu/items/kind_switch.js.erb b/app/views/admin/communication/website/menu/items/kind_switch.js.erb index 71103f36439b36b7d4b65d0f6e1d3ba8699bc4a4..cb32d1252526a3b26625f5bbc0b8134883ed9fcd 100644 --- a/app/views/admin/communication/website/menu/items/kind_switch.js.erb +++ b/app/views/admin/communication/website/menu/items/kind_switch.js.erb @@ -21,11 +21,29 @@ function hideAbout() { %> displayAbout('Communication::Website::Page', "<%= j(raw(options.join(''))) %>"); <% elsif @kind == 'program' %> - displayAbout(); + <% + options = ['<option value="" label=" "></option>'] + @website.list_of_programs.each do |page| + options << "<option value=\"#{page[:id]}\">#{page[:label]}</option>" + end + %> + displayAbout('Education::Program', "<%= j(raw(options.join(''))) %>"); <% elsif @kind == 'news_category' %> - displayAbout(); + <% + options = ['<option value="" label=" "></option>'] + @website.list_of_categories.each do |category| + options << "<option value=\"#{category[:id]}\">#{category[:label]}</option>" + end + %> + displayAbout('Communication::Website::Category', "<%= j(raw(options.join(''))) %>"); <% elsif @kind == 'news_article' %> - displayAbout(); + <% + options = ['<option value="" label=" "></option>'] + @website.posts.ordered.each do |post| + options << "<option value=\"#{post.id}\">#{post.to_s}</option>" + end + %> + displayAbout('Communication::Website::Post', "<%= j(raw(options.join(''))) %>"); <% else %> $('.js-url-field').addClass('d-none'); hideAbout(); diff --git a/app/views/admin/communication/website/menu/items/show.html.erb b/app/views/admin/communication/website/menu/items/show.html.erb index d07ab910e09a342a4c9b3c9713f85f3cf9a47b5c..312dd7bb5f58e865d54b5e10c7aa8377583ab9e8 100644 --- a/app/views/admin/communication/website/menu/items/show.html.erb +++ b/app/views/admin/communication/website/menu/items/show.html.erb @@ -26,7 +26,7 @@ <% if @item.kind_url? %> <h3 class="h5"><%= Communication::Website::Menu::Item.human_attribute_name('url') %></h3> <p><%= link_to @item.url, @item.url, target: '_blank' unless @item.url.blank? %></p> - <% elsif @item.kind_page? %> + <% elsif @item.has_about? %> <h3 class="h5"><%= Communication::Website::Menu::Item.human_attribute_name('about') %></h3> <p><%= link_to_if can?(:read, @item.about), @item.about, [:admin, @item.about] %></p> <% end %>