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 a ChildTypes to manage selection display during work attachment #4181

Merged
merged 5 commits into from
Jan 24, 2020
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
17 changes: 10 additions & 7 deletions app/presenters/hyrax/model_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ module Hyrax
# object represented by the solr document.
module ModelProxy
delegate :to_param, :to_key, :id, to: :solr_document

delegate :model_name, :valid_child_concerns, to: :_delegated_to

def to_partial_path
_delegated_to._to_partial_path
end
delegate :model_name, :to_partial_path, to: :_delegated_to

def persisted?
true
Expand All @@ -18,10 +13,18 @@ def to_model
self
end

##
# @deprecated this isn't related to the ModelProxy issue, and has been moved
# to `WorkShowPresenter`.
def valid_child_concerns
Deprecation.warn "#{self.class}#valid_child_concerns will be removed in Hyrax 4.0."
Hyrax::ChildTypes.for(parent: solr_document.hydra_model)
end

private

def _delegated_to
@_delegated_to ||= solr_document.hydra_model
solr_document.to_model
end
end
end
6 changes: 6 additions & 0 deletions app/presenters/hyrax/work_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ def show_deposit_for?(collections:)
collections.present? || current_ability.can?(:create_any, ::Collection)
end

##
# @return [Array<Class>]
def valid_child_concerns
Hyrax::ChildTypes.for(parent: solr_document.hydra_model).to_a
end

private

# list of item ids to display is based on ordered_ids
Expand Down
40 changes: 40 additions & 0 deletions app/services/hyrax/child_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Hyrax
##
# A list of child work types a user may choose to attach for a given work type
#
# These lists are used when users select among work types to attach to an
# existing work, e.g. from Actions available on the show view.
#
# @example
# child_types = Hyrax::ChildTypes.for(parent: MyWorkType)
#
class ChildTypes
include Enumerable
extend Forwardable

def_delegators :@types, :each

##
# @!attribute [r] types
# @return [Array<Class>]
attr_reader :types

##
# @params [Class] parent
jeremyf marked this conversation as resolved.
Show resolved Hide resolved
no-reply marked this conversation as resolved.
Show resolved Hide resolved
# @return [Enumerable<Class>] a list of classes that are valid as child types for `parent`
def self.for(parent:)
return new(parent.valid_child_concerns) if
parent.respond_to?(:valid_child_concerns)

new([parent])
end

##
# @param [Array<Class>] types
def initialize(types)
@types = types.to_a
end
end
end
5 changes: 1 addition & 4 deletions spec/controllers/hyrax/generic_works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,8 @@
end

context 'with a user granted workflow permission' do
before do
allow(document).to receive(:hydra_model).and_return(GenericWork)
end
let(:document) { SolrDocument.new(id: work.id, has_model_ssim: ["GenericWork"]) }
let(:document_list) { [document] }
let(:document) { instance_double(SolrDocument) }

it 'renders without the unauthorized message' do
get :show, params: { id: work.id }
Expand Down
63 changes: 63 additions & 0 deletions spec/presenters/hyrax/model_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

RSpec.describe Hyrax::ModelProxy do
subject(:proxy) { proxy_class.new(solr_document) }
let(:solr_document) { SolrDocument.new(attributes) }
let(:model) { GenericWork }

let(:attributes) do
{ "id" => '888888',
"has_model_ssim" => [model.to_s] }
end

let(:proxy_class) do
Class.new do
include Hyrax::ModelProxy

attr_accessor :solr_document

def initialize(solr_document)
self.solr_document = solr_document
end
end
end

it { is_expected.to be_persisted }

describe '#id' do
it 'delegates to the solr document' do
expect(proxy.id).to eq '888888'
end
end

describe '#model_name' do
it 'delegates to the has_model_ssim model' do
expect(proxy.model_name).to eq model.model_name
end
end

describe '#to_key' do
it 'delegates to the solr document' do
expect(proxy.to_key).to contain_exactly '888888'
end
end

describe '#to_model' do
it 'gives self' do
expect(proxy.to_model).to eql proxy
end
end

describe '#to_param' do
it 'delegates to the solr document' do
expect(proxy.to_param).to eq '888888'
end
end

describe '#valid_child_concerns' do
it 'delegates to the has_model_ssim model' do
expect(proxy.valid_child_concerns)
.to contain_exactly(*model.valid_child_concerns)
end
end
end
34 changes: 34 additions & 0 deletions spec/services/hyrax/child_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

RSpec.describe Hyrax::ChildTypes do
subject(:child_types) { described_class.new(types) }
let(:types) { [GenericWork] }

describe '.for' do
let(:parent) { Hyrax::Test::SimpleWork }

it 'can have itself as a child by default' do
expect(described_class.for(parent: parent)).to contain_exactly(parent)
end

context 'with an ActiveFedora work' do
let(:parent) { GenericWork }

it 'gives the configured valid_child_concerns' do
expect(described_class.for(parent: parent)).to contain_exactly(*parent.valid_child_concerns)
end
end
end

describe '#types' do
it 'returns the initialized types' do
expect(child_types.types).to contain_exactly(*types)
end
end

describe '#to_a' do
it 'returns the initialized types' do
expect(child_types.to_a).to contain_exactly(*types)
end
end
end