Skip to content

Commit

Permalink
treat title as a single value even though it is multivalue on form
Browse files Browse the repository at this point in the history
  • Loading branch information
blancoj committed Apr 24, 2019
1 parent 523fdad commit c2bc313
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 20 deletions.
8 changes: 8 additions & 0 deletions app/forms/hyrax/forms/admin_set_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ class AdminSetForm < Hyrax::Forms::CollectionForm
# Cast any array values on the model to scalars.
def [](key)
return super if key == :thumbnail_id
if key == :title
@attributes["title"].each do |value|
@attributes["alt_title"] << value
end
@attributes["alt_title"].delete(@attributes["alt_title"].sort.first) unless @attributes["alt_title"].empty?
return @attributes["title"].sort.first unless @attributes["title"].empty?
return ""
end
super.first
end

Expand Down
37 changes: 34 additions & 3 deletions app/forms/hyrax/forms/collection_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CollectionForm
# Used by the search builder
attr_reader :scope

delegate :id, :depositor, :permissions, :human_readable_type, :member_ids, :nestable?, to: :model
delegate :id, :depositor, :permissions, :human_readable_type, :member_ids, :nestable?, :alt_title, to: :model

class_attribute :membership_service_class

Expand All @@ -20,7 +20,7 @@ class CollectionForm

delegate :blacklight_config, to: Hyrax::CollectionsController

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

# Cast back to multi-value when saving
# Reads from form
def self.model_attributes(attributes)
attrs = super
return attrs unless attributes[:title]

attrs[:title] = Array(attributes[:title])
return attrs if attributes[:alt_title].nil?
Array(attributes[:alt_title]).each do |value|
attrs["title"] << value if value != ""
end
attrs
end

# @param [Symbol] key the field to read
# @return the value of the form field.
# For display in edit page
def [](key)
return model.member_of_collection_ids if key == :member_of_collection_ids
if key == :title
@attributes["title"].each do |value|
@attributes["alt_title"] << value
end
@attributes["alt_title"].delete(@attributes["alt_title"].sort.first) unless @attributes["alt_title"].empty?
return @attributes["title"].sort.first unless @attributes["title"].empty?
return ""
end
super
end

def permission_template
@permission_template ||= begin
template_model = PermissionTemplate.find_or_create_by(source_id: model.id)
Expand All @@ -60,7 +90,8 @@ def primary_terms

