Skip to content
Snippets Groups Projects
Commit 3c327ed5 authored by Arnaud Levy's avatar Arnaud Levy
Browse files

Fix #819

parent ee8c17d6
No related branches found
No related tags found
No related merge requests found
Showing
with 204 additions and 47 deletions
......@@ -23,6 +23,19 @@ class Admin::UsersController < Admin::ApplicationController
add_breadcrumb t('edit')
end
def favorite
operation = params[:operation]
id = params[:about_id]
type = params[:about_type]
about = type.constantize.find id
if operation == 'add'
current_user.add_favorite(about)
else
current_user.remove_favorite(about)
end
redirect_back fallback_location: [:admin, about]
end
def create
# we don't want the confirmation mail to be send when the user is created from admin!
@user.skip_confirmation!
......
......@@ -61,6 +61,8 @@ class User < ApplicationRecord
include WithAdminTheme
include WithAuthorship
include WithAvatar
include WithFavorites
# FIXME y a-t-il des couplages temporels qui justifient l'ordre ci-dessous ?
include WithRegistrationContext
include WithUniversity
include WithAuthentication
......
# == Schema Information
#
# Table name: user_favorites
#
# id :uuid not null, primary key
# about_type :string not null, indexed => [about_id]
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null, indexed => [about_type]
# user_id :uuid not null, indexed
#
# Indexes
#
# index_user_favorites_on_about (about_type,about_id)
# index_user_favorites_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_25ed4cb388 (user_id => users.id)
#
class User::Favorite < ApplicationRecord
belongs_to :user
belongs_to :about, polymorphic: true
end
module User::WithFavorites
extend ActiveSupport::Concern
included do
has_many :favorites
end
def add_favorite(about)
favorites_for(about).first_or_create
end
def remove_favorite(about)
favorites_for(about).destroy_all
end
def favorite?(about)
favorites_for(about).any?
end
protected
def favorites_for(about)
favorites.where(about_id: about.id, about_type: about.class.polymorphic_name)
end
end
<h2 class="category"><%= User::Favorite.model_name.human(count: 2) %></h2>
<div class="row">
<% if current_user.favorites.none? %>
<p><%= t 'user.favorites.none' %></p>
<% else %>
<% current_user.favorites.each do |favorite| %>
<% about = favorite.about %>
<% next unless can?(:read, about) %>
<div class="col-md-6 col-xl-4 d-flex">
<%= osuny_panel about do %>
<p class="small"><%= about.class.model_name.human %></p>
<%= link_to t('show'),
[:admin, about],
class: button_classes('stretched-link') %>
<% end %>
</div>
<% end %>
<% end %>
</div>
\ No newline at end of file
<div>
<% if current_user.favorite?(about)%>
<%= link_to admin_favorite_path(operation: :remove,
about_id: about.id,
about_type: about.class.polymorphic_name),
method: :put do %>
<%= t 'user.favorites.remove' %>
<i class="fas fa-star"></i>
<% end %>
<% else %>
<%= link_to admin_favorite_path(operation: :add,
about_id: about.id,
about_type: about.class.polymorphic_name),
method: :put do %>
<%= t 'user.favorites.add' %>
<i class="fa-regular fa-star"></i>
<% end %>
<% end %>
</div>
\ No newline at end of file
......@@ -8,6 +8,7 @@
<% if @extranet.about %>
(<%= link_to @extranet.about, [:admin, @extranet.about] unless @extranet.about.nil? %>)
<% end %>
<%= render 'admin/application/favorites/widget', about: @extranet %>
<% end %>
<%= render 'admin/communication/extranets/sidebar' do %>
......
......@@ -10,6 +10,7 @@
@website.about,
[:admin, @website.about] unless @website.about.nil? %>)
<% end %>
<%= render 'admin/application/favorites/widget', about: @website %>
<% end %>
<%= render 'admin/communication/websites/sidebar' do %>
......
......@@ -36,52 +36,7 @@
</div>
</div>
<% if current_university.communication_websites.any? && can?(:read, Communication::Website) %>
<h2 class="category"><%= Communication::Website.model_name.human(count: 2) %></h2>
<div class="row">
<% current_university.communication_websites.each do |website| %>
<% next unless can?(:read, website) %>
<div class="<%= classes %>">
<%= osuny_panel website,
action: "<i class=\"#{ Icon::COMMUNICATION_WEBSITE }\"></i>" do %>
<p class="small"><%= website.url %></p>
<%= link_to t('show'), [:admin, website], class: button_classes('stretched-link') %>
<% end %>
</div>
<% end %>
</div>
<% end %>
<% if current_university.communication_extranets.any? && can?(:read, Communication::Extranet) %>
<h2 class="category"><%= Communication::Extranet.model_name.human(count: 2) %></h2>
<div class="row">
<% current_university.communication_extranets.each do |extranet| %>
<% next unless can?(:read, extranet) %>
<div class="<%= classes %>">
<%= osuny_panel extranet,
action: "<i class=\"#{ Icon::COMMUNICATION_EXTRANET }\"></i>" do %>
<p class="small"><%= extranet.url %></p>
<%= link_to t('show'), [:admin, extranet], class: button_classes('stretched-link') %>
<% end %>
</div>
<% end %>
</div>
<% end %>
<% if current_university.research_journals.any? && can?(:read, Research::Journal) %>
<h2 class="category"><%= Research::Journal.model_name.human(count: 2) %></h2>
<div class="row pure__row--small">
<% current_university.research_journals.each do |journal| %>
<% next unless can?(:read, journal) %>
<div class="<%= classes %>">
<%= osuny_panel journal,
action: "<i class=\"#{ Icon::RESEARCH_JOURNAL }\"></i>" do %>
<%= link_to t('show'), [:admin, journal], class: button_classes('stretched-link') %>
<% end %>
</div>
<% end %>
</div>
<% end %>
<%= render 'admin/application/favorites/show' %>
<div class="row mt-5 pt-5">
<% @namespaces.each do |namespace| %>
......
......@@ -9,6 +9,7 @@
<% end %>
<% end %>
<% if @journal.issn %><%= Research::Journal.human_attribute_name('issn') %> <%= @journal.issn %><% end %>
<%= render 'admin/application/favorites/widget', about: @journal %>
<% end %>
<%= render 'admin/research/journals/sidebar' do %>
......
......@@ -86,6 +86,9 @@ en:
user:
one: User
other: Users
user/favorite:
one: Favorite
other: Favorites
add: Add
add_field: Add field
admin:
......@@ -299,6 +302,11 @@ en:
date_with_hour: "%B %d, %Y %H:%M"
today: Today
true: Yes
user:
favorites:
add: Add to favorites
remove: Remove from favorites
none: You have no favorites yet. Feel free to add the websites, extranets or journals you often work on!
photo_import:
next: Next image
nothing: No results for this search
......
......@@ -86,6 +86,9 @@ fr:
user:
one: Utilisateur·rice
other: Utilisateur·rice·s
user/favorite:
one: Favori
other: Favoris
add: Ajouter
add_field: Ajouter un champ
admin:
......@@ -299,6 +302,11 @@ fr:
date_with_hour: "%d %B %Y %H:%M"
today: Aujourd'hui
true: Oui
user:
favorites:
add: Ajouter aux favoris
remove: Enlever des favoris
none: Vous n'avez pas encore de favoris, n'hésitez pas à ajouter les sites Web, extranets ou revues scientifiques auxquels vous accédez fréquemment !
photo_import:
next: Images suivantes
nothing: Aucun résultat pour cette recherche
......
......@@ -24,6 +24,7 @@ Rails.application.routes.draw do
patch 'unlock' => 'users#unlock', on: :member
end
put 'theme' => 'application#set_theme', as: :set_theme
put 'favorite' => 'users#favorite', as: :favorite
draw 'admin/administration'
draw 'admin/communication'
draw 'admin/education'
......
class CreateUserFavorites < ActiveRecord::Migration[7.0]
def change
create_table :user_favorites, id: :uuid do |t|
t.references :user, null: false, foreign_key: true, type: :uuid
t.references :about, polymorphic: true, null: false, type: :uuid
t.timestamps
end
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_06_05_080634) do
ActiveRecord::Schema[7.0].define(version: 2023_06_14_062545) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
......@@ -1048,6 +1048,16 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_05_080634) do
t.index ["university_id"], name: "index_university_roles_on_university_id"
end
create_table "user_favorites", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "user_id", null: false
t.string "about_type", null: false
t.uuid "about_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["about_type", "about_id"], name: "index_user_favorites_on_about"
t.index ["user_id"], name: "index_user_favorites_on_user_id"
end
create_table "users", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
t.uuid "university_id", null: false
t.string "first_name"
......@@ -1214,6 +1224,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_05_080634) do
add_foreign_key "university_person_involvements", "universities"
add_foreign_key "university_person_involvements", "university_people", column: "person_id"
add_foreign_key "university_roles", "universities"
add_foreign_key "user_favorites", "users"
add_foreign_key "users", "languages"
add_foreign_key "users", "universities"
end
# == Schema Information
#
# Table name: user_favorites
#
# id :uuid not null, primary key
# about_type :string not null, indexed => [about_id]
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null, indexed => [about_type]
# user_id :uuid not null, indexed
#
# Indexes
#
# index_user_favorites_on_about (about_type,about_id)
# index_user_favorites_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_25ed4cb388 (user_id => users.id)
#
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user: one
about: one
about_type: About
two:
user: two
about: two
about_type: About
# == Schema Information
#
# Table name: user_favorites
#
# id :uuid not null, primary key
# about_type :string not null, indexed => [about_id]
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null, indexed => [about_type]
# user_id :uuid not null, indexed
#
# Indexes
#
# index_user_favorites_on_about (about_type,about_id)
# index_user_favorites_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_25ed4cb388 (user_id => users.id)
#
require "test_helper"
class User::FavoriteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment