From d951457bc773150bfdef3ab86af46d22d075b6c9 Mon Sep 17 00:00:00 2001 From: Blanco Date: Fri, 22 Feb 2019 11:52:32 -0500 Subject: [PATCH] in the form make title a single entry, but the title def stays as multiple in the rest of the code. --- app/forms/hyrax/forms/admin_set_form.rb | 8 ++++ app/forms/hyrax/forms/collection_form.rb | 37 +++++++++++++++++-- app/forms/hyrax/forms/work_form.rb | 26 ++++++++++++- app/indexers/hyrax/basic_metadata_indexer.rb | 2 +- app/models/admin_set.rb | 5 +++ app/models/concerns/hyrax/basic_metadata.rb | 2 + .../concerns/hyrax/solr_document/metadata.rb | 1 + app/presenters/hyrax/work_show_presenter.rb | 2 +- .../records/edit_fields/_alt_title.html.erb | 3 ++ app/views/records/edit_fields/_title.html.erb | 1 + .../hyrax/forms/batch_upload_form_spec.rb | 3 +- .../forms/hyrax/forms/collection_form_spec.rb | 7 +++- spec/forms/hyrax/forms/work_form_spec.rb | 4 +- .../edit_fields/_alt_title.html.erb_spec.rb | 26 +++++++++++++ .../edit_fields/_title.html.erb_spec.rb | 24 ++++++++++++ 15 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 app/views/records/edit_fields/_alt_title.html.erb create mode 100644 app/views/records/edit_fields/_title.html.erb create mode 100644 spec/views/records/edit_fields/_alt_title.html.erb_spec.rb create mode 100644 spec/views/records/edit_fields/_title.html.erb_spec.rb diff --git a/app/forms/hyrax/forms/admin_set_form.rb b/app/forms/hyrax/forms/admin_set_form.rb index c4cc8e2517..4760025935 100644 --- a/app/forms/hyrax/forms/admin_set_form.rb +++ b/app/forms/hyrax/forms/admin_set_form.rb @@ -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 diff --git a/app/forms/hyrax/forms/collection_form.rb b/app/forms/hyrax/forms/collection_form.rb index 95c7ad8731..98d5a87f73 100644 --- a/app/forms/hyrax/forms/collection_form.rb +++ b/app/forms/hyrax/forms/collection_form.rb @@ -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 @@ -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] @@ -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) @@ -60,7 +90,8 @@ def primary_terms # Terms that appear within the accordion def secondary_terms - [:creator, + [:alt_title, + :creator, :contributor, :keyword, :license, diff --git a/app/forms/hyrax/forms/work_form.rb b/app/forms/hyrax/forms/work_form.rb index 094a310b54..9022ab3d0b 100644 --- a/app/forms/hyrax/forms/work_form.rb +++ b/app/forms/hyrax/forms/work_form.rb @@ -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, + self.terms = [:title, :alt_title, :creator, :contributor, :description, :keyword, :license, :rights_statement, :publisher, :date_created, :subject, :language, :identifier, :based_near, :related_url, :representative_id, :thumbnail_id, :rendering_ids, :files, @@ -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 @@ -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 diff --git a/app/indexers/hyrax/basic_metadata_indexer.rb b/app/indexers/hyrax/basic_metadata_indexer.rb index a09c6ac819..5e0aac6e79 100644 --- a/app/indexers/hyrax/basic_metadata_indexer.rb +++ b/app/indexers/hyrax/basic_metadata_indexer.rb @@ -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 license rights_statement date_created identifier related_url bibliographic_citation source] + self.stored_fields = %i[alt_title description license rights_statement date_created identifier related_url bibliographic_citation source] self.symbol_fields = %i[import_url] private diff --git a/app/models/admin_set.rb b/app/models/admin_set.rb index 64baf02de6..a4faafd427 100644 --- a/app/models/admin_set.rb +++ b/app/models/admin_set.rb @@ -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 diff --git a/app/models/concerns/hyrax/basic_metadata.rb b/app/models/concerns/hyrax/basic_metadata.rb index 0012bdf32e..9211c509cf 100644 --- a/app/models/concerns/hyrax/basic_metadata.rb +++ b/app/models/concerns/hyrax/basic_metadata.rb @@ -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 diff --git a/app/models/concerns/hyrax/solr_document/metadata.rb b/app/models/concerns/hyrax/solr_document/metadata.rb index 4105ae074f..bbf713e778 100644 --- a/app/models/concerns/hyrax/solr_document/metadata.rb +++ b/app/models/concerns/hyrax/solr_document/metadata.rb @@ -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') diff --git a/app/presenters/hyrax/work_show_presenter.rb b/app/presenters/hyrax/work_show_presenter.rb index adf74d16d1..240ab6d2e3 100644 --- a/app/presenters/hyrax/work_show_presenter.rb +++ b/app/presenters/hyrax/work_show_presenter.rb @@ -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) diff --git a/app/views/records/edit_fields/_alt_title.html.erb b/app/views/records/edit_fields/_alt_title.html.erb new file mode 100644 index 0000000000..11e040d941 --- /dev/null +++ b/app/views/records/edit_fields/_alt_title.html.erb @@ -0,0 +1,3 @@ +<% f.object.alt_title.each do |value| %> + <%= f.hidden_field :alt_title, multiple: true, value: value.to_s %> +<% end %> diff --git a/app/views/records/edit_fields/_title.html.erb b/app/views/records/edit_fields/_title.html.erb new file mode 100644 index 0000000000..cefb71391d --- /dev/null +++ b/app/views/records/edit_fields/_title.html.erb @@ -0,0 +1 @@ +<%= f.input :title, required: f.object.required?(:title) %> \ No newline at end of file diff --git a/spec/forms/hyrax/forms/batch_upload_form_spec.rb b/spec/forms/hyrax/forms/batch_upload_form_spec.rb index 21b4325949..6a4221a751 100644 --- a/spec/forms/hyrax/forms/batch_upload_form_spec.rb +++ b/spec/forms/hyrax/forms/batch_upload_form_spec.rb @@ -41,7 +41,8 @@ subject { form.terms } it do - is_expected.to eq [:creator, + is_expected.to eq [:alt_title, + :creator, :contributor, :description, :keyword, diff --git a/spec/forms/hyrax/forms/collection_form_spec.rb b/spec/forms/hyrax/forms/collection_form_spec.rb index c376028c44..799a788fa6 100644 --- a/spec/forms/hyrax/forms/collection_form_spec.rb +++ b/spec/forms/hyrax/forms/collection_form_spec.rb @@ -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, @@ -40,6 +41,7 @@ it do is_expected.to eq [ + :alt_title, :creator, :contributor, :keyword, @@ -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: [] }, diff --git a/spec/forms/hyrax/forms/work_form_spec.rb b/spec/forms/hyrax/forms/work_form_spec.rb index 1760a7c064..07f8a69a9a 100644 --- a/spec/forms/hyrax/forms/work_form_spec.rb +++ b/spec/forms/hyrax/forms/work_form_spec.rb @@ -100,7 +100,7 @@ let(:params) { ActionController::Parameters.new(attributes) } let(:attributes) do { - title: ['foo'], + title: ['aaa', 'bbb', 'ccc'], description: [''], visibility: 'open', parent_id: '123', @@ -116,7 +116,7 @@ subject { described_class.model_attributes(params) } it 'permits parameters' do - expect(subject['title']).to eq ['foo'] + expect(subject['title']).to eq ['aaa', 'bbb', 'ccc'] expect(subject['description']).to be_empty expect(subject['visibility']).to eq 'open' expect(subject['license']).to eq ['http://creativecommons.org/licenses/by/3.0/us/'] diff --git a/spec/views/records/edit_fields/_alt_title.html.erb_spec.rb b/spec/views/records/edit_fields/_alt_title.html.erb_spec.rb new file mode 100644 index 0000000000..d4aee8afbd --- /dev/null +++ b/spec/views/records/edit_fields/_alt_title.html.erb_spec.rb @@ -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 diff --git a/spec/views/records/edit_fields/_title.html.erb_spec.rb b/spec/views/records/edit_fields/_title.html.erb_spec.rb new file mode 100644 index 0000000000..03563786a4 --- /dev/null +++ b/spec/views/records/edit_fields/_title.html.erb_spec.rb @@ -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