-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
ChildTypes
to manage selection display during work attachment
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
Showing
3 changed files
with
78 additions
and
1 deletion.
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
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,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 |
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,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 |