diff --git a/Gemfile b/Gemfile index 766d0c6590eadc140eefb43bd37970b4c4385392..2b6e662f946af9d076f8de69b9c352524067e212 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 92c97865da74ea89f13d85e5ef6b4798993d24b8..c437773bb6e65d60ce4c734d95f8596e3c9bb309 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 0000000000000000000000000000000000000000..9dd7da0fa4c534a0a5e601599d8d3498ead0ac32 --- /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 763f5441c4ceb65426031aaa4c710c491f107d66..4ab819ffb4d8ec62febf28672ab716cc72fc1361 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 0000000000000000000000000000000000000000..cfd2b501b3176a66defd2b14a9a4ae25d030c8b0 --- /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 0000000000000000000000000000000000000000..027a6208af0564297635cf8c16785b7781a1a5a6 --- /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 192c8fde23befce2e6a0232ec6d6e712f131d358..4de0d8b74aa67c6d08ed8a92bbadef03868a0b1f 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 f164a01a319aa190f3affc42d97fe6163de30428..512a5a42acbf4163c394007681ba90991aac3242 100644 --- a/app/views/admin/communication/websites/show.html.erb +++ b/app/views/admin/communication/websites/show.html.erb @@ -11,6 +11,7 @@ <% end %> <%= render 'admin/communication/websites/show/posts' %> + <div class="row"> <div class="col-md-4"> <%= render 'admin/communication/websites/show/home' %> diff --git a/app/views/admin/communication/websites/show/_posts.html.erb b/app/views/admin/communication/websites/show/_posts.html.erb index e74723e31099da084a9d4be042e3e3932d1567b8..0942701dc82fa4365e546437581f3a8abab55271 100644 --- a/app/views/admin/communication/websites/show/_posts.html.erb +++ b/app/views/admin/communication/websites/show/_posts.html.erb @@ -1,6 +1,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 c7679dc4ad3ccdc92f81b72cb66cfd31fe85d439..74e74ead345bb7613e50fafb9b7f664bd9675f52 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 3038dc200e487156dad427f14de5d4636edaf745..114547f6922eb2bf38266254e2d140cdb411b01b 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 d27e977b98bc8e629cb6c6dd4837984747668122..a42948203171e607d2fc3bfbe28a97e9f8ec724d 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