Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎁 Add listener to remove PCDM Collection from Featured #2154

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions app/listeners/hyrax_listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

##
# Generated by hyrax:listeners
#
# The Hyrax engine uses a publish/subscribe programming model to allow
# pluggable behavior in response to certain repository events. A range of events
# are published on a topic based event bus.
#
# This listener provides a template.
#
# For simple use cases, it's fine to add behavior to the `#on_*` methods in this
# Listener. If you have more than trivial behavior here, you probably want to add
# new classes that are named narrowly scoped and named for what the listener is
# for.
#
# When writing listener methods, it's important to carefully consider error,
# handling. Unhandled exceptions short-circuit behavior for other listeners,
# so it's a good idea to be paying attention to failure cases.
#
# @see https://github.com/samvera/hyrax/wiki/Hyrax's-Event-Bus-(Hyrax::Publisher)
# @see https://www.rubydoc.info/gems/hyrax/Hyrax/Publisher
# @see https://dry-rb.org/gems/dry-events
class HyraxListener
# def on_batch_created
# end

##
# Remove the event's associated collection from the {FeaturedCollection}.
#
# @param event [Dry::Events::Event]
#
# @see FeaturedCollection.destroy_for
def on_collection_deleted(event)
collection = event[:collection]
return false unless collection

FeaturedCollection.destroy_for(collection:)
end

##
# Conditionally remove the event's associated collection from the {FeaturedCollection}.
#
# @param event [Dry::Events::Event]
#
# @see FeaturedCollection.destroy_for
def on_collection_metadata_updated(event)
collection = event[:collection]
return false unless collection
return false unless collection.private?

FeaturedCollection.destroy_for(collection:)
end

# def on_collection_membership_update
# end

# def on_file_characterized
# end

# def on_file_downloaded
# end

# def on_file_metadata_updated
# end

# def on_file_metadata_deleted
# end

# def on_file_uploaded
# end

# def on_file_set_audited
# end

# def on_file_set_attached
# end

# def on_file_set_url_imported
# end

# def on_file_set_restored
# end

# def on_object_deleted
# end

# def on_object_failed_deposit
# end

# def on_object_deposited
# end

# def on_object_acl_updated
# end

# def on_object_membership_updated
# end

# def on_object_metadata_updated
# end
end
2 changes: 1 addition & 1 deletion app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class Collection < ActiveFedora::Base
prepend OrderAlready.for(:creator)

def remove_featured
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this method get called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called via some callbacks defined inside the class but outside the scope of this diff.

FeaturedCollection.where(collection_id: id).destroy_all
FeaturedCollection.destroy_for(collection: self)
end
end
10 changes: 6 additions & 4 deletions app/models/featured_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ class FeaturedCollection < ApplicationRecord

default_scope { order(:order) }

def self.destroy_for(collection:)
where(collection_id: collection.id).destroy_all
end

def count_within_limit
return if FeaturedCollection.can_create_another?
errors.add(:base, "Limited to #{feature_limit} featured collections.")
end

attr_accessor :presenter

class << self
def can_create_another?
FeaturedCollection.count < feature_limit
end
def self.can_create_another?
FeaturedCollection.count < feature_limit
end
end
13 changes: 5 additions & 8 deletions app/models/hyrax/pcdm_collection_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
Hyrax::PcdmCollection.class_eval do
include Hyrax::Schema(:basic_metadata)
include Hyrax::ArResource
prepend OrderAlready.for(:creator)

## TODO: Custom behavior to make functional
# after_update :remove_featured, if: proc { |collection| collection.private? }
# after_destroy :remove_featured

# def remove_featured
# FeaturedCollection.where(collection_id: id).destroy_all
# end
# This module provides the #public?, #private?, #restricted? methods; consider contributing this
# back to Hyrax; but that decision requires further discussion on architecture.
# @see https://samvera.slack.com/archives/C0F9JQJDQ/p1705421588370699 Slack discussion thread.
include Hyrax::Permissions::Readable
prepend OrderAlready.for(:creator)
end
1 change: 0 additions & 1 deletion app/models/solr_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def connection_options
switchable_options.reverse_merge(bl_defaults).reverse_merge(af_defaults)
end


def ping
connection.get('admin/ping')['status']
rescue RSolr::Error::Http, RSolr::Error::ConnectionRefused
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/hyrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,5 @@ def initialize(options = {})
@options = { timeout: 120, open_timeout: 120, url: 'http://localhost:8080/solr' }.merge(options)
end
end

Hyrax.publisher.subscribe(HyraxListener.new)
41 changes: 41 additions & 0 deletions spec/listeners/hyrax_listener_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe HyraxListener do
let(:instance) { HyraxListener.new }
let(:collection) { Hyrax::PcdmCollection.new }
let(:event) { Dry::Events::Event.new(event_type, { collection: }) }

describe "on_collection_deleted" do
let(:event_type) { :on_collection_deleted }

it 'destroys the featured collection instance' do
expect(FeaturedCollection).to receive(:destroy_for).with(collection:)

instance.on_collection_metadata_updated(event)
end
end

describe "on_collection_metadata_updated" do
let(:event_type) { :on_collection_metadata_updated }

context 'when the collection is private' do
it 'destroys the featured collection instance' do
expect(collection).to receive(:private?).and_return(true)
expect(FeaturedCollection).to receive(:destroy_for).with(collection:)

instance.on_collection_metadata_updated(event)
end
end

context 'when the resource is not private' do
it "does not destroy the featured collection" do
expect(collection).to receive(:private?).and_return(false)
expect(FeaturedCollection).not_to receive(:destroy_for)

instance.on_collection_metadata_updated(event)
end
end
end
end
16 changes: 16 additions & 0 deletions spec/models/hyrax/pcdm_collection_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Hyrax::PcdmCollection do
subject(:collection) { described_class.new }

it_behaves_like 'a Hyrax::PcdmCollection'

context 'with Hyrax::Permissions::Readable' do
subject { described_class.new }
it { is_expected.to respond_to :public? }
it { is_expected.to respond_to :private? }
it { is_expected.to respond_to :restricted? }
end
end
Loading