Skip to content

Commit c2bc313

Browse files
committed
treat title as a single value even though it is multivalue on form
1 parent 523fdad commit c2bc313

File tree

18 files changed

+145
-20
lines changed

18 files changed

+145
-20
lines changed

app/forms/hyrax/forms/admin_set_form.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ class AdminSetForm < Hyrax::Forms::CollectionForm
77
# Cast any array values on the model to scalars.
88
def [](key)
99
return super if key == :thumbnail_id
10+
if key == :title
11+
@attributes["title"].each do |value|
12+
@attributes["alt_title"] << value
13+
end
14+
@attributes["alt_title"].delete(@attributes["alt_title"].sort.first) unless @attributes["alt_title"].empty?
15+
return @attributes["title"].sort.first unless @attributes["title"].empty?
16+
return ""
17+
end
1018
super.first
1119
end
1220

app/forms/hyrax/forms/collection_form.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CollectionForm
77
# Used by the search builder
88
attr_reader :scope
99

10-
delegate :id, :depositor, :permissions, :human_readable_type, :member_ids, :nestable?, to: :model
10+
delegate :id, :depositor, :permissions, :human_readable_type, :member_ids, :nestable?, :alt_title, to: :model
1111

1212
class_attribute :membership_service_class
1313

@@ -20,7 +20,7 @@ class CollectionForm
2020

2121
delegate :blacklight_config, to: Hyrax::CollectionsController
2222