# Terms that appear within the accordion
def secondary_terms
[:creator,
[:alt_title,
:creator,
:contributor,
:keyword,
:license,
Expand Down
26 changes: 24 additions & 2 deletions app/forms/hyrax/forms/work_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class WorkForm
:visibility_during_embargo, :embargo_release_date, :visibility_after_embargo,
:visibility_during_lease, :lease_expiration_date, :visibility_after_lease,
:visibility, :in_works_ids, :depositor, :on_behalf_of, :permissions,
:member_ids, to: :model
:member_ids, :alt_title, to: :model

attr_reader :agreement_accepted

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

# Cast back to multi-value when saving
# Reads from form
def self.model_attributes(attributes)
attrs = super
return attrs unless attributes[:title]

attrs[:title] = Array(attributes[:title])
return attrs if attributes[:alt_title].nil?
Array(attributes[:alt_title]).each do |value|
attrs["title"] << value if value != ""
end
attrs
end

# when the add_works_to_collection parameter is set, they mean to create
# a new work and add it to that collection.
def member_of_collections
Expand Down Expand Up @@ -95,8 +109,16 @@ def initialize_field(key)

# @param [Symbol] key the field to read
# @return the value of the form field.
# For display in edit page
def [](key)
return model.member_of_collection_ids if key == :member_of_collection_ids
if key == :title
@attributes["title"].each do |value|
@attributes["alt_title"] << value
end
@attributes["alt_title"].delete(@attributes["alt_title"].sort.first) unless @attributes["alt_title"].empty?
return @attributes[key.to_s].sort.first
end
super
end

Expand Down
2 changes: 1 addition & 1 deletion app/indexers/hyrax/basic_metadata_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Hyrax
class BasicMetadataIndexer < ActiveFedora::RDF::IndexingService
class_attribute :stored_and_facetable_fields, :stored_fields, :symbol_fields
self.stored_and_facetable_fields = %i[resource_type creator contributor keyword publisher subject language based_near]
self.stored_fields = %i[description abstract license rights_statement rights_notes access_right date_created identifier related_url bibliographic_citation source]
self.stored_fields = %i[alt_title description abstract license rights_statement rights_notes access_right date_created identifier related_url bibliographic_citation source]
self.symbol_fields = %i[import_url]

private
Expand Down
5 changes: 5 additions & 0 deletions app/models/admin_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class AdminSet < ActiveFedora::Base
property :title, predicate: ::RDF::Vocab::DC.title do |index|
index.as :stored_searchable, :facetable
end

property :alt_title, predicate: ::RDF::Vocab::DC.alternative do |index|
index.as :stored_searchable
end

property :description, predicate: ::RDF::Vocab::DC.description do |index|
index.as :stored_searchable
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/hyrax/basic_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module BasicMetadata
extend ActiveSupport::Concern

included do
property :alt_title, predicate: ::RDF::Vocab::DC.alternative

property :label, predicate: ActiveFedora::RDF::Fcrepo::Model.downloadFilename, multiple: false

property :relative_path, predicate: ::RDF::URI.new('http://scholarsphere.psu.edu/ns#relativePath'), multiple: false
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/hyrax/solr_document/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def self.coerce(input)
end

included do
attribute :alt_title, Solr::Array, solr_name('alt_title')
attribute :identifier, Solr::Array, solr_name('identifier')
attribute :based_near, Solr::Array, solr_name('based_near')
attribute :based_near_label, Solr::Array, solr_name('based_near_label')
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/hyrax/work_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def page_title
delegate :title, :date_created, :description,
:creator, :contributor, :subject, :publisher, :language, :embargo_release_date,
:lease_expiration_date, :license, :source, :rights_statement, :thumbnail_id, :representative_id,
:rendering_ids, :member_of_collection_ids, to: :solr_document
:rendering_ids, :member_of_collection_ids, :alt_title, to: :solr_document

def workflow
@workflow ||= WorkflowPresenter.new(solr_document, current_ability)
Expand Down
4 changes: 2 additions & 2 deletions app/views/hyrax/dashboard/collections/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% provide :page_title, construct_page_title( t('.header', type_title: @collection.collection_type.title, title: @form.title.first) ) %>
<% provide :page_title, construct_page_title( t('.header', type_title: @collection.collection_type.title, title: @form.title) ) %>
<% provide :page_header do %>
<h1><span class="fa fa-edit" aria-hidden="true"></span><%= t('.header', type_title: @collection.collection_type.title, title: @form.title.first) %></h1>
<h1><span class="fa fa-edit" aria-hidden="true"></span><%= t('.header', type_title: @collection.collection_type.title, title: @form.title) %></h1>
<% end %>

<div class="row">
Expand Down
3 changes: 3 additions & 0 deletions app/views/records/edit_fields/_alt_title.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% f.object.alt_title.each do |value| %>
<%= f.hidden_field :alt_title, multiple: true, value: value.to_s %>
<% end %>
1 change: 1 addition & 0 deletions app/views/records/edit_fields/_title.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= f.input :title, required: f.object.required?(:title) %>
6 changes: 2 additions & 4 deletions spec/features/dashboard/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@
click_on('Create collection')

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

click_link('Additional fields')
expect(page).to have_selector "input.collection_creator.multi_value"
Expand Down Expand Up @@ -275,7 +274,6 @@
it 'makes a new collection' do
click_link "New Collection"
expect(page).to have_selector('h1', text: 'New User Collection')
expect(page).to have_selector "input.collection_title.multi_value"

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

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

# check data attributes have been transferred from table row to the modal
# Check data attributes have been transferred from table row to the modal
def check_modal_data_attributes(id, type)
url_fragment = get_url_fragment(type)
expect(page).to have_selector("div[data-id='#{id}']")
Expand Down
3 changes: 2 additions & 1 deletion spec/forms/hyrax/forms/batch_upload_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
subject { form.terms }

it do
is_expected.to eq [:creator,
is_expected.to eq [:alt_title,
:creator,
:contributor,
:description,
:abstract,
Expand Down
7 changes: 5 additions & 2 deletions spec/forms/hyrax/forms/collection_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
subject { described_class.terms }

it do
is_expected.to eq [:resource_type,
is_expected.to eq [:alt_title,
:resource_type,
:title,
:creator,
:contributor,
Expand Down Expand Up @@ -40,6 +41,7 @@

it do
is_expected.to eq [
:alt_title,
:creator,
:contributor,
:keyword,
Expand Down Expand Up @@ -127,7 +129,8 @@
subject { described_class.build_permitted_params }

it do
is_expected.to eq [{ resource_type: [] },
is_expected.to eq [{ alt_title: [] },
{ resource_type: [] },
{ title: [] },
{ creator: [] },
{ contributor: [] },
Expand Down
6 changes: 3 additions & 3 deletions spec/forms/hyrax/forms/work_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
let(:params) { ActionController::Parameters.new(attributes) }
let(:attributes) do
{
title: ['foo'],
title: ['aaa', 'bbb', 'ccc'],
description: [''],
abstract: [''],
visibility: 'open',
Expand All @@ -118,8 +118,8 @@

subject { described_class.model_attributes(params) }

it 'permits metadata parameters' do
expect(subject['title']).to eq ['foo']
it 'permits parameters' do
expect(subject['title']).to eq ['aaa', 'bbb', 'ccc']
expect(subject['description']).to be_empty
expect(subject['abstract']).to be_empty
expect(subject['visibility']).to eq 'open'
Expand Down
2 changes: 1 addition & 1 deletion spec/forms/hyrax/generic_work_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
end

it 'removes blank parameters' do
expect(subject['title']).to be_empty
expect(subject['title']).to eq ['']
expect(subject['description']).to be_empty
expect(subject['license']).to be_empty
expect(subject['keyword']).to be_empty
Expand Down
26 changes: 26 additions & 0 deletions spec/views/records/edit_fields/_alt_title.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
RSpec.describe 'records/edit_fields/_title.html.erb', type: :view do
let(:work) { GenericWork.new }
let(:form) { Hyrax::GenericWorkForm.new(work, nil, controller) }

let(:form_template) do
%(
<%= simple_form_for [main_app, @form] do |f| %>
<%= render "records/edit_fields/alt_title", f: f, key: 'description' %>
<% end %>
)
end

before do
work.title = ["bbb", "aaa", "ccc"]
work.alt_title = []
assign(:form, form)
end

context "when there are 3 titles" do
it 'hides the last 2 after alphabetizing all 3 titles' do
render inline: form_template
expect(rendered).to have_selector('input[type="hidden"][value="bbb"]', visible: false)
expect(rendered).to have_selector('input[type="hidden"][value="ccc"]', visible: false)
end
end
end
24 changes: 24 additions & 0 deletions spec/views/records/edit_fields/_title.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RSpec.describe 'records/edit_fields/_title.html.erb', type: :view do
let(:work) { GenericWork.new }
let(:form) { Hyrax::GenericWorkForm.new(work, nil, controller) }

let(:form_template) do
%(
<%= simple_form_for [main_app, @form] do |f| %>
<%= render "records/edit_fields/title", f: f, key: 'description' %>
<% end %>
)
end

before do
work.title = ["ccc", "bbb", "aaa"]
assign(:form, form)
end

context "when there are 3 titles" do
it 'displays the first after alphabetizing the list' do
render inline: form_template
expect(rendered).to have_selector('input[class="form-control string required"][value="aaa"]')
end
end
end

0 comments on commit c2bc313

Please sign in to comment.