Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Adding controlled vocabulary for creator to input field
Browse files Browse the repository at this point in the history
Includes form for editing a creator
  • Loading branch information
carolyncole authored and awead committed Dec 14, 2018
1 parent b710ede commit 9407fe2
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 39 deletions.
4 changes: 4 additions & 0 deletions app/cho/agent/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ class Resource < Valkyrie::Resource

attribute :given_name, Valkyrie::Types::String
attribute :surname, Valkyrie::Types::String

def to_s
"#{given_name} #{surname}"
end
end
end
8 changes: 4 additions & 4 deletions app/cho/schema/input_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class InputField

attr_reader :form, :metadata_field

delegate :text?, :required?, :valkyrie_id?, :label, to: :metadata_field
delegate :text?, :required?, :valkyrie_id?, :creator?, :label, to: :metadata_field

# @param [ActionView::Helpers::FormBuilder] form
# @param [Schema::MetadataField] metadata_field
Expand All @@ -20,7 +20,7 @@ def display_label
end

def partial
if text? || valkyrie_id?
if text? || valkyrie_id? || creator?
metadata_field.field_type
else
'string'
Expand All @@ -31,8 +31,8 @@ def options
{ required: required?, 'aria-required': required? }
end

def datalist
ControlledVocabulary::Factory.lookup(metadata_field.controlled_vocabulary.to_sym).list
def datalist(component: nil)
ControlledVocabulary::Factory.lookup(metadata_field.controlled_vocabulary.to_sym).list(component: component)
end

def value
Expand Down
25 changes: 25 additions & 0 deletions app/views/shared/input_partials/_creator.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form.fields_for :creator do |creator_form| %>
<div class="container">
<div class="row">
<div class="col-md-12 col-lg-3">
<%= form.label :role %>
<%= creator_form.text_field :role, field.options.merge!(list: "#{field.label}_role") %>
<datalist id="<%= "#{field.label}_role" %>">
<% field.datalist(component: :roles).each do |item| %>
<option value="<%= item %>"><%= item.label %></option>
<% end %>
</datalist>
</div>

<div class="col-md-12 col-lg-3">
<%= form.label :agent %>
<%= creator_form.text_field :agent, field.options.merge!(list: "#{field.label}_agent") %>
<datalist id="<%= "#{field.label}_agent" %>">
<% field.datalist(component: :agents).each do |item| %>
<option value="<%= item.id %>"><%= item %></option>
<% end %>
</datalist>
</div>
</div>
</div>
<% end %>
2 changes: 1 addition & 1 deletion config/data_dictionary/data_dictionary_development.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Label,Field Type,Requirement Designation,Validation,Multiple,Controlled Vocabula
title,string,required,no_validation,false,no_vocabulary,,Title,no_transformation,no_facet,help me,true
member_of_collection_ids,valkyrie_id,optional,resource_exists,false,cho_collections,,Member of Collection,no_transformation,no_facet,help me,false
alternate_ids,alternate_id,required_to_publish,no_validation,true,no_vocabulary,,Identifier,no_transformation,no_facet,help me,true
creator,creator,optional,no_validation,true,no_vocabulary,,Creator,no_transformation,no_facet,help me,true
creator,creator,optional,no_validation,true,creator_vocabulary,,Creator,no_transformation,no_facet,help me,true
date_created,string,required_to_publish,no_validation,true,no_vocabulary,,Date Created,no_transformation,no_facet,help me,true
subject,string,optional,no_validation,true,no_vocabulary,,Subject,no_transformation,no_facet,help me,true
location,string,optional,no_validation,true,no_vocabulary,,Location,no_transformation,no_facet,help me,true
Expand Down
12 changes: 1 addition & 11 deletions spec/cho/controlled_vocabulary/creators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@
context 'with roles' do
subject { described_class.list(component: :roles) }

let(:relator_list) do
[
RDF::Vocabulary::Term.new('http://id.loc.gov/vocabulary/relators/bsl'),
RDF::Vocabulary::Term.new('http://id.loc.gov/vocabulary/relators/cli')
]
end

# @note We mock this response because calling it takes 10 seconds to build the full list
before { allow(RDF::Vocab::MARCRelators).to receive(:to_a).and_return(relator_list) }

it { is_expected.to eq(relator_list) }
it { is_expected.to eq(MockRDF.relators) }
end

context 'with a bogus component' do
Expand Down
45 changes: 22 additions & 23 deletions spec/cho/schema/input_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@

it { is_expected.to eq('string') }
end

context 'creator field type' do
let(:type) { 'creator' }

it { is_expected.to eq('creator') }
end
end

describe '#options' do
Expand All @@ -99,44 +105,37 @@
end

describe '#datalist' do
subject { model.datalist }
subject { model.datalist.map(&:id) }

