From 97d98f7b2c7aa12f70dd6412e01cecb93947b3fe Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Tue, 19 Oct 2021 12:37:12 +0200 Subject: [PATCH] working parents --- .../communication/website/pages_controller.rb | 3 +- .../communication/website/posts_controller.rb | 2 +- .../communication/website/imported/page.rb | 3 +- .../communication/website/imported/post.rb | 3 +- .../communication/website/imported/website.rb | 10 ++- app/models/communication/website/page.rb | 6 +- app/models/communication/website/post.rb | 4 +- .../website/pages/_list.html.erb | 2 + .../communication/website/pages/show.html.erb | 61 +++++++++++++------ .../communication/website/posts/show.html.erb | 30 +++++---- ...add_slug_to_communication_website_posts.rb | 5 ++ db/schema.rb | 3 +- 12 files changed, 95 insertions(+), 37 deletions(-) create mode 100644 db/migrate/20211019100112_add_slug_to_communication_website_posts.rb diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb index 4e29babb1..dd827a9f8 100644 --- a/app/controllers/admin/communication/website/pages_controller.rb +++ b/app/controllers/admin/communication/website/pages_controller.rb @@ -42,7 +42,8 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web end def destroy - redirect_to admin_communication_website_url, notice: t('admin.successfully_destroyed_html', model: @page.to_s) + @page.destroy + redirect_to admin_communication_website_pages_url(@website), notice: t('admin.successfully_destroyed_html', model: @page.to_s) end protected diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb index 30ad90870..ed5450d9e 100644 --- a/app/controllers/admin/communication/website/posts_controller.rb +++ b/app/controllers/admin/communication/website/posts_controller.rb @@ -42,7 +42,7 @@ class Admin::Communication::Website::PostsController < Admin::Communication::Web end def destroy - @communication_website_post.destroy + @post.destroy redirect_to admin_communication_website_posts_url, notice: t('admin.successfully_destroyed_html', model: @post.to_s) end diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb index 00d49f2d7..16ee4fd3f 100644 --- a/app/models/communication/website/imported/page.rb +++ b/app/models/communication/website/imported/page.rb @@ -64,10 +64,11 @@ class Communication::Website::Imported::Page < ApplicationRecord self.page = Communication::Website::Page.new university: university, website: website.website, # Real website, not imported website slug: path - self.page.title = "TMP" + self.page.title = "Untitled" self.page.save end # TODO only if not modified since import + page.slug = slug page.title = Wordpress.clean title.to_s page.description = Wordpress.clean excerpt.to_s page.text = Wordpress.clean content.to_s diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb index ba052620a..d683be4ca 100644 --- a/app/models/communication/website/imported/post.rb +++ b/app/models/communication/website/imported/post.rb @@ -64,11 +64,12 @@ class Communication::Website::Imported::Post < ApplicationRecord if post.nil? self.post = Communication::Website::Post.new university: university, website: website.website # Real website, not imported website - self.post.title = "TMP" # No title yet + self.post.title = "Untitled" # No title yet self.post.save end # TODO only if not modified since import post.title = Wordpress.clean title.to_s + post.slug = slug post.description = Wordpress.clean excerpt.to_s post.text = Wordpress.clean content.to_s post.published_at = published_at if published_at diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb index 962cd135a..1ffb3b713 100644 --- a/app/models/communication/website/imported/website.rb +++ b/app/models/communication/website/imported/website.rb @@ -45,7 +45,15 @@ class Communication::Website::Imported::Website < ApplicationRecord page.data = data page.save end - # TODO parents + pages.find_each do |page| + next if page.parent.blank? + parent = pages.where(identifier: page.parent).first + next if parent.nil? + generated_page = page.page + generated_page.parent = parent.page + generated_page.save + # TODO save children + end end def sync_posts diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index 3fa0fe501..65c9cdddc 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -39,9 +39,13 @@ class Communication::Website::Page < ApplicationRecord belongs_to :parent, class_name: 'Communication::Website::Page', optional: true + has_many :children, + class_name: 'Communication::Website::Page', + foreign_key: :parent_id has_one :imported_page, class_name: 'Communication::Website::Imported::Page', - foreign_key: :page_id + foreign_key: :page_id, + dependent: :nullify validates :title, presence: true diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index b453f2082..7d40a1fb3 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -6,6 +6,7 @@ # description :text # published :boolean default(FALSE) # published_at :datetime +# slug :text # text :text # title :string # created_at :datetime not null @@ -29,7 +30,8 @@ class Communication::Website::Post < ApplicationRecord foreign_key: :communication_website_id has_one :imported_post, class_name: 'Communication::Website::Imported::Post', - foreign_key: :post_id + foreign_key: :post_id, + dependent: :nullify scope :ordered, -> { order(published_at: :desc, created_at: :desc) } scope :recent, -> { order(published_at: :desc).limit(5) } diff --git a/app/views/admin/communication/website/pages/_list.html.erb b/app/views/admin/communication/website/pages/_list.html.erb index 3d0543ce1..8d714dc6b 100644 --- a/app/views/admin/communication/website/pages/_list.html.erb +++ b/app/views/admin/communication/website/pages/_list.html.erb @@ -3,6 +3,7 @@ <tr> <th><%= Communication::Website::Page.human_attribute_name('title') %></th> <th><%= Communication::Website::Page.human_attribute_name('path') %></th> + <th><%= Communication::Website::Page.human_attribute_name('parent') %></th> <th width="150"></th> </tr> </thead> @@ -11,6 +12,7 @@ <tr> <td><%= link_to page, admin_communication_website_page_path(website_id: page.website.id, id: page.id) %></td> <td><%= page.path %></td> + <td><%= link_to page.parent, admin_communication_website_page_path(website_id: page.website.id, id: page.parent.id) if page.parent %></td> <td class="text-end"> <%= link_to t('edit'), edit_admin_communication_website_page_path(website_id: page.website.id, id: page.id), diff --git a/app/views/admin/communication/website/pages/show.html.erb b/app/views/admin/communication/website/pages/show.html.erb index 3e51aa8ba..98963fb5e 100644 --- a/app/views/admin/communication/website/pages/show.html.erb +++ b/app/views/admin/communication/website/pages/show.html.erb @@ -24,23 +24,50 @@ <div class="card-header"> <h5 class="card-title mb-0">Metadata</h5> </div> - <div class="card-body"> - <p> - <strong>Slug</strong> - <%= @page.slug %> - </p> - <p> - <strong>Path</strong> - <%= @page.path %> - </p> - - <% if @page.imported_page %> - <p> - <strong>Imported from</strong><br> - <a href="<%= @page.imported_page.url %>" target="_blank"><%= @page.imported_page.url %></a> - </p> - <% end %> - </div> + <table class="<%= table_classes %>"> + <tbody> + <tr> + <td><%= Communication::Website::Page.human_attribute_name('slug') %></td> + <td><%= @page.slug %></td> + </tr> + <tr> + <td><%= Communication::Website::Page.human_attribute_name('path') %></td> + <td><%= @page.path %></td> + </tr> + <% if @page.imported_page %> + <tr> + <td>Imported from</td> + <td><a href="<%= @page.imported_page.url %>" target="_blank">Original URL</a></td> + </tr> + <% end %> + <% if @page.parent %> + <tr> + <td><%= Communication::Website::Page.human_attribute_name('parent') %></td> + <td><%= link_to @page.parent, + admin_communication_website_page_path( + website_id: @website.id, + id: @page.parent.id + ) %></td> + </tr> + <% end %> + <% if @page.children.any? %> + <tr> + <td><%= Communication::Website::Page.human_attribute_name('children') %></td> + <td> + <ul class="list-unstyled mb-0"> + <% @page.children.each do |child| %> + <li><%= link_to child, + admin_communication_website_page_path( + website_id: @website.id, + id: child.id + ) %></li> + <% end %> + </ul> + </td> + </tr> + <% end %> + </tbody> + </table> </div> </div> </div> diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb index a3c9b3ab7..c228339cd 100644 --- a/app/views/admin/communication/website/posts/show.html.erb +++ b/app/views/admin/communication/website/posts/show.html.erb @@ -24,18 +24,24 @@ <div class="card-header"> <h5 class="card-title mb-0">Metadata</h5> </div> - <div class="card-body"> - <p> - <strong>Published</strong> - <%= @post.published %> - </p> - <% if @post.imported_post %> - <p> - <strong>Imported from</strong> - <a href="<%= @post.imported_post.url %>" target="_blank"><%= @post.imported_post.url %></a> - </p> - <% end %> - </div> + <table class="<%= table_classes %>"> + <tbody> + <tr> + <td><%= Communication::Website::Page.human_attribute_name('slug') %></td> + <td><%= @post.slug %></td> + </tr> + <tr> + <td><%= Communication::Website::Page.human_attribute_name('published') %></td> + <td><%= @post.published %></td> + </tr> + <% if @post.imported_post %> + <tr> + <td>Imported from</td> + <td><a href="<%= @post.imported_post.url %>" target="_blank">Original URL</a></td> + </tr> + <% end %> + </tbody> + </table> </div> </div> </div> diff --git a/db/migrate/20211019100112_add_slug_to_communication_website_posts.rb b/db/migrate/20211019100112_add_slug_to_communication_website_posts.rb new file mode 100644 index 000000000..e990e2ac3 --- /dev/null +++ b/db/migrate/20211019100112_add_slug_to_communication_website_posts.rb @@ -0,0 +1,5 @@ +class AddSlugToCommunicationWebsitePosts < ActiveRecord::Migration[6.1] + def change + add_column :communication_website_posts, :slug, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 683a249f5..de439897c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_19_092503) do +ActiveRecord::Schema.define(version: 2021_10_19_100112) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -151,6 +151,7 @@ ActiveRecord::Schema.define(version: 2021_10_19_092503) do t.datetime "published_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.text "slug" t.index ["communication_website_id"], name: "index_communication_website_posts_on_communication_website_id" t.index ["university_id"], name: "index_communication_website_posts_on_university_id" end -- GitLab