From 9058d634ec6960ca2112645ddbf7d0c5cad92821 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Thu, 16 Feb 2023 19:00:50 +0100 Subject: [PATCH] connections --- .../websites/connections_controller.rb | 15 +++++-- .../communication/website/connection.rb | 11 +++++ .../communication/website/with_connections.rb | 5 ++- .../websites/connections/_list.html.erb | 40 +++++++++++++++++++ .../websites/connections/index.html.erb | 40 +------------------ .../websites/connections/show.html.erb | 24 +++++++++++ config/routes/admin/communication.rb | 2 +- 7 files changed, 92 insertions(+), 45 deletions(-) create mode 100644 app/views/admin/communication/websites/connections/_list.html.erb create mode 100644 app/views/admin/communication/websites/connections/show.html.erb diff --git a/app/controllers/admin/communication/websites/connections_controller.rb b/app/controllers/admin/communication/websites/connections_controller.rb index 0c0da98df..4578da5cb 100644 --- a/app/controllers/admin/communication/websites/connections_controller.rb +++ b/app/controllers/admin/communication/websites/connections_controller.rb @@ -1,16 +1,25 @@ class Admin::Communication::Websites::ConnectionsController < Admin::Communication::Websites::ApplicationController - before_action :load_object, except: :index + before_action :load_from_object, only: [:create, :destroy] def index @connections = @website.connections.page params[:page] breadcrumb end + def show + @connection = @website.connections.find params[:id] + @others = @connection.for_same_object + breadcrumb + add_breadcrumb @connection + end + + # Strange use of create, does not really create a connection def create @website.connect @object head :ok end + # Strange use of destroy, does not really create a connection def destroy @website.disconnect @object redirect_back(fallback_location: [:admin, @object]) @@ -18,7 +27,7 @@ class Admin::Communication::Websites::ConnectionsController < Admin::Communicati protected - def load_object + def load_from_object object_type = params[:object_type] object_id = params[:object_id] @object = object_type.constantize.find object_id @@ -26,6 +35,6 @@ class Admin::Communication::Websites::ConnectionsController < Admin::Communicati def breadcrumb super - add_breadcrumb Communication::Website::Connection.model_name.human(count: 2) + add_breadcrumb Communication::Website::Connection.model_name.human(count: 2), admin_communication_website_connections_path end end \ No newline at end of file diff --git a/app/models/communication/website/connection.rb b/app/models/communication/website/connection.rb index 425d0594f..cc4862437 100644 --- a/app/models/communication/website/connection.rb +++ b/app/models/communication/website/connection.rb @@ -29,4 +29,15 @@ class Communication::Website::Connection < ApplicationRecord belongs_to :website belongs_to :object, polymorphic: true belongs_to :source, polymorphic: true, optional: true + + def for_same_object + self.class.where( university: university, + website: website, + object: object) + .where.not(id: id) + end + + def to_s + "#{id}" + end end diff --git a/app/models/communication/website/with_connections.rb b/app/models/communication/website/with_connections.rb index c90c7af3d..99a98d148 100644 --- a/app/models/communication/website/with_connections.rb +++ b/app/models/communication/website/with_connections.rb @@ -13,9 +13,9 @@ module Communication::Website::WithConnections end def clean_connections! - # start = Time.now + start = Time.now connect self - # connections.where('updated_at < ?', start).destroy_all + connections.where('updated_at < ?', start).destroy_all end def connect(object, source = nil) @@ -25,6 +25,7 @@ module Communication::Website::WithConnections puts "#{dependencies.count} dependencies to connect" dependencies.each do |dependency| connect_object dependency, source + connect_object dependency, object end end diff --git a/app/views/admin/communication/websites/connections/_list.html.erb b/app/views/admin/communication/websites/connections/_list.html.erb new file mode 100644 index 000000000..4a42f37bc --- /dev/null +++ b/app/views/admin/communication/websites/connections/_list.html.erb @@ -0,0 +1,40 @@ +<div class="table-responsive"> + <table class="<%= table_classes %>"> + <thead> + <tr> + <th><%= Communication::Website::Connection.human_attribute_name('id') %></th> + <th><%= Communication::Website::Connection.human_attribute_name('object') %></th> + <th><%= Communication::Website::Connection.human_attribute_name('object_type') %></th> + <th><%= Communication::Website::Connection.human_attribute_name('source') %></th> + <th><%= Communication::Website::Connection.human_attribute_name('source_type') %></th> + <th><%= Communication::Website::Connection.human_attribute_name('updated_at') %></th> + </tr> + </thead> + <tbody> + <% connections.each do |connection| %> + <% + object = connection.object + object_name = object.to_s + object_name = object.id if object.is_a?(ActiveStorage::Blob) + begin + object_link = url_for [:admin, object] + rescue + end + source = connection.source + begin + source_link = url_for [:admin, source] + rescue + end + %> + <tr> + <td><%= link_to connection, [:admin, connection] %></td> + <td><%= link_to_if object_link, object_name, object_link %></td> + <td><%= connection.object_type %></td> + <td><%= link_to_if source_link, source, source_link %></td> + <td><%= connection.source_type %></td> + <td><%= l connection.updated_at %></td> + </tr> + <% end %> + </tbody> + </table> +</div> \ No newline at end of file diff --git a/app/views/admin/communication/websites/connections/index.html.erb b/app/views/admin/communication/websites/connections/index.html.erb index add7e8218..6dd7d698c 100644 --- a/app/views/admin/communication/websites/connections/index.html.erb +++ b/app/views/admin/communication/websites/connections/index.html.erb @@ -1,43 +1,5 @@ <% content_for :title, Communication::Website::Connection.model_name.human(count: 2) %> <% content_for :title_right, @connections.total_count %> -<div class="table-responsive"> - <table class="<%= table_classes %>"> - <thead> - <tr> - <th><%= Communication::Website::Connection.human_attribute_name('object') %></th> - <th><%= Communication::Website::Connection.human_attribute_name('object_type') %></th> - <th><%= Communication::Website::Connection.human_attribute_name('source') %></th> - <th><%= Communication::Website::Connection.human_attribute_name('source_type') %></th> - <th><%= Communication::Website::Connection.human_attribute_name('updated_at') %></th> - </tr> - </thead> - <tbody> - <% @connections.each do |connection| %> - <% - object = connection.object - object_name = object.to_s - object_name = object.id if object.is_a?(ActiveStorage::Blob) - begin - object_link = url_for [:admin, object] - rescue - end - source = connection.source - begin - source_link = url_for [:admin, source] - rescue - end - %> - <tr> - <td><%= link_to_if object_link, object_name, object_link %></td> - <td><%= connection.object_type %></td> - <td><%= link_to_if source_link, source, source_link %></td> - <td><%= connection.source_type %></td> - <td><%= l connection.updated_at %></td> - </tr> - <% end %> - </tbody> - </table> -</div> - +<%= render 'admin/communication/websites/connections/list', connections: @connections %> <%= paginate @connections, theme: 'bootstrap-5' %> \ No newline at end of file diff --git a/app/views/admin/communication/websites/connections/show.html.erb b/app/views/admin/communication/websites/connections/show.html.erb new file mode 100644 index 000000000..10be4f529 --- /dev/null +++ b/app/views/admin/communication/websites/connections/show.html.erb @@ -0,0 +1,24 @@ +<% content_for :title, @connection %> + +<div class="row"> + <div class="col-lg-6 mb-5"> + <%= osuny_label Communication::Website::Connection.human_attribute_name('object') %> + <p><%= @connection.object %></p> + <%= osuny_label Communication::Website::Connection.human_attribute_name('object_type') %> + <p><%= @connection.object_type %></p> + <%= osuny_label Communication::Website::Connection.human_attribute_name('object_id') %> + <p><%= @connection.object_id %></p> + </div> + <div class="col-lg-6 mb-5"> + <%= osuny_label Communication::Website::Connection.human_attribute_name('source') %> + <p><%= @connection.source %></p> + <%= osuny_label Communication::Website::Connection.human_attribute_name('source_type') %> + <p><%= @connection.source_type %></p> + <%= osuny_label Communication::Website::Connection.human_attribute_name('source_id') %> + <p><%= @connection.source_id %></p> + </div> +</div> + +<%= osuny_panel Communication::Website::Connection.human_attribute_name('others') do %> + <%= render 'admin/communication/websites/connections/list', connections: @others %> +<% end %> \ No newline at end of file diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb index 093c4fe60..8dd62e40f 100644 --- a/config/routes/admin/communication.rb +++ b/config/routes/admin/communication.rb @@ -10,7 +10,7 @@ namespace :communication do get :style get :analytics end - resources :connections, controller: 'websites/connections', only: [:index, :create, :destroy] + resources :connections, controller: 'websites/connections', except: [:new, :edit] resources :pages, controller: 'websites/pages', path: '/:lang/pages' do collection do post :reorder -- GitLab