Skip to content

Commit

Permalink
Add a ChildTypes to manage selection display during work attachment
Browse files Browse the repository at this point in the history
With `ActiveFedora` models, we use `MyWork.valid_child_concerns` to determine
which types the user can select to attach to an existing work.

Since this is a display-only issue, we break this out into a new service. For
legacy models, we delegate the configuration back to the model, avoiding any
need for configuration changes.

For Valkyrie models, we provide a default config (a work can nest works of its
own type), but punt on configuration issues. There are other configuration
matters to discuss.
  • Loading branch information
Tom Johnson committed Jan 10, 2020
1 parent 7597155 commit e4b4439
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/presenters/hyrax/model_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Hyrax
module ModelProxy
delegate :to_param, :to_key, :id, to: :solr_document

delegate :model_name, :valid_child_concerns, to: :_delegated_to
delegate :model_name, to: :_delegated_to

def to_partial_path
_delegated_to._to_partial_path
Expand All @@ -18,6 +18,10 @@ def to_model
self
end

def valid_child_concerns
Hyrax::ChildTypes.for(parent: solr_document.hydra_model)
end

private

def _delegated_to
Expand Down
39 changes: 39 additions & 0 deletions app/services/hyrax/child_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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
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
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

0 comments on commit e4b4439

Please sign in to comment.