Skip to content
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
4 changes: 2 additions & 2 deletions app/forms/hyrax/forms/resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def initialize(deprecated_resource = nil, resource: nil)
if r.flexible?
self.class.deserializer_class = nil # need to reload this on first use after schema is loaded
singleton_class.schema_definitions = self.class.definitions
context = r.respond_to?(:context) ? r.context : nil
Hyrax::Schema.m3_schema_loader.form_definitions_for(schema: r.class.name, version: Hyrax::FlexibleSchema.current_schema_id, contexts: context).map do |field_name, options|
contexts = r.respond_to?(:contexts) ? r.contexts : nil
Hyrax::Schema.m3_schema_loader.form_definitions_for(schema: r.class.name, version: Hyrax::FlexibleSchema.current_schema_id, contexts: contexts).map do |field_name, options|
singleton_class.property field_name.to_sym, options.merge(display: options.fetch(:display, true), default: [])
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/forms/hyrax/forms/resource_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@
end
end

describe 'flexible form with contexts' do
let(:work) { build(:monograph) }
let(:schema_loader) { instance_double(Hyrax::M3SchemaLoader, form_definitions_for: { title: { required: true, primary: true } }) }

before do
allow(Hyrax.config).to receive(:flexible?).and_return(true)
allow(Hyrax::Schema).to receive(:m3_schema_loader).and_return(schema_loader)
allow(work).to receive(:flexible?).and_return(true)
allow(work).to receive(:respond_to?).and_call_original
allow(work).to receive(:respond_to?).with(:contexts).and_return(true)
allow(work).to receive(:contexts).and_return(['special_context'])
end

it 'passes the resource contexts to form_definitions_for so context-specific fields are included' do
described_class.for(resource: work)

expect(schema_loader).to have_received(:form_definitions_for) do |args|
expect(Array(args[:contexts])).to contain_exactly('special_context')
end
end
end

describe '#based_near' do
subject(:form) { form_class.new(work) }

Expand Down
46 changes: 46 additions & 0 deletions spec/services/hyrax/m3_schema_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,52 @@
expect(schema_loader.form_definitions_for(schema: Monograph.to_s))
.to eq(title: { required: true, primary: true, multiple: true })
end

context 'with context filtering' do
let(:profile_with_context_form) do
modified_profile = profile.dup
modified_profile['properties']['dimensions'] = {
'available_on' => {
'class' => ['Monograph'],
'context' => ['primary_context']
},
'cardinality' => { 'minimum' => 0 },
'data_type' => 'array',
'display_label' => { 'default' => 'Dimensions' },
'form' => { 'primary' => false, 'multiple' => true },
'property_uri' => 'http://purl.org/dc/terms/format',
'range' => 'http://www.w3.org/2001/XMLSchema#string'
}
modified_profile
end
let(:schema_with_context_form) do
Hyrax::FlexibleSchema.create(
profile: profile_with_context_form
)
end

before do
allow(Hyrax::FlexibleSchema).to receive(:find_by).and_return(schema_with_context_form)
end

it 'excludes context-specific fields when no context is provided' do
result = schema_loader.form_definitions_for(schema: Monograph.to_s, contexts: nil)
expect(result).to eq(title: { required: true, primary: true, multiple: true })
expect(result).not_to have_key(:dimensions)
end

it 'includes context-specific fields when matching context is provided' do
result = schema_loader.form_definitions_for(schema: Monograph.to_s, contexts: ['primary_context'])
expect(result).to include(:title, :dimensions)
expect(result[:dimensions]).to eq(primary: false, multiple: true, required: false)
end

it 'excludes context-specific fields when non-matching context is provided' do
result = schema_loader.form_definitions_for(schema: Monograph.to_s, contexts: ['other_context'])
expect(result).not_to have_key(:dimensions)
expect(result).to include(:title)
end
end
end

describe '#view_definitions_for' do
Expand Down
Loading