-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented uncollected_works_collection
- Loading branch information
Showing
18 changed files
with
283 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
# Controller to work with Authorities records | ||
class AuthoritiesController < ApplicationController | ||
before_action :require_editor | ||
|
||
before_action :set_authority, only: %i(refresh_uncollected_works_collection) | ||
|
||
def refresh_uncollected_works_collection | ||
RefreshUncollectedWorksCollection.call(@authority) | ||
redirect_to authority_path(@authority), notice: t(:updated_successfully) | ||
end | ||
|
||
private | ||
|
||
def set_authority | ||
@authority = Authority.find(params[:id]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
# We want to group all works author was involved into but not belonging to any colleciton into a special | ||
# 'Uncollected works' collection | ||
class RefreshUncollectedWorksCollection < ApplicationService | ||
# rubocop:disable Style/GuardClause | ||
def call(authority) | ||
collection = authority.uncollected_works_collection | ||
|
||
remove_collected_works(authority) if collection.present? | ||
|
||
if collection.nil? | ||
collection = Collection.new( | ||
collection_type: :uncollected, | ||
title: I18n.t(:uncollected_works_collection_title) | ||
) | ||
end | ||
|
||
nextseqno = (collection.collection_items.maximum(:seqno) || 0) + 1 | ||
|
||
# Checking all manifestations given authority is involved into as author or translator | ||
authority.manifestations(:author, :translator) | ||
.preload(collection_items: :collection) | ||
.find_each do |m| | ||
# skipping if manifestation is included in some other collection or already included in uncollected works | ||
# collection for this authority | ||
next if m.collection_items.any? do |ci| | ||
!ci.collection.uncollected? || (collection.present? && ci.collection == collection) | ||
end | ||
|
||
collection.collection_items.build(item: m, seqno: nextseqno) | ||
nextseqno += 1 | ||
end | ||
|
||
collection.save! # should save all added items | ||
|
||
if authority.uncollected_works_collection.nil? | ||
authority.uncollected_works_collection = collection | ||
authority.save! | ||
end | ||
end | ||
# rubocop:enable Style/GuardClause | ||
|
||
# removes from uncollected_works collection works which was included in some other collection | ||
def remove_collected_works(authority) | ||
authority.uncollected_works_collection | ||
.collection_items | ||
.preload(item: { collection_items: :collection }) | ||
.find_each do |collection_item| | ||
# The only possible item type in uncollected works collection is Manifestation | ||
manifestation = collection_item.item | ||
# NOTE: same work can be in several different uncollected works collection related to different authorities | ||
if manifestation.collection_items.any? { |ci| !ci.collection.uncollected? } | ||
collection_item.destroy! | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
db/migrate/20240719103532_add_uncollected_works_collection_id_to_authorities.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddUncollectedWorksCollectionIdToAuthorities < ActiveRecord::Migration[6.1] | ||
def change | ||
add_belongs_to :authorities, :uncollected_works_collection, | ||
foreign_key: { to_table: :collections}, index: { unique: true }, type: :integer | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.