From 34a46d47a6a6ecea942b9af783ff8c8ee1a3cf09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Wed, 24 Nov 2021 12:25:11 +0100 Subject: [PATCH] curation --- Gemfile | 1 + Gemfile.lock | 32 ++++++++++++++ .../website/posts/curations_controller.rb | 30 +++++++++++++ app/models/user.rb | 2 +- app/services/curator.rb | 42 +++++++++++++++++++ .../website/posts/curations/new.html.erb | 21 ++++++++++ .../website/posts/index.html.erb | 3 ++ .../communication/websites/show.html.erb | 3 ++ config/locales/communication/en.yml | 2 + config/locales/communication/fr.yml | 2 + config/routes/admin/communication.rb | 1 + 11 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/communication/website/posts/curations_controller.rb create mode 100644 app/services/curator.rb create mode 100644 app/views/admin/communication/website/posts/curations/new.html.erb diff --git a/Gemfile b/Gemfile index 766d0c659..2b6e662f9 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ gem 'octokit' gem 'front_matter_parser' gem 'two_factor_authentication', git: 'https://github.com/noesya/two_factor_authentication.git' # gem 'two_factor_authentication', path: '../two_factor_authentication' +gem 'curation'#, path: '../../arnaudlevy/curation' # Front gem 'jquery-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 92c97865d..c437773bb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -130,6 +130,9 @@ GEM countries (~> 4.0) sort_alphabetical (~> 1.1) crass (1.0.6) + curation (1.7) + metainspector + nokogiri delayed_job (4.1.9) activesupport (>= 3.0, < 6.2) delayed_job_active_record (4.1.6) @@ -148,6 +151,8 @@ GEM warden (~> 1.2.3) devise-i18n (1.10.1) devise (>= 4.8.0) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) encryptor (3.0.0) enum-i18n (0.1.1) activerecord (>= 4.1) @@ -168,14 +173,24 @@ GEM faraday-rack (~> 1.0) multipart-post (>= 1.2, < 3) ruby2_keywords (>= 0.0.4) + faraday-cookie_jar (0.0.7) + faraday (>= 0.8.0) + http-cookie (~> 1.0.0) faraday-em_http (1.0.0) faraday-em_synchrony (1.0.0) + faraday-encoding (0.0.5) + faraday faraday-excon (1.1.0) + faraday-http-cache (2.2.0) + faraday (>= 0.8) faraday-httpclient (1.0.1) faraday-net_http (1.0.1) faraday-net_http_persistent (1.2.0) faraday-patron (1.0.0) faraday-rack (1.0.0) + faraday_middleware (1.2.0) + faraday (~> 1.0) + fastimage (2.2.5) ffi (1.15.4) figaro (1.2.0) thor (>= 0.14.0, < 2) @@ -186,6 +201,8 @@ GEM sassc-rails globalid (0.5.2) activesupport (>= 5.0) + http-cookie (1.0.4) + domain_name (~> 0.5) i18n (1.8.11) concurrent-ruby (~> 1.0) i18n_data (0.13.0) @@ -227,6 +244,16 @@ GEM mini_mime (>= 0.1.1) marcel (1.0.2) matrix (0.4.2) + metainspector (5.11.2) + addressable (~> 2.7) + faraday (~> 1.4) + faraday-cookie_jar (~> 0.0) + faraday-encoding (~> 0.0) + faraday-http-cache (~> 2.2) + faraday_middleware (~> 1.0) + fastimage (~> 2.2) + nesty (~> 1.0) + nokogiri (~> 1.11) method_source (1.0.0) mini_magick (4.11.0) mini_mime (1.1.2) @@ -236,6 +263,7 @@ GEM multipart-post (2.1.1) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) + nesty (1.0.2) nio4r (2.5.8) nokogiri (1.12.5) mini_portile2 (~> 2.6.1) @@ -355,6 +383,9 @@ GEM ethon (>= 0.9.0) tzinfo (2.0.4) concurrent-ruby (~> 1.0) + unf (0.1.4) + unf_ext + unf_ext (0.0.8) unicode_utils (1.4.0) warden (1.2.9) rack (>= 2.0.9) @@ -389,6 +420,7 @@ DEPENDENCIES cancancan capybara (>= 3.26) country_select + curation delayed_job_active_record delayed_job_web devise diff --git a/app/controllers/admin/communication/website/posts/curations_controller.rb b/app/controllers/admin/communication/website/posts/curations_controller.rb new file mode 100644 index 000000000..9dd7da0fa --- /dev/null +++ b/app/controllers/admin/communication/website/posts/curations_controller.rb @@ -0,0 +1,30 @@ +class Admin::Communication::Website::Posts::CurationsController < Admin::Communication::Website::ApplicationController + def new + breadcrumb + end + + def create + @curator = Curator.new @website, current_user, curation_params[:url] + if @curator.valid? + redirect_to [:edit, :admin, @curator.post], notice: t('admin.successfully_created_html', model: @curator.post.to_s) + else + breadcrumb + byebug + flash[:alert] = "Erreur lors de la curation" + render :new, status: :unprocessable_entity + end + end + + protected + + def breadcrumb + super + add_breadcrumb Communication::Website::Post.model_name.human(count: 2), + admin_communication_website_posts_path + add_breadcrumb t('communication.website.posts.new_curation') + end + + def curation_params + params.require(:curation).permit(:url) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 763f5441c..4ab819ffb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -61,7 +61,7 @@ class User < ApplicationRecord belongs_to :university belongs_to :language has_one :researcher, class_name: 'Research::Researcher', dependent: :nullify - has_one :auhtor, class_name: 'Communication::Website::Author', dependent: :nullify + has_one :author, class_name: 'Communication::Website::Author', dependent: :nullify scope :ordered, -> { order(:last_name, :first_name) } diff --git a/app/services/curator.rb b/app/services/curator.rb new file mode 100644 index 000000000..cfd2b501b --- /dev/null +++ b/app/services/curator.rb @@ -0,0 +1,42 @@ +class Curator + attr_reader :website, :user, :url, :post + + def initialize(website, user, url) + @website = website + @user = user + @url = url + create_post! + attach_image! unless page.image.blank? + end + + def valid? + @post.valid? + end + + protected + + def create_post! + text = Wordpress.clean_html("#{page.text}<br><a href=\"#{@url}\" target=\"_blank\">Source</a>") + @post = website.posts.create( + university: website.university, + title: page.title, + text: text, + slug: page.title.parameterize, + author: @user.author, + published_at: Time.now + ) + end + + def attach_image! + @post.featured_image.attach( + io: URI.open(page.image), + filename: File.basename(page.image).split('?').first + ) + rescue + puts "Attach image failed" + end + + def page + @page ||= Curation::Page.new(@url) + end +end diff --git a/app/views/admin/communication/website/posts/curations/new.html.erb b/app/views/admin/communication/website/posts/curations/new.html.erb new file mode 100644 index 000000000..027a6208a --- /dev/null +++ b/app/views/admin/communication/website/posts/curations/new.html.erb @@ -0,0 +1,21 @@ +<% content_for :title, t('communication.website.posts.new_curation') %> + +<%= simple_form_for :curation, url: admin_communication_website_post_curations_path, html: { id: 'new_communication_website_post_curation' } 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"><%= t('content') %></h5> + </div> + <div class="card-body"> + <%= f.input :url, + label: 'URL', + value: params.dig(:curation, :url) %> + </div> + </div> + </div> + </div> + <% content_for :action_bar_right do %> + <%= submit f %> + <% end %> +<% end %> diff --git a/app/views/admin/communication/website/posts/index.html.erb b/app/views/admin/communication/website/posts/index.html.erb index 192c8fde2..4de0d8b74 100644 --- a/app/views/admin/communication/website/posts/index.html.erb +++ b/app/views/admin/communication/website/posts/index.html.erb @@ -5,4 +5,7 @@ <% content_for :action_bar_right do %> <%= create_link Communication::Website::Post %> + <%= link_to t('communication.website.posts.new_curation'), + new_admin_communication_website_post_curation_path(website_id: @website.id), + class: button_classes if can?(:create, Communication::Website::Post) %> <% end %> diff --git a/app/views/admin/communication/websites/show.html.erb b/app/views/admin/communication/websites/show.html.erb index ea7a61a12..01f8bc4f7 100644 --- a/app/views/admin/communication/websites/show.html.erb +++ b/app/views/admin/communication/websites/show.html.erb @@ -65,6 +65,9 @@ <div class="card mt-5"> <div class="card-header"> <div class="float-end"> + <%= link_to t('communication.website.posts.new_curation'), + new_admin_communication_website_post_curation_path(website_id: @website.id), + class: button_classes if can?(:create, Communication::Website::Post) %> <%= link_to t('create'), new_admin_communication_website_post_path(website_id: @website), class: button_classes if can?(:create, Communication::Website::Post) %> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index c7679dc4a..74e74ead3 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -15,6 +15,8 @@ en: pending: Import in progress last_pages: Last pages last_posts: Last posts + posts: + new_curation: New curation see_all: See the full list (%{number} elements) activemodel: models: diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index 3038dc200..114547f69 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -15,6 +15,8 @@ fr: pending: Import en cours last_pages: Dernières pages last_posts: Dernières actualités + posts: + new_curation: Nouvelle curation see_all: Voir la liste complète (%{number} éléments) activemodel: models: diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb index d27e977b9..a42948203 100644 --- a/config/routes/admin/communication.rb +++ b/config/routes/admin/communication.rb @@ -35,6 +35,7 @@ namespace :communication do post :publish end end + resources :curations, path: 'posts/curations', as: :post_curations, controller: 'website/posts/curations', only: [:new, :create] resources :menus, controller: 'website/menus' do resources :items, controller: 'website/menu/items', except: :index do collection do -- GitLab