mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-04 10:54:30 +08:00
Merge pull request #8464 from artoscinote/ma_SCI_11853
Implement favorite actions for PET [SCI-11853]
This commit is contained in:
commit
bf593d8567
18 changed files with 162 additions and 5 deletions
27
app/controllers/concerns/favorites_actions.rb
Normal file
27
app/controllers/concerns/favorites_actions.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module FavoritesActions
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_action :load_favorite_item, only: %i(favorite unfavorite)
|
||||
end
|
||||
|
||||
def favorite
|
||||
@favorite_item.favorite!(current_user, current_team)
|
||||
head :ok
|
||||
end
|
||||
|
||||
def unfavorite
|
||||
@favorite_item.unfavorite!(current_user, current_team)
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_favorite_item
|
||||
@favorite_item = controller_name.singularize.camelize.constantize.find(params[:id])
|
||||
|
||||
render_403 unless public_send(:"can_read_#{controller_name.singularize}?", @favorite_item)
|
||||
end
|
||||
end
|
|
@ -7,6 +7,7 @@ class ExperimentsController < ApplicationController
|
|||
include ApplicationHelper
|
||||
include Rails.application.routes.url_helpers
|
||||
include Breadcrumbs
|
||||
include FavoritesActions
|
||||
|
||||
before_action :load_project, only: %i(index create archive_group restore_group move)
|
||||
before_action :load_experiment, except: %i(create archive_group restore_group
|
||||
|
|
|
@ -7,6 +7,7 @@ class MyModulesController < ApplicationController
|
|||
include ApplicationHelper
|
||||
include MyModulesHelper
|
||||
include Breadcrumbs
|
||||
include FavoritesActions
|
||||
|
||||
before_action :load_vars, except: %i(index restore_group create new save_table_state
|
||||
inventory_assigning_my_module_filter actions_toolbar)
|
||||
|
|
|
@ -9,6 +9,7 @@ class ProjectsController < ApplicationController
|
|||
include ExperimentsHelper
|
||||
include Breadcrumbs
|
||||
include UserRolesHelper
|
||||
include FavoritesActions
|
||||
|
||||
attr_reader :current_folder
|
||||
|
||||
|
@ -17,9 +18,10 @@ class ProjectsController < ApplicationController
|
|||
before_action :switch_team_with_param, only: :index
|
||||
before_action :load_vars, only: %i(update create_tag assigned_users_list)
|
||||
before_action :load_current_folder, only: :index
|
||||
before_action :check_view_permissions, except: %i(index create update archive_group restore_group
|
||||
before_action :check_read_permissions, except: %i(index create update archive_group restore_group
|
||||
inventory_assigning_project_filter
|
||||
actions_toolbar user_roles users_filter head_of_project_users_list)
|
||||
actions_toolbar user_roles users_filter head_of_project_users_list
|
||||
favorite unfavorite)
|
||||
before_action :check_create_permissions, only: :create
|
||||
before_action :check_manage_permissions, only: :update
|
||||
before_action :set_folder_inline_name_editing, only: %i(index cards)
|
||||
|
@ -338,7 +340,7 @@ class ProjectsController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def check_view_permissions
|
||||
def check_read_permissions
|
||||
current_team_switch(@project.team) if current_team != @project.team
|
||||
render_403 unless can_read_project?(@project)
|
||||
end
|
||||
|
|
19
app/models/concerns/favoritable.rb
Normal file
19
app/models/concerns/favoritable.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Favoritable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :favorites, as: :item, inverse_of: :item, dependent: :destroy
|
||||
|
||||
scope :favorite_for, ->(user) { joins(:favorites).where(favorites: { user: user }) }
|
||||
end
|
||||
|
||||
def favorite!(user, favorite_team = nil)
|
||||
favorites.create!(user: user, team: favorite_team || team)
|
||||
end
|
||||
|
||||
def unfavorite!(user, favorite_team = nil)
|
||||
favorites.find_by(user: user, team: favorite_team || team).destroy!
|
||||
end
|
||||
end
|
|
@ -14,6 +14,7 @@ class Experiment < ApplicationRecord
|
|||
include Assignable
|
||||
include Cloneable
|
||||
include TimeTrackable
|
||||
include Favoritable
|
||||
|
||||
before_save -> { report_elements.destroy_all }, if: -> { !new_record? && project_id_changed? }
|
||||
before_save :reset_due_date_notification_sent, if: -> { due_date_changed? }
|
||||
|
|
7
app/models/favorite.rb
Normal file
7
app/models/favorite.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Favorite < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :team
|
||||
belongs_to :item, polymorphic: true
|
||||
end
|
|
@ -13,6 +13,7 @@ class MyModule < ApplicationRecord
|
|||
include PermissionCheckableModel
|
||||
include Assignable
|
||||
include Cloneable
|
||||
include Favoritable
|
||||
|
||||
attr_accessor :transition_error_rollback, :my_module_status_created_by
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ class Project < ApplicationRecord
|
|||
include PermissionCheckableModel
|
||||
include Assignable
|
||||
include TimeTrackable
|
||||
include Favoritable
|
||||
|
||||
enum visibility: { hidden: 0, visible: 1 }
|
||||
|
||||
|
|
|
@ -327,6 +327,8 @@ class User < ApplicationRecord
|
|||
|
||||
has_many :hidden_repository_cell_reminders, dependent: :destroy
|
||||
|
||||
has_many :favorites, dependent: :destroy
|
||||
|
||||
before_validation :downcase_email!
|
||||
|
||||
def name
|
||||
|
|
|
@ -68,7 +68,9 @@ module Lists
|
|||
clone: clone_experiment_path(object),
|
||||
update: experiment_path(object),
|
||||
show_access: access_permissions_experiment_path(object),
|
||||
workflow_img: fetch_workflow_img_experiment_path(object)
|
||||
workflow_img: fetch_workflow_img_experiment_path(object),
|
||||
favorite: favorite_experiment_url(object),
|
||||
unfavorite: unfavorite_experiment_url(object)
|
||||
}
|
||||
|
||||
if can_manage_project_users?(object.project)
|
||||
|
|
|
@ -66,7 +66,9 @@ module Lists
|
|||
experiments_to_move: experiments_to_move_experiment_path(object.experiment),
|
||||
update: my_module_path(object),
|
||||
show_access: access_permissions_my_module_path(object),
|
||||
provisioning_status: provisioning_status_my_module_url(object)
|
||||
provisioning_status: provisioning_status_my_module_url(object),
|
||||
favorite: favorite_my_module_url(object),
|
||||
unfavorite: unfavorite_my_module_url(object)
|
||||
}
|
||||
|
||||
urls_list[:update_access] = access_permissions_my_module_path(object) if can_manage_project_users?(object.experiment.project)
|
||||
|
|
|
@ -126,6 +126,11 @@ module Lists
|
|||
project_folder_path(object)
|
||||
end
|
||||
|
||||
if project?
|
||||
urls_list[:favorite] = favorite_project_url(object)
|
||||
urls_list[:unfavorite] = unfavorite_project_url(object)
|
||||
end
|
||||
|
||||
urls_list[:show_access] = access_permissions_project_path(object)
|
||||
if project? && can_manage_project_users?(object)
|
||||
urls_list[:assigned_users] = assigned_users_list_project_path(object)
|
||||
|
|
|
@ -389,6 +389,8 @@ Rails.application.routes.draw do
|
|||
|
||||
member do
|
||||
get :assigned_users_list
|
||||
post :favorite
|
||||
post :unfavorite
|
||||
end
|
||||
|
||||
collection do
|
||||
|
@ -457,6 +459,8 @@ Rails.application.routes.draw do
|
|||
get :projects_to_clone
|
||||
get :projects_to_move
|
||||
get :experiments_to_move
|
||||
post :favorite
|
||||
post :unfavorite
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -476,6 +480,8 @@ Rails.application.routes.draw do
|
|||
get :permissions
|
||||
get :actions_dropdown
|
||||
get :provisioning_status
|
||||
post :favorite
|
||||
post :unfavorite
|
||||
end
|
||||
resources :my_module_tags, path: '/tags', only: [:index, :create, :destroy] do
|
||||
collection do
|
||||
|
|
20
db/migrate/20250428070801_create_favorites.rb
Normal file
20
db/migrate/20250428070801_create_favorites.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateFavorites < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :favorites do |t|
|
||||
t.references :user, null: false, foreign_key: true
|
||||
t.references :team, null: false, foreign_key: true
|
||||
t.references :item, polymorphic: true, null: false
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index(
|
||||
:favorites,
|
||||
%i(user_id team_id item_id item_type),
|
||||
unique: true,
|
||||
name: :index_favorites_on_user_and_item_and_team
|
||||
)
|
||||
end
|
||||
end
|
|
@ -103,4 +103,24 @@ describe ExperimentsController, type: :controller do
|
|||
.to(change { Activity.count })
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST favorite' do
|
||||
let(:action) { post :favorite, params: { id: experiment.id } }
|
||||
|
||||
it 'creates a favorite' do
|
||||
expect(user.favorites.exists?(item: experiment )).to eq(false)
|
||||
action
|
||||
expect(user.favorites.exists?(item: experiment )).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST unfavorite' do
|
||||
let(:action) { post :unfavorite, params: { id: experiment.id } }
|
||||
|
||||
it 'removes a favorite' do
|
||||
Favorite.create!(user: user, item: experiment, team: experiment.team)
|
||||
action
|
||||
expect(user.favorites.exists?(item: experiment )).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -256,4 +256,24 @@ describe MyModulesController, type: :controller do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST favorite' do
|
||||
let(:action) { post :favorite, params: { id: my_module.id } }
|
||||
|
||||
it 'creates a favorite' do
|
||||
expect(user.favorites.exists?(item: my_module )).to eq(false)
|
||||
action
|
||||
expect(user.favorites.exists?(item: my_module )).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST unfavorite' do
|
||||
let(:action) { post :unfavorite, params: { id: my_module.id } }
|
||||
|
||||
it 'removes a favorite' do
|
||||
Favorite.create!(user: user, item: my_module, team: my_module.team)
|
||||
action
|
||||
expect(user.favorites.exists?(item: my_module )).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -83,4 +83,24 @@ describe ProjectsController, type: :controller do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST favorite' do
|
||||
let(:action) { post :favorite, params: { id: project.id } }
|
||||
|
||||
it 'creates a favorite' do
|
||||
expect(user.favorites.exists?(item: project )).to eq(false)
|
||||
action
|
||||
expect(user.favorites.exists?(item: project )).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST unfavorite' do
|
||||
let(:action) { post :unfavorite, params: { id: project.id } }
|
||||
|
||||
it 'removes a favorite' do
|
||||
Favorite.create!(user: user, item: project, team: project.team)
|
||||
action
|
||||
expect(user.favorites.exists?(item: project )).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue