From e9e536b371e27eeded3b4b2da64470b5824b57f0 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Fri, 8 Oct 2021 15:18:48 +0200 Subject: [PATCH] posts --- .../communication/website/pages_controller.rb | 2 +- .../communication/website/posts_controller.rb | 60 ++++++++++++++++++ .../communication/websites_controller.rb | 1 + app/models/communication/website.rb | 1 + .../communication/website/imported/page.rb | 8 ++- .../communication/website/imported/post.rb | 62 +++++++++++++++++++ .../communication/website/imported/website.rb | 43 ++++++++++--- app/models/communication/website/page.rb | 2 +- app/models/communication/website/post.rb | 42 +++++++++++++ .../website/pages/_form.html.erb | 1 - .../website/pages/_list.html.erb | 8 +-- .../communication/website/pages/new.html.erb | 2 +- .../communication/website/pages/show.html.erb | 2 +- .../website/posts/_form.html.erb | 30 +++++++++ .../website/posts/_list.html.erb | 27 ++++++++ .../communication/website/posts/edit.html.erb | 3 + .../website/posts/index.html.erb | 7 +++ .../communication/website/posts/new.html.erb | 3 + .../communication/website/posts/show.html.erb | 45 ++++++++++++++ .../communication/websites/import.html.erb | 29 +++++++-- .../communication/websites/show.html.erb | 12 +++- config/locales/communication/en.yml | 5 ++ config/locales/communication/fr.yml | 5 ++ config/routes.rb | 3 +- config/routes/communication.rb | 3 +- ...0417_create_communication_website_posts.rb | 15 +++++ ...te_communication_website_imported_posts.rb | 19 ++++++ db/schema.rb | 40 +++++++++++- docs/websites/readme.md | 8 ++- .../website/posts_controller_test.rb | 48 ++++++++++++++ .../communication/website/imported/posts.yml | 53 ++++++++++++++++ test/fixtures/communication/website/posts.yml | 43 +++++++++++++ .../website/imported/post_test.rb | 38 ++++++++++++ .../models/communication/website/post_test.rb | 32 ++++++++++ .../communication/website/posts_test.rb | 55 ++++++++++++++++ 35 files changed, 723 insertions(+), 34 deletions(-) create mode 100644 app/controllers/admin/communication/website/posts_controller.rb create mode 100644 app/models/communication/website/imported/post.rb create mode 100644 app/models/communication/website/post.rb create mode 100644 app/views/admin/communication/website/posts/_form.html.erb create mode 100644 app/views/admin/communication/website/posts/_list.html.erb create mode 100644 app/views/admin/communication/website/posts/edit.html.erb create mode 100644 app/views/admin/communication/website/posts/index.html.erb create mode 100644 app/views/admin/communication/website/posts/new.html.erb create mode 100644 app/views/admin/communication/website/posts/show.html.erb create mode 100644 db/migrate/20211008120417_create_communication_website_posts.rb create mode 100644 db/migrate/20211008124637_create_communication_website_imported_posts.rb create mode 100644 test/controllers/communication/website/posts_controller_test.rb create mode 100644 test/fixtures/communication/website/imported/posts.yml create mode 100644 test/fixtures/communication/website/posts.yml create mode 100644 test/models/communication/website/imported/post_test.rb create mode 100644 test/models/communication/website/post_test.rb create mode 100644 test/system/communication/website/posts_test.rb diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb index f7ddbfa47..fc0e9fe7f 100644 --- a/app/controllers/admin/communication/website/pages_controller.rb +++ b/app/controllers/admin/communication/website/pages_controller.rb @@ -11,7 +11,6 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web end def new - @page.website = @website breadcrumb end @@ -22,6 +21,7 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web def create @page.university = current_university + @page.website = @website if @page.save redirect_to admin_communication_website_page_path(@page), notice: "Page was successfully created." else diff --git a/app/controllers/admin/communication/website/posts_controller.rb b/app/controllers/admin/communication/website/posts_controller.rb new file mode 100644 index 000000000..d6a49f8ea --- /dev/null +++ b/app/controllers/admin/communication/website/posts_controller.rb @@ -0,0 +1,60 @@ +class Admin::Communication::Website::PostsController < Admin::Communication::Website::ApplicationController + load_and_authorize_resource class: Communication::Website::Post + + def index + @posts = @website.posts.ordered + breadcrumb + end + + def show + breadcrumb + end + + def new + breadcrumb + end + + def edit + breadcrumb + add_breadcrumb t('edit') + end + + def create + @post.university = current_university + @post.website = @website + if @post.save + redirect_to admin_communication_website_post_path(@post), notice: "Post was successfully created." + else + breadcrumb + render :new, status: :unprocessable_entity + end + end + + def update + if @post.update(post_params) + redirect_to admin_communication_website_post_path(@post), notice: "Post was successfully updated." + else + breadcrumb + render :edit, status: :unprocessable_entity + end + end + + def destroy + @communication_website_post.destroy + redirect_to admin_communication_website_posts_url, notice: "Post was successfully destroyed." + end + + protected + + def breadcrumb + super + add_breadcrumb Communication::Website::Post.model_name.human(count: 2), + admin_communication_website_posts_path + breadcrumb_for @post + end + + def post_params + params.require(:communication_website_post) + .permit(:university_id, :website_id, :title, :description, :text, :published, :published_at) + end +end diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb index 81ada3583..2af62df5c 100644 --- a/app/controllers/admin/communication/websites_controller.rb +++ b/app/controllers/admin/communication/websites_controller.rb @@ -21,6 +21,7 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati end @imported_website = @website.imported_website @imported_pages = @imported_website.pages + @imported_posts = @imported_website.posts breadcrumb add_breadcrumb Communication::Website::Imported::Website.model_name.human end diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index 77a6fc3b3..dc9397701 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -26,6 +26,7 @@ class Communication::Website < ApplicationRecord belongs_to :university belongs_to :about, polymorphic: true, optional: true has_many :pages, foreign_key: :communication_website_id + has_many :posts, foreign_key: :communication_website_id has_one :imported_website, class_name: 'Communication::Website::Imported::Website', dependent: :destroy diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb index 28bb55c22..8400f0fb7 100644 --- a/app/models/communication/website/imported/page.rb +++ b/app/models/communication/website/imported/page.rb @@ -34,7 +34,7 @@ class Communication::Website::Imported::Page < ApplicationRecord class_name: 'Communication::Website::Page', optional: true - before_validation :sync_page + before_validation :sync def to_s "#{title}" @@ -42,7 +42,7 @@ class Communication::Website::Imported::Page < ApplicationRecord protected - def sync_page + def sync if page.nil? self.page = Communication::Website::Page.new university: university, website: website.website, # Real website, not imported website @@ -50,8 +50,10 @@ class Communication::Website::Imported::Page < ApplicationRecord self.page.title = "TMP" self.page.save end - # TODO only if not modified + # TODO only if not modified since import page.title = title.to_s + # TODO add that + # page.description = description.to_s page.text = content.to_s page.save end diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb new file mode 100644 index 000000000..ff80c8b51 --- /dev/null +++ b/app/models/communication/website/imported/post.rb @@ -0,0 +1,62 @@ +# == Schema Information +# +# Table name: communication_website_imported_posts +# +# id :uuid not null, primary key +# content :text +# description :text +# identifier :string +# path :text +# published_at :datetime +# status :integer default(0) +# title :string +# url :text +# created_at :datetime not null +# updated_at :datetime not null +# post_id :uuid not null +# university_id :uuid not null +# website_id :uuid not null +# +# Indexes +# +# index_communication_website_imported_posts_on_post_id (post_id) +# index_communication_website_imported_posts_on_university_id (university_id) +# index_communication_website_imported_posts_on_website_id (website_id) +# +# Foreign Keys +# +# fk_rails_... (post_id => communication_website_posts.id) +# fk_rails_... (university_id => universities.id) +# fk_rails_... (website_id => communication_website_imported_websites.id) +# +class Communication::Website::Imported::Post < ApplicationRecord + belongs_to :university + belongs_to :website, + class_name: 'Communication::Website::Imported::Website' + belongs_to :post, + class_name: 'Communication::Website::Post', + optional: true + + before_validation :sync + + def to_s + "#{title}" + end + + protected + + def sync + 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.save + end + # TODO only if not modified since import + post.title = title.to_s + post.description = description.to_s + post.text = content.to_s + post.published_at = published_at if published_at + post.save + end +end diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb index e4d908c35..ecc802bf2 100644 --- a/app/models/communication/website/imported/website.rb +++ b/app/models/communication/website/imported/website.rb @@ -21,24 +21,51 @@ # class Communication::Website::Imported::Website < ApplicationRecord belongs_to :university - belongs_to :website, class_name: 'Communication::Website' - has_many :pages, class_name: 'Communication::Website::Imported::Page' + belongs_to :website, + class_name: 'Communication::Website' + has_many :pages, + class_name: 'Communication::Website::Imported::Page' + has_many :posts, + class_name: 'Communication::Website::Imported::Post' def run! - load("#{website.domain_url}/wp-json/wp/v2/pages").each do |hash| + sync_posts + sync_pages + end + + protected + + def sync_pages + # TODO paginate + load("#{website.domain_url}/wp-json/wp/v2/pages?per_page=100").each do |hash| url = hash['link'] path = URI(url).path - title = hash['title']['rendered'] - content = hash['content']['rendered'] + # TODO id page = pages.where(university: university, path: path).first_or_create page.url = url - page.title = title - page.content = content + page.title = hash['title']['rendered'] + page.content = hash['content']['rendered'] page.save end end - protected + def sync_posts + # TODO paginate + # Communication::Website::Imported::Post.destroy_all + # Communication::Website::Post.destroy_all + load("#{website.domain_url}/wp-json/wp/v2/posts?per_page=100").each do |hash| + identifier = hash['id'] + post = posts.where(university: university, identifier: identifier).first_or_create + post.url = hash['link'] + post.path = URI(post.url).path + post.title = hash['title']['rendered'] + # TODO excerpt + post.description = hash['content']['excerpt'] + post.content = hash['content']['rendered'] + post.published_at = hash['date'] + post.save + end + end def load(url) uri = URI(url) diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index 931f30728..3fa0fe501 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -49,7 +49,7 @@ class Communication::Website::Page < ApplicationRecord after_save :publish_to_github scope :ordered, -> { order(:path) } - scope :recent, -> { order(updated_at: :desc).limit(10) } + scope :recent, -> { order(updated_at: :desc).limit(5) } def content @content ||= github.read_file_at "_pages/#{id}.html" diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb new file mode 100644 index 000000000..b453f2082 --- /dev/null +++ b/app/models/communication/website/post.rb @@ -0,0 +1,42 @@ +# == Schema Information +# +# Table name: communication_website_posts +# +# id :uuid not null, primary key +# description :text +# published :boolean default(FALSE) +# published_at :datetime +# text :text +# title :string +# created_at :datetime not null +# updated_at :datetime not null +# communication_website_id :uuid not null +# university_id :uuid not null +# +# Indexes +# +# index_communication_website_posts_on_communication_website_id (communication_website_id) +# index_communication_website_posts_on_university_id (university_id) +# +# Foreign Keys +# +# fk_rails_... (communication_website_id => communication_websites.id) +# fk_rails_... (university_id => universities.id) +# +class Communication::Website::Post < ApplicationRecord + belongs_to :university + belongs_to :website, + foreign_key: :communication_website_id + has_one :imported_post, + class_name: 'Communication::Website::Imported::Post', + foreign_key: :post_id + + scope :ordered, -> { order(published_at: :desc, created_at: :desc) } + scope :recent, -> { order(published_at: :desc).limit(5) } + + validates :title, presence: true + + def to_s + "#{title}" + end +end diff --git a/app/views/admin/communication/website/pages/_form.html.erb b/app/views/admin/communication/website/pages/_form.html.erb index da8707a0e..15edf3743 100644 --- a/app/views/admin/communication/website/pages/_form.html.erb +++ b/app/views/admin/communication/website/pages/_form.html.erb @@ -21,7 +21,6 @@ <%= f.input :slug %> <%= f.input :published %> <%= f.association :parent, collection: page.website.pages.where.not(id: page) %> - <%= f.association :website, include_blank: false %> </div> </div> </div> diff --git a/app/views/admin/communication/website/pages/_list.html.erb b/app/views/admin/communication/website/pages/_list.html.erb index 1679b84ed..dba9d2fd4 100644 --- a/app/views/admin/communication/website/pages/_list.html.erb +++ b/app/views/admin/communication/website/pages/_list.html.erb @@ -1,17 +1,17 @@ <table class="table"> <thead> <tr> - <th><%= Communication::Website::Page.human_attribute_name('title') %></th> + <th class="ps-0"><%= Communication::Website::Page.human_attribute_name('title') %></th> <th><%= Communication::Website::Page.human_attribute_name('path') %></th> - <th></th> + <th width="150"></th> </tr> </thead> <tbody> <% pages.each do |page| %> <tr> - <td><%= link_to page, admin_communication_website_page_path(website_id: page.website.id, id: page.id) %></td> + <td class="ps-0"><%= link_to page, admin_communication_website_page_path(website_id: page.website.id, id: page.id) %></td> <td><%= page.path %></td> - <td class="text-end"> + <td class="text-end pe-0"> <%= link_to t('edit'), edit_admin_communication_website_page_path(website_id: page.website.id, id: page.id), class: button_classes %> diff --git a/app/views/admin/communication/website/pages/new.html.erb b/app/views/admin/communication/website/pages/new.html.erb index 4817c4565..7f249a1c4 100644 --- a/app/views/admin/communication/website/pages/new.html.erb +++ b/app/views/admin/communication/website/pages/new.html.erb @@ -1,3 +1,3 @@ -<% content_for :title, Research::Journal::Article.model_name.human %> +<% content_for :title, Communication::Website::Page.model_name.human %> <%= render 'form', page: @page %> diff --git a/app/views/admin/communication/website/pages/show.html.erb b/app/views/admin/communication/website/pages/show.html.erb index 926d0e646..b3f80e932 100644 --- a/app/views/admin/communication/website/pages/show.html.erb +++ b/app/views/admin/communication/website/pages/show.html.erb @@ -46,5 +46,5 @@ </div> <% content_for :buttons do %> - <%= link_to 'Modifier', edit_admin_communication_website_page_path(@page.id), class: button_classes %> + <%= edit_link @page %> <% end %> diff --git a/app/views/admin/communication/website/posts/_form.html.erb b/app/views/admin/communication/website/posts/_form.html.erb new file mode 100644 index 000000000..0201b4e08 --- /dev/null +++ b/app/views/admin/communication/website/posts/_form.html.erb @@ -0,0 +1,30 @@ +<%= simple_form_for [:admin, post] do |f| %> + <div class="row"> + <div class="col-md-8"> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h5 class="card-title mb-0">Content</h5> + </div> + <div class="card-body"> + <%= f.input :title %> + <%= f.input :description %> + <%= f.input :text, input_html: { rows: 20 } %> + </div> + </div> + </div> + <div class="col-md-4"> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h5 class="card-title mb-0">Metadata</h5> + </div> + <div class="card-body"> + <%= f.input :published %> + <%= f.input :published_at, html5: true %> + </div> + </div> + </div> + </div> + <% content_for :buttons do %> + <%= submit f %> + <% end %> +<% end %> diff --git a/app/views/admin/communication/website/posts/_list.html.erb b/app/views/admin/communication/website/posts/_list.html.erb new file mode 100644 index 000000000..6dae7e87f --- /dev/null +++ b/app/views/admin/communication/website/posts/_list.html.erb @@ -0,0 +1,27 @@ +<table class="table"> + <thead> + <tr> + <th class="ps-0"><%= Communication::Website::Post.human_attribute_name('title') %></th> + <th><%= Communication::Website::Post.human_attribute_name('published_at') %></th> + <th width="150"></th> + </tr> + </thead> + <tbody> + <% posts.each do |post| %> + <tr> + <td class="ps-0"><%= link_to post, admin_communication_website_post_path(website_id: post.website.id, id: post.id) %></td> + <td><%= l post.published_at, format: :long if post.published_at %></td> + <td class="text-end pe-0"> + <%= link_to t('edit'), + edit_admin_communication_website_post_path(website_id: post.website.id, id: post.id), + class: button_classes %> + <%= link_to t('delete'), + admin_communication_website_post_path(website_id: post.website.id, id: post.id), + method: :delete, + data: { confirm: t('please-confirm') }, + class: button_classes_danger %> + </td> + </tr> + <% end %> + </tbody> +</table> diff --git a/app/views/admin/communication/website/posts/edit.html.erb b/app/views/admin/communication/website/posts/edit.html.erb new file mode 100644 index 000000000..6a9e1ccb8 --- /dev/null +++ b/app/views/admin/communication/website/posts/edit.html.erb @@ -0,0 +1,3 @@ +<% content_for :title, @post %> + +<%= render 'form', post: @post %> diff --git a/app/views/admin/communication/website/posts/index.html.erb b/app/views/admin/communication/website/posts/index.html.erb new file mode 100644 index 000000000..395eec23b --- /dev/null +++ b/app/views/admin/communication/website/posts/index.html.erb @@ -0,0 +1,7 @@ +<% content_for :title, Communication::Website::Post.model_name.human(count: 2) %> + +<%= render 'admin/communication/website/posts/list', posts: @posts %> + +<% content_for :buttons do %> + <%= create_link Communication::Website::Post %> +<% end %> diff --git a/app/views/admin/communication/website/posts/new.html.erb b/app/views/admin/communication/website/posts/new.html.erb new file mode 100644 index 000000000..e004e29ef --- /dev/null +++ b/app/views/admin/communication/website/posts/new.html.erb @@ -0,0 +1,3 @@ +<% content_for :title, Communication::Website::Post.model_name.human %> + +<%= render 'form', post: @post %> diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb new file mode 100644 index 000000000..c4c124033 --- /dev/null +++ b/app/views/admin/communication/website/posts/show.html.erb @@ -0,0 +1,45 @@ +<% content_for :title, @post %> + +<div class="row"> + <div class="col-md-8"> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h5 class="card-title mb-0">Content</h5> + </div> + <div class="card-body"> + <p> + <strong>Description</strong> + <%= @post.description %> + </p> + + <p> + <strong>Text</strong> + </p> + <%= raw @post.text %> + </div> + </div> + </div> + <div class="col-md-4"> + <div class="card flex-fill w-100"> + <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> + </div> + </div> +</div> + +<% content_for :buttons do %> + <%= edit_link @post %> +<% end %> diff --git a/app/views/admin/communication/websites/import.html.erb b/app/views/admin/communication/websites/import.html.erb index 3be008d69..bdc56cc65 100644 --- a/app/views/admin/communication/websites/import.html.erb +++ b/app/views/admin/communication/websites/import.html.erb @@ -6,20 +6,37 @@ class: button_classes %> <% end %> +<h2><%= @imported_posts.count %> posts</h2> <table class="table"> <thead> <tr> - <th><%= Communication::Website::Imported::Page.human_attribute_name('title') %></th> - <th><%= Communication::Website::Imported::Page.human_attribute_name('original_page') %></th> - <th><%= Communication::Website::Imported::Page.human_attribute_name('imported_page') %></th> + <th class="ps-0"><%= Communication::Website::Imported::Post.human_attribute_name('title') %></th> + <th><%= Communication::Website::Imported::Post.human_attribute_name('original') %></th> + </tr> + </thead> + <tbody> + <% @imported_posts.each do |post| %> + <tr> + <td class="ps-0"><%= link_to post, admin_communication_website_post_path(website_id: post.post.website.id, id: post.post.id) %></td> + <td class="small"><%= link_to post.path, post.url, target: :_blank %></td> + </tr> + <% end %> + </tbody> +</table> + +<h2 class="mt-5"><%= @imported_pages.count %> pages</h2> +<table class="table"> + <thead> + <tr> + <th class="ps-0"><%= Communication::Website::Imported::Page.human_attribute_name('title') %></th> + <th><%= Communication::Website::Imported::Page.human_attribute_name('original') %></th> </tr> </thead> <tbody> <% @imported_pages.each do |page| %> <tr> - <td><%= page %></td> - <td><%= link_to page.path, page.url, target: :_blank %></td> - <td><%= link_to 'Imported page', admin_communication_website_page_path(website_id: page.page.website.id, id: page.page.id) %></td> + <td><%= link_to page, admin_communication_website_page_path(website_id: page.page.website.id, id: page.page.id) %></td> + <td class="small"><%= link_to page.path, page.url, target: :_blank %></td> </tr> <% end %> </tbody> diff --git a/app/views/admin/communication/websites/show.html.erb b/app/views/admin/communication/websites/show.html.erb index 7388fad41..c80c33b93 100644 --- a/app/views/admin/communication/websites/show.html.erb +++ b/app/views/admin/communication/websites/show.html.erb @@ -9,15 +9,21 @@ <%= link_to @website.about, [:admin, @website.about] unless @website.about.nil? %> </p> +<h2 class="mt-5"><%= Communication::Website::Post.model_name.human(count: 2) %></h2> +<%= link_to t('create'), + new_admin_communication_website_post_path(website_id: @website), + class: button_classes('me-3') %> +<%= link_to t('activerecord.models.communication/website/post.all'), + admin_communication_website_posts_path(website_id: @website) %> +<%= render 'admin/communication/website/posts/list', posts: @website.posts.recent %> + <h2 class="mt-5"><%= Communication::Website::Page.model_name.human(count: 2) %></h2> <%= link_to t('create'), new_admin_communication_website_page_path(website_id: @website), class: button_classes('me-3') %> - -<%= link_to 'Toutes les pages', +<%= link_to t('activerecord.models.communication/website/page.all'), admin_communication_website_pages_path(website_id: @website) %> - <%= render 'admin/communication/website/pages/list', pages: @website.pages.recent %> <% content_for :buttons do %> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index 91b44b6c0..9623e9237 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -17,6 +17,11 @@ en: communication/website/page: one: Page other: Pages + all: All pages + communication/website/post: + one: Post + other: Posts + all: All posts communication/website/imported/website: one: Imported website other: Imported websites diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index 2c2bd1642..f8c2eb904 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -17,6 +17,11 @@ fr: communication/website/page: one: Page other: Pages + all: Toutes les pages + communication/website/post: + one: Actualité + other: Actualités + all: Toutes les actualités communication/website/imported/website: one: Site importé other: Sites importés diff --git a/config/routes.rb b/config/routes.rb index 540db86ee..a2c11953d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,11 @@ Rails.application.routes.draw do - resources :languages devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions' } + resources :languages + namespace :admin do resources :users draw 'education' diff --git a/config/routes/communication.rb b/config/routes/communication.rb index 038649e63..edff8b75e 100644 --- a/config/routes/communication.rb +++ b/config/routes/communication.rb @@ -1,9 +1,10 @@ namespace :communication do resources :websites do - resources :pages, controller: 'website/pages' member do get :import post :import end + resources :pages, controller: 'website/pages' + resources :posts, controller: 'website/posts' end end diff --git a/db/migrate/20211008120417_create_communication_website_posts.rb b/db/migrate/20211008120417_create_communication_website_posts.rb new file mode 100644 index 000000000..224dac398 --- /dev/null +++ b/db/migrate/20211008120417_create_communication_website_posts.rb @@ -0,0 +1,15 @@ +class CreateCommunicationWebsitePosts < ActiveRecord::Migration[6.1] + def change + create_table :communication_website_posts, id: :uuid do |t| + t.references :university, null: false, foreign_key: true, type: :uuid + t.references :communication_website, null: false, foreign_key: true, type: :uuid + t.string :title + t.text :description + t.text :text + t.boolean :published, default: false + t.datetime :published_at + + t.timestamps + end + end +end diff --git a/db/migrate/20211008124637_create_communication_website_imported_posts.rb b/db/migrate/20211008124637_create_communication_website_imported_posts.rb new file mode 100644 index 000000000..2c8a278e7 --- /dev/null +++ b/db/migrate/20211008124637_create_communication_website_imported_posts.rb @@ -0,0 +1,19 @@ +class CreateCommunicationWebsiteImportedPosts < ActiveRecord::Migration[6.1] + def change + create_table :communication_website_imported_posts, id: :uuid do |t| + t.references :university, null: false, foreign_key: true, type: :uuid + t.references :website, null: false, foreign_key: {to_table: :communication_website_imported_websites}, type: :uuid + t.references :post, null: false, foreign_key: {to_table: :communication_website_posts}, type: :uuid + t.integer :status, default: 0 + t.string :title + t.text :description + t.text :content + t.text :path + t.text :url + t.datetime :published_at + t.string :identifier + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ef44af74f..752a392f4 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_08_090117) do +ActiveRecord::Schema.define(version: 2021_10_08_124637) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -82,6 +82,25 @@ ActiveRecord::Schema.define(version: 2021_10_08_090117) do t.index ["website_id"], name: "index_communication_website_imported_pages_on_website_id" end + create_table "communication_website_imported_posts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.uuid "university_id", null: false + t.uuid "website_id", null: false + t.uuid "post_id", null: false + t.integer "status", default: 0 + t.string "title" + t.text "description" + t.text "content" + t.text "path" + t.text "url" + t.datetime "published_at" + t.string "identifier" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["post_id"], name: "index_communication_website_imported_posts_on_post_id" + t.index ["university_id"], name: "index_communication_website_imported_posts_on_university_id" + t.index ["website_id"], name: "index_communication_website_imported_posts_on_website_id" + end + create_table "communication_website_imported_websites", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "university_id", null: false t.uuid "website_id", null: false @@ -113,6 +132,20 @@ ActiveRecord::Schema.define(version: 2021_10_08_090117) do t.index ["university_id"], name: "index_communication_website_pages_on_university_id" end + create_table "communication_website_posts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.uuid "university_id", null: false + t.uuid "communication_website_id", null: false + t.string "title" + t.text "description" + t.text "text" + t.boolean "published", default: false + t.datetime "published_at" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + 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 + create_table "communication_websites", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "university_id", null: false t.string "name" @@ -283,11 +316,16 @@ ActiveRecord::Schema.define(version: 2021_10_08_090117) do add_foreign_key "communication_website_imported_pages", "communication_website_imported_websites", column: "website_id" add_foreign_key "communication_website_imported_pages", "communication_website_pages", column: "page_id" add_foreign_key "communication_website_imported_pages", "universities" + add_foreign_key "communication_website_imported_posts", "communication_website_imported_websites", column: "website_id" + add_foreign_key "communication_website_imported_posts", "communication_website_posts", column: "post_id" + add_foreign_key "communication_website_imported_posts", "universities" add_foreign_key "communication_website_imported_websites", "communication_websites", column: "website_id" add_foreign_key "communication_website_imported_websites", "universities" add_foreign_key "communication_website_pages", "communication_website_pages", column: "parent_id" add_foreign_key "communication_website_pages", "communication_websites" add_foreign_key "communication_website_pages", "universities" + add_foreign_key "communication_website_posts", "communication_websites" + add_foreign_key "communication_website_posts", "universities" add_foreign_key "communication_websites", "universities" add_foreign_key "education_programs", "universities" add_foreign_key "research_journal_articles", "research_journal_volumes" diff --git a/docs/websites/readme.md b/docs/websites/readme.md index e60cc76ce..4c78d105b 100644 --- a/docs/websites/readme.md +++ b/docs/websites/readme.md @@ -11,19 +11,23 @@ Attributes: Attributes: - university:references +- website:references - title:string - description:text -- data:json +- text:text +- published:boolean - published_at:datetime ## websites/Page Attributes: - university:references +- website:references - title:string - description:text -- data:json +- text:text - parent:references +- published:boolean ## websites/Document diff --git a/test/controllers/communication/website/posts_controller_test.rb b/test/controllers/communication/website/posts_controller_test.rb new file mode 100644 index 000000000..96ca644e1 --- /dev/null +++ b/test/controllers/communication/website/posts_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class Communication::Website::PostsControllerTest < ActionDispatch::IntegrationTest + setup do + @communication_website_post = communication_website_posts(:one) + end + + test "should get index" do + get communication_website_posts_url + assert_response :success + end + + test "should get new" do + get new_communication_website_post_url + assert_response :success + end + + test "should create communication_website_post" do + assert_difference('Communication::Website::Post.count') do + post communication_website_posts_url, params: { communication_website_post: { description: @communication_website_post.description, published: @communication_website_post.published, published_at: @communication_website_post.published_at, text: @communication_website_post.text, title: @communication_website_post.title, university_id: @communication_website_post.university_id, website_id: @communication_website_post.website_id } } + end + + assert_redirected_to communication_website_post_url(Communication::Website::Post.last) + end + + test "should show communication_website_post" do + get communication_website_post_url(@communication_website_post) + assert_response :success + end + + test "should get edit" do + get edit_communication_website_post_url(@communication_website_post) + assert_response :success + end + + test "should update communication_website_post" do + patch communication_website_post_url(@communication_website_post), params: { communication_website_post: { description: @communication_website_post.description, published: @communication_website_post.published, published_at: @communication_website_post.published_at, text: @communication_website_post.text, title: @communication_website_post.title, university_id: @communication_website_post.university_id, website_id: @communication_website_post.website_id } } + assert_redirected_to communication_website_post_url(@communication_website_post) + end + + test "should destroy communication_website_post" do + assert_difference('Communication::Website::Post.count', -1) do + delete communication_website_post_url(@communication_website_post) + end + + assert_redirected_to communication_website_posts_url + end +end diff --git a/test/fixtures/communication/website/imported/posts.yml b/test/fixtures/communication/website/imported/posts.yml new file mode 100644 index 000000000..a59f72baa --- /dev/null +++ b/test/fixtures/communication/website/imported/posts.yml @@ -0,0 +1,53 @@ +# == Schema Information +# +# Table name: communication_website_imported_posts +# +# id :uuid not null, primary key +# content :text +# description :text +# identifier :string +# path :text +# published_at :datetime +# status :integer default(0) +# title :string +# url :text +# created_at :datetime not null +# updated_at :datetime not null +# post_id :uuid not null +# university_id :uuid not null +# website_id :uuid not null +# +# Indexes +# +# index_communication_website_imported_posts_on_post_id (post_id) +# index_communication_website_imported_posts_on_university_id (university_id) +# index_communication_website_imported_posts_on_website_id (website_id) +# +# Foreign Keys +# +# fk_rails_... (post_id => communication_website_posts.id) +# fk_rails_... (university_id => universities.id) +# fk_rails_... (website_id => communication_website_imported_websites.id) +# + +one: + university: one + website: one + post: one + status: 1 + title: MyString + description: MyText + content: MyText + published_at: 2021-10-08 14:46:37 + identifier: MyString + +two: + university: two + website: two + post: two + status: 1 + title: MyString + description: MyText + content: MyText + published_at: 2021-10-08 14:46:37 + identifier: MyString diff --git a/test/fixtures/communication/website/posts.yml b/test/fixtures/communication/website/posts.yml new file mode 100644 index 000000000..28582416b --- /dev/null +++ b/test/fixtures/communication/website/posts.yml @@ -0,0 +1,43 @@ +# == Schema Information +# +# Table name: communication_website_posts +# +# id :uuid not null, primary key +# description :text +# published :boolean default(FALSE) +# published_at :datetime +# text :text +# title :string +# created_at :datetime not null +# updated_at :datetime not null +# communication_website_id :uuid not null +# university_id :uuid not null +# +# Indexes +# +# index_communication_website_posts_on_communication_website_id (communication_website_id) +# index_communication_website_posts_on_university_id (university_id) +# +# Foreign Keys +# +# fk_rails_... (communication_website_id => communication_websites.id) +# fk_rails_... (university_id => universities.id) +# + +one: + university: one + website: one + title: MyString + description: MyText + text: MyText + published: + published_at: 2021-10-08 14:04:23 + +two: + university: two + website: two + title: MyString + description: MyText + text: MyText + published: + published_at: 2021-10-08 14:04:23 diff --git a/test/models/communication/website/imported/post_test.rb b/test/models/communication/website/imported/post_test.rb new file mode 100644 index 000000000..4319bd5f8 --- /dev/null +++ b/test/models/communication/website/imported/post_test.rb @@ -0,0 +1,38 @@ +# == Schema Information +# +# Table name: communication_website_imported_posts +# +# id :uuid not null, primary key +# content :text +# description :text +# identifier :string +# path :text +# published_at :datetime +# status :integer default(0) +# title :string +# url :text +# created_at :datetime not null +# updated_at :datetime not null +# post_id :uuid not null +# university_id :uuid not null +# website_id :uuid not null +# +# Indexes +# +# index_communication_website_imported_posts_on_post_id (post_id) +# index_communication_website_imported_posts_on_university_id (university_id) +# index_communication_website_imported_posts_on_website_id (website_id) +# +# Foreign Keys +# +# fk_rails_... (post_id => communication_website_posts.id) +# fk_rails_... (university_id => universities.id) +# fk_rails_... (website_id => communication_website_imported_websites.id) +# +require "test_helper" + +class Communication::Website::Imported::PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/communication/website/post_test.rb b/test/models/communication/website/post_test.rb new file mode 100644 index 000000000..9673a3e48 --- /dev/null +++ b/test/models/communication/website/post_test.rb @@ -0,0 +1,32 @@ +# == Schema Information +# +# Table name: communication_website_posts +# +# id :uuid not null, primary key +# description :text +# published :boolean default(FALSE) +# published_at :datetime +# text :text +# title :string +# created_at :datetime not null +# updated_at :datetime not null +# communication_website_id :uuid not null +# university_id :uuid not null +# +# Indexes +# +# index_communication_website_posts_on_communication_website_id (communication_website_id) +# index_communication_website_posts_on_university_id (university_id) +# +# Foreign Keys +# +# fk_rails_... (communication_website_id => communication_websites.id) +# fk_rails_... (university_id => universities.id) +# +require "test_helper" + +class Communication::Website::PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/communication/website/posts_test.rb b/test/system/communication/website/posts_test.rb new file mode 100644 index 000000000..cbd0d35c7 --- /dev/null +++ b/test/system/communication/website/posts_test.rb @@ -0,0 +1,55 @@ +require "application_system_test_case" + +class Communication::Website::PostsTest < ApplicationSystemTestCase + setup do + @communication_website_post = communication_website_posts(:one) + end + + test "visiting the index" do + visit communication_website_posts_url + assert_selector "h1", text: "Communication/Website/Posts" + end + + test "creating a Post" do + visit communication_website_posts_url + click_on "New Communication/Website/Post" + + fill_in "Description", with: @communication_website_post.description + fill_in "Published", with: @communication_website_post.published + fill_in "Published at", with: @communication_website_post.published_at + fill_in "Text", with: @communication_website_post.text + fill_in "Title", with: @communication_website_post.title + fill_in "University", with: @communication_website_post.university_id + fill_in "Website", with: @communication_website_post.website_id + click_on "Create Post" + + assert_text "Post was successfully created" + click_on "Back" + end + + test "updating a Post" do + visit communication_website_posts_url + click_on "Edit", match: :first + + fill_in "Description", with: @communication_website_post.description + fill_in "Published", with: @communication_website_post.published + fill_in "Published at", with: @communication_website_post.published_at + fill_in "Text", with: @communication_website_post.text + fill_in "Title", with: @communication_website_post.title + fill_in "University", with: @communication_website_post.university_id + fill_in "Website", with: @communication_website_post.website_id + click_on "Update Post" + + assert_text "Post was successfully updated" + click_on "Back" + end + + test "destroying a Post" do + visit communication_website_posts_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Post was successfully destroyed" + end +end -- GitLab