before { metadata_field.field_type = type }

context 'with the default string type' do
let(:type) { 'string' }
let!(:agent) { create :agent }
let!(:collection) { create :collection }

it { is_expected.to be_empty }
end
before { metadata_field.controlled_vocabulary = controlled_vocabulary }

context 'text field type' do
let(:type) { 'text' }
context 'with the default vocabulary' do
let(:controlled_vocabulary) { 'no_vocabulary' }

it { is_expected.to be_empty }
end

context 'numeric field type' do
let(:type) { 'numeric' }
context 'collections vocabulary' do
let(:controlled_vocabulary) { 'cho_collections' }

it { is_expected.to be_empty }
it { is_expected.to contain_exactly(collection.id) }
end

context 'date field type' do
let(:type) { 'date' }
context 'agent vocabulary' do
let(:controlled_vocabulary) { 'cho_agents' }

it { is_expected.to be_empty }
it { is_expected.to contain_exactly(agent.id) }
end

context 'valkyrie_id field type' do
let(:type) { 'valkyrie_id' }

it { is_expected.to eq(ControlledVocabulary::Collections.list) }
end
context 'creator vocabulary' do
subject { model.datalist(component: :agents).map(&:id) }

context 'alternate_id field type' do
let(:type) { 'alternate_id' }
let(:controlled_vocabulary) { 'creator_vocabulary' }

it { is_expected.to be_empty }
it { is_expected.to contain_exactly(agent.id) }
end
end

Expand Down
18 changes: 18 additions & 0 deletions spec/support/mock_rdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# @note Class used for mocking sets of RDF terms that are usually very large and can take a long time
# to initially load. By mocking them, we can reduce testing time.
class MockRDF
def self.relators
[
RDF::Vocabulary::Term.new('http://id.loc.gov/vocabulary/relators/bsl', attributes: { label: 'blasting' }),
RDF::Vocabulary::Term.new('http://id.loc.gov/vocabulary/relators/cli', attributes: { label: 'climbing' })
]
end
end

RSpec.configure do |config|
config.before do
allow(RDF::Vocab::MARCRelators).to receive(:to_a).and_return(MockRDF.relators)
end
end
20 changes: 20 additions & 0 deletions spec/support/shared/views.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

RSpec.shared_examples 'a creator form' do
it 'renders the new form' do
assert_select 'input[name=?]', "#{resource.model_name.param_key}[creator][agent]"
assert_select 'input[name=?]', "#{resource.model_name.param_key}[creator][role]"

assert_select 'datalist[id=?]', 'creator_role'
assert_select 'option[value=?]', 'http://id.loc.gov/vocabulary/relators/bsl'
assert_select 'option', 'blasting'
assert_select 'option[value=?]', 'http://id.loc.gov/vocabulary/relators/cli'
assert_select 'option', 'climbing'

assert_select 'datalist[id=?]', 'creator_agent'
assert_select 'option[value=?]', agent1.id.to_s
assert_select 'option', 'Jane Doe'
assert_select 'option[value=?]', agent2.id.to_s
assert_select 'option', 'John Doe'
end
end
41 changes: 41 additions & 0 deletions spec/views/shared/_creator.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'shared/input_partials/creator', type: :view do
let!(:agent1) { create :agent, given_name: 'Jane', surname: 'Doe' }
let!(:agent2) { create :agent }
let(:field) { instance_double Schema::InputField, options: {}, label: 'creator' }

before do
allow(field).to receive(:datalist).with(component: :roles).and_return(MockRDF.relators)
allow(field).to receive(:datalist).with(component: :agents).and_return([agent1, agent2])
view.form_for(resource, url: 'blah') do |form|
render 'shared/input_partials/creator', form: form, field: field
end
end

context 'with a work' do
let(:resource) { build :work }

it_behaves_like 'a creator form'
end

context 'with an archival collection' do
let(:resource) { build :archival_collection }

it_behaves_like 'a creator form'
end

context 'with an library collection' do
let(:resource) { build :library_collection }

it_behaves_like 'a creator form'
end

context 'with an curated collection' do
let(:resource) { build :curated_collection }

it_behaves_like 'a creator form'
end
end
2 changes: 2 additions & 0 deletions spec/views/work_submissions/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
assert_select 'form[action=?][method=?]', works_path, 'post' do
assert_select 'input[name=?]', 'work_submission[title]'
assert_select 'input[name=?]', 'work_submission[member_of_collection_ids]'
assert_select 'input[name=?]', 'work_submission[creator][agent]'
assert_select 'input[name=?]', 'work_submission[creator][role]'
assert_select 'label', 'File Selection'
end
end
Expand Down

0 comments on commit 9407fe2

Please sign in to comment.