23-
self.terms = [:resource_type, :title, :creator, :contributor, :description,
23+
self.terms = [:alt_title, :resource_type, :title, :creator, :contributor, :description,
2424
:keyword, :license, :publisher, :date_created, :subject, :language,
2525
:representative_id, :thumbnail_id, :identifier, :based_near,
2626
:related_url, :visibility, :collection_type_gid]
@@ -41,6 +41,36 @@ def initialize(model, current_ability, repository)
4141
@scope = ProxyScope.new(current_ability, repository, blacklight_config)
4242
end
4343

44+
# Cast back to multi-value when saving
45+
# Reads from form
46+
def self.model_attributes(attributes)
47+
attrs = super
48+
return attrs unless attributes[:title]
49+
50+
attrs[:title] = Array(attributes[:title])
51+
return attrs if attributes[:alt_title].nil?
52+
Array(attributes[:alt_title]).each do |value|
53+
attrs["title"] << value if value != ""
54+
end
55+
attrs
56+
end
57+
58+
# @param [Symbol] key the field to read
59+
# @return the value of the form field.
60+
# For display in edit page
61+
def [](key)
62+
return model.member_of_collection_ids if key == :member_of_collection_ids
63+
if key == :title
64+
@attributes["title"].each do |value|
65+
@attributes["alt_title"] << value
66+
end
67+
@attributes["alt_title"].delete(@attributes["alt_title"].sort.first) unless @attributes["alt_title"].empty?
68+
return @attributes["title"].sort.first unless @attributes["title"].empty?
69+
return ""
70+
end
71+
super
72+
end
73+
4474
def permission_template
4575
@permission_template ||= begin
4676
template_model = PermissionTemplate.find_or_create_by(source_id: model.id)
@@ -60,7 +90,8 @@ def primary_terms
6090

6191
# Terms that appear within the accordion
6292
def secondary_terms
63-
[:creator,
93+
[:alt_title,
94+
:creator,
6495
:contributor,
6596
:keyword,
6697
:license,

app/forms/hyrax/forms/work_form.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class WorkForm
1717
:visibility_during_embargo, :embargo_release_date, :visibility_after_embargo,
1818
:visibility_during_lease, :lease_expiration_date, :visibility_after_lease,
1919
:visibility, :in_works_ids, :depositor, :on_behalf_of, :permissions,
20-
:member_ids, to: :model
20+
:member_ids, :alt_title, to: :model
2121

2222
attr_reader :agreement_accepted
2323

24-
self.terms = [:title, :creator, :contributor, :description, :abstract,
24+
self.terms = [:title, :alt_title, :creator, :contributor, :description, :abstract,
2525
:keyword, :license, :rights_statement, :access_right, :rights_notes, :publisher, :date_created,
2626
:subject, :language, :identifier, :based_near, :related_url,
2727
:representative_id, :thumbnail_id, :rendering_ids, :files,
@@ -42,6 +42,20 @@ def initialize(model, current_ability, controller)
4242
super(model)
4343
end
4444

45+
# Cast back to multi-value when saving
46+
# Reads from form
47+
def self.model_attributes(attributes)
48+
attrs = super
49+
return attrs unless attributes[:title]
50+
51+
attrs[:title] = Array(attributes[:title])
52+
return attrs if attributes[:alt_title].nil?
53+
Array(attributes[:alt_title]).each do |value|
54+
attrs["title"] << value if value != ""
55+
end
56+
attrs
57+
end
58+
4559
# when the add_works_to_collection parameter is set, they mean to create
4660
# a new work and add it to that collection.
4761
def member_of_collections
@@ -95,8 +109,16 @@ def initialize_field(key)
95109

96110
# @param [Symbol] key the field to read
97111
# @return the value of the form field.
112+
# For display in edit page
98113
def [](key)
99114
return model.member_of_collection_ids if key == :member_of_collection_ids
115+
if key == :title
116+
@attributes["title"].each do |value|
117+
@attributes["alt_title"] << value
118+
end
119+
@attributes["alt_title"].delete(@attributes["alt_title"].sort.first) unless @attributes["alt_title"].empty?
120+
return @attributes[key.to_s].sort.first
121+
end
100122
super
101123
end
102124

app/indexers/hyrax/basic_metadata_indexer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Hyrax
33
class BasicMetadataIndexer < ActiveFedora::RDF::IndexingService
44
class_attribute :stored_and_facetable_fields, :stored_fields, :symbol_fields
55
self.stored_and_facetable_fields = %i[resource_type creator contributor keyword publisher subject language based_near]
6-
self.stored_fields = %i[description abstract license rights_statement rights_notes access_right date_created identifier related_url bibliographic_citation source]
6+
self.stored_fields = %i[alt_title description abstract license rights_statement rights_notes access_right date_created identifier related_url bibliographic_citation source]
77
self.symbol_fields = %i[import_url]
88

99
private

app/models/admin_set.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class AdminSet < ActiveFedora::Base
3131
property :title, predicate: ::RDF::Vocab::DC.title do |index|
3232
index.as :stored_searchable, :facetable
3333
end
34+
35+
property :alt_title, predicate: ::RDF::Vocab::DC.alternative do |index|
36+
index.as :stored_searchable
37+
end
38+
3439
property :description, predicate: ::RDF::Vocab::DC.description do |index|
3540
index.as :stored_searchable
3641
end

app/models/concerns/hyrax/basic_metadata.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module BasicMetadata
66
extend ActiveSupport::Concern
77

88
included do
9+
property :alt_title, predicate: ::RDF::Vocab::DC.alternative
10+
911
property :label, predicate: ActiveFedora::RDF::Fcrepo::Model.downloadFilename, multiple: false
1012

1113
property :relative_path, predicate: ::RDF::URI.new('http://scholarsphere.psu.edu/ns#relativePath'), multiple: false

app/models/concerns/hyrax/solr_document/metadata.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def self.coerce(input)
4444
end
4545

4646
included do
47+
attribute :alt_title, Solr::Array, solr_name('alt_title')
4748
attribute :identifier, Solr::Array, solr_name('identifier')
4849
attribute :based_near, Solr::Array, solr_name('based_near')
4950
attribute :based_near_label, Solr::Array, solr_name('based_near_label')

app/presenters/hyrax/work_show_presenter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def page_title
3939
delegate :title, :date_created, :description,
4040
:creator, :contributor, :subject, :publisher, :language, :embargo_release_date,
4141
:lease_expiration_date, :license, :source, :rights_statement, :thumbnail_id, :representative_id,
42-
:rendering_ids, :member_of_collection_ids, to: :solr_document
42+
:rendering_ids, :member_of_collection_ids, :alt_title, to: :solr_document
4343

4444
def workflow
4545
@workflow ||= WorkflowPresenter.new(solr_document, current_ability)

app/views/hyrax/dashboard/collections/edit.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<% provide :page_title, construct_page_title( t('.header', type_title: @collection.collection_type.title, title: @form.title.first) ) %>
1+
<% provide :page_title, construct_page_title( t('.header', type_title: @collection.collection_type.title, title: @form.title) ) %>
22

33
<% provide :page_header do %>
4-
<h1><span class="fa fa-edit" aria-hidden="true"></span><%= t('.header', type_title: @collection.collection_type.title, title: @form.title.first) %></h1>
4+
<h1><span class="fa fa-edit" aria-hidden="true"></span><%= t('.header', type_title: @collection.collection_type.title, title: @form.title) %></h1>
55
<% end %>
66

77
<div class="row">
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<% f.object.alt_title.each do |value| %>
2+
<%= f.hidden_field :alt_title, multiple: true, value: value.to_s %>
3+
<% end %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= f.input :title, required: f.object.required?(:title) %>

spec/features/dashboard/collection_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@
244244
click_on('Create collection')
245245

246246
expect(page).to have_selector('h1', text: 'New User Collection')
247-
expect(page).to have_selector "input.collection_title.multi_value"
248247

249248
click_link('Additional fields')
250249
expect(page).to have_selector "input.collection_creator.multi_value"
@@ -275,7 +274,6 @@
275274
it 'makes a new collection' do
276275
click_link "New Collection"
277276
expect(page).to have_selector('h1', text: 'New User Collection')
278-
expect(page).to have_selector "input.collection_title.multi_value"
279277

280278
click_link('Additional fields')
281279
expect(page).to have_selector "input.collection_creator.multi_value"
@@ -335,15 +333,15 @@
335333
let!(:adminset) { create(:admin_set, title: ['Admin Set with Work'], creator: [admin_user.user_key], with_permission_template: true) }
336334
let!(:work) { create(:work, title: ["King Louie"], admin_set: adminset, member_of_collections: [collection], user: user) }
337335

338-
# check table row has appropriate data attributes added
336+
# Check table row has appropriate data attributes added
339337
def check_tr_data_attributes(id, type)
340338
url_fragment = get_url_fragment(type)
341339
expect(page).to have_selector("tr[data-id='#{id}'][data-colls-hash]")
342340
expect(page).to have_selector("tr[data-post-url='/dashboard/collections/#{id}/within?locale=en']")
343341
expect(page).to have_selector("tr[data-post-delete-url='/#{url_fragment}/#{id}?locale=en']")
344342
end
345343

346-
# check data attributes have been transferred from table row to the modal
344+
# Check data attributes have been transferred from table row to the modal
347345
def check_modal_data_attributes(id, type)
348346
url_fragment = get_url_fragment(type)
349347
expect(page).to have_selector("div[data-id='#{id}']")

spec/forms/hyrax/forms/batch_upload_form_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
subject { form.terms }
4242

4343
it do
44-
is_expected.to eq [:creator,
44+
is_expected.to eq [:alt_title,
45+
:creator,
4546
:contributor,
4647
:description,
4748
:abstract,

spec/forms/hyrax/forms/collection_form_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
subject { described_class.terms }
44

55
it do
6-
is_expected.to eq [:resource_type,
6+
is_expected.to eq [:alt_title,
7+
:resource_type,
78
:title,
89
:creator,
910
:contributor,
@@ -40,6 +41,7 @@
4041

4142
it do
4243
is_expected.to eq [
44+
:alt_title,
4345
:creator,
4446
:contributor,
4547
:keyword,
@@ -127,7 +129,8 @@
127129
subject { described_class.build_permitted_params }
128130

129131
it do
130-
is_expected.to eq [{ resource_type: [] },
132+
is_expected.to eq [{ alt_title: [] },
133+
{ resource_type: [] },
131134
{ title: [] },
132135
{ creator: [] },
133136
{ contributor: [] },

spec/forms/hyrax/forms/work_form_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
let(:params) { ActionController::Parameters.new(attributes) }
101101
let(:attributes) do
102102
{
103-
title: ['foo'],
103+
title: ['aaa', 'bbb', 'ccc'],
104104
description: [''],
105105
abstract: [''],
106106
visibility: 'open',
@@ -118,8 +118,8 @@
118118

119119
subject { described_class.model_attributes(params) }
120120

121-
it 'permits metadata parameters' do
122-
expect(subject['title']).to eq ['foo']
121+
it 'permits parameters' do
122+
expect(subject['title']).to eq ['aaa', 'bbb', 'ccc']
123123
expect(subject['description']).to be_empty
124124
expect(subject['abstract']).to be_empty
125125
expect(subject['visibility']).to eq 'open'

spec/forms/hyrax/generic_work_form_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
end
9191

9292
it 'removes blank parameters' do
93-
expect(subject['title']).to be_empty
93+
expect(subject['title']).to eq ['']
9494
expect(subject['description']).to be_empty
9595
expect(subject['license']).to be_empty
9696
expect(subject['keyword']).to be_empty
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
RSpec.describe 'records/edit_fields/_title.html.erb', type: :view do
2+
let(:work) { GenericWork.new }
3+
let(:form) { Hyrax::GenericWorkForm.new(work, nil, controller) }
4+
5+
let(:form_template) do
6+
%(
7+
<%= simple_form_for [main_app, @form] do |f| %>
8+
<%= render "records/edit_fields/alt_title", f: f, key: 'description' %>
9+
<% end %>
10+
)
11+
end
12+
13+
before do
14+
work.title = ["bbb", "aaa", "ccc"]
15+
work.alt_title = []
16+
assign(:form, form)
17+
end
18+
19+
context "when there are 3 titles" do
20+
it 'hides the last 2 after alphabetizing all 3 titles' do
21+
render inline: form_template
22+
expect(rendered).to have_selector('input[type="hidden"][value="bbb"]', visible: false)
23+
expect(rendered).to have_selector('input[type="hidden"][value="ccc"]', visible: false)
24+
end
25+
end
26+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
RSpec.describe 'records/edit_fields/_title.html.erb', type: :view do
2+
let(:work) { GenericWork.new }
3+
let(:form) { Hyrax::GenericWorkForm.new(work, nil, controller) }
4+
5+
let(:form_template) do
6+
%(
7+
<%= simple_form_for [main_app, @form] do |f| %>
8+
<%= render "records/edit_fields/title", f: f, key: 'description' %>
9+
<% end %>
10+
)
11+
end
12+
13+
before do
14+
work.title = ["ccc", "bbb", "aaa"]
15+
assign(:form, form)
16+
end
17+
18+
context "when there are 3 titles" do
19+
it 'displays the first after alphabetizing the list' do
20+
render inline: form_template
21+
expect(rendered).to have_selector('input[class="form-control string required"][value="aaa"]')
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)