Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1247 from pulibrary/controlled_vocabulary_qa
Browse files Browse the repository at this point in the history
Add language dropdown from Vocabularies.
  • Loading branch information
escowles committed May 4, 2017
2 parents de49019 + 8e77d99 commit d082463
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Metrics/ClassLength:
- 'app/controllers/catalog_controller.rb'
- 'app/jobs/ingest_mets_job.rb'

Metrics/CyclomaticComplexity:
Exclude:
- 'app/indexers/work_indexer.rb'

Metrics/PerceivedComplexity:
Exclude:
- 'app/indexers/work_indexer.rb'

Metrics/MethodLength:
Enabled: false

Expand Down
8 changes: 6 additions & 2 deletions app/indexers/work_indexer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class WorkIndexer < Hyrax::WorkIndexer
# TODO: Refactor complexity here.
def generate_solr_document
super.tap do |solr_doc|
object.member_of_collections.each do |col|
solr_doc[Solrizer.solr_name('member_of_collection_slugs', :symbol)] = col.exhibit_id
key = Solrizer.solr_name('member_of_collection_slugs', :symbol)
solr_doc[key] ||= []
solr_doc[key] << col.try(:exhibit_id)
solr_doc[key].compact!
end
(PlumSchema.display_fields + [:title]).each do |field|
objects = object.get_values(field, literal: true)
Expand All @@ -25,7 +29,7 @@ def generate_solr_document
end
solr_doc[Solrizer.solr_name("identifier", :symbol)] = object.identifier
solr_doc[Solrizer.solr_name("language", :facetable)] = object.language.map do |code|
LanguageService.label(code)
AuthorityFinder.for(property: :language, model: object).try(:find, code).try(:[], :label) || LanguageService.label(code)
end

suppress solr_doc, 'source_metadata'
Expand Down
1 change: 1 addition & 0 deletions app/models/ephemera_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class EphemeraFolder < ActiveFedora::Base
ActiveFedora::Indexers::GlobalIndexer.new([:symbol, :stored_searchable, :facetable])
)
include ::CommonMetadata
self.indexer = ::WorkIndexer
# Change this to restrict which works can be added as a child.
# self.valid_child_concerns = []
validates :title, presence: { message: 'Your work must have a title.' }
Expand Down
5 changes: 5 additions & 0 deletions app/models/vocabulary.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class Vocabulary < ApplicationRecord
belongs_to :parent, class_name: "Vocabulary"
validates :label, presence: true
after_create :register_authority

def register_authority
Qa::Authorities::Local.register_subauthority(label, 'VocabularySubauthority')
end
end
5 changes: 3 additions & 2 deletions app/models/vocabulary_subauthority.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class VocabularySubauthority < Qa::Authorities::Base
attr_reader :vocabulary
attr_reader :vocabulary, :subauthority_name

def initialize(subauthority_name)
@subauthority_name = subauthority_name
@vocabulary = Vocabulary.find_by(label: subauthority_name)
end

Expand All @@ -26,6 +27,6 @@ def sub_vocabs
end

def format_item(item)
{ id: item.id, label: item.label, type: item.class.name }
{ id: item.id, label: item.label, type: item.class.name, active: true }.with_indifferent_access
end
end
6 changes: 6 additions & 0 deletions app/presenters/ephemera_folder_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ def renderer_for(field, _options)
return ::BarcodeAttributeRenderer if field == :identifier
super
end

def language
super.map do |id|
AuthorityFinder.for(property: :language, model: self).find(id)[:label]
end
end
end
14 changes: 14 additions & 0 deletions app/services/authority_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AuthorityFinder
def self.for(property:, model:)
return unless model.model_name.singular == "ephemera_folder"
begin
case property
when :language
Qa::Authorities::Local.subauthority_for('languages')
end
rescue Qa::InvalidSubAuthority
Rails.logger.debug("Non-existent sub-authority requested for property #{property}")
nil
end
end
end
10 changes: 10 additions & 0 deletions app/views/records/edit_fields/_language.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% authority = AuthorityFinder.for(property: key, model: f.object) %>
<% if authority %>
<% languages = Hyrax::QaSelectService.new(authority.subauthority_name) %>
<%= f.input :language,
collection: languages.select_active_options,
include_blank: true,
input_html: { class: 'form-control', multiple: f.object.multiple?(key) } %>
<% else %>
<%= render "records/edit_fields/default", f: f, key: key%>
<% end %>
6 changes: 6 additions & 0 deletions spec/controllers/vocabularies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
}.to change(Vocabulary, :count).by(1)
end

it "registers the newly created Vocabulary" do
post :create, params: { vocabulary: valid_attributes }
vocab = Vocabulary.first
expect(Qa::Authorities::Local.subauthority_for(vocab.label)).not_to be_blank
end

it "assigns a newly created vocabulary as @vocabulary" do
post :create, params: { vocabulary: valid_attributes }
expect(assigns(:vocabulary)).to be_a(Vocabulary)
Expand Down
22 changes: 22 additions & 0 deletions spec/models/ephemera_folder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@
end
end

describe "language indexing" do
context "when given a set of language IDs" do
after do
Qa::Authorities::Local.registry.instance_variable_get(:@hash).delete("languages")
end
before do
Qa::Authorities::Local.registry.instance_variable_get(:@hash).delete("languages")
vocabulary
end
let(:vocabulary) do
Vocabulary.create!(label: "languages").tap do |vocab|
VocabularyTerm.create!(vocabulary: vocab, label: "English")
VocabularyTerm.create!(vocabulary: vocab, label: "Japanese")
end
end
let(:folder) { FactoryGirl.build(:ephemera_folder, language: [VocabularyTerm.first.id.to_s]) }
it "Returns the label for the IDs" do
expect(folder.to_solr["language_sim"]).to eq ["English"]
end
end
end

describe "box and box_id" do
let(:box) { FactoryGirl.create :ephemera_box }
let(:col) { FactoryGirl.build :collection }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/scanned_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
let(:solr_doc) { scanned_resource.to_solr }
it "indexes collection" do
expect(solr_doc['member_of_collections_ssim']).to eq(['Test Collection'])
expect(solr_doc['member_of_collection_slugs_ssim']).to eq(scanned_resource.member_of_collections.first.exhibit_id)
expect(solr_doc['member_of_collection_slugs_ssim']).to eq([scanned_resource.member_of_collections.first.exhibit_id])
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/models/vocabulary_subauthority_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
subject { described_class.new(parent.label) }

it 'lists terms and vocabularies' do
child_hash = { id: child.id, label: child.label, type: child.class.name }
term_hash = { id: term.id, label: term.label, type: term.class.name }
child_hash = { id: child.id, label: child.label, type: child.class.name, active: true }.with_indifferent_access
term_hash = { id: term.id, label: term.label, type: term.class.name, active: true }.with_indifferent_access
expect(subject.all).to contain_exactly(child_hash, term_hash)
end

it 'finds terms by their ids' do
term_hash = { id: term.id, label: term.label, type: term.class.name }
term_hash = { id: term.id, label: term.label, type: term.class.name, active: true }.with_indifferent_access
expect(subject.find(term.id)).to eq(term_hash)
end
end
22 changes: 22 additions & 0 deletions spec/presenters/ephemera_folder_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
let(:controller) { double(blacklight_config: blacklight_config) }
subject { described_class.new(SolrDocument.new(folder.to_solr), controller) }

describe "#language" do
context "when given a set of language IDs" do
after do
Qa::Authorities::Local.registry.instance_variable_get(:@hash).delete("languages")
end
before do
Qa::Authorities::Local.registry.instance_variable_get(:@hash).delete("languages")
vocabulary
end
let(:vocabulary) do
Vocabulary.create!(label: "languages").tap do |vocab|
VocabularyTerm.create!(vocabulary: vocab, label: "English")
VocabularyTerm.create!(vocabulary: vocab, label: "Japanese")
end
end
let(:folder) { FactoryGirl.build(:ephemera_folder, language: [VocabularyTerm.first.id.to_s]) }
it "Returns the label for the IDs" do
expect(subject.language).to eq ["English"]
end
end
end

describe "#renderer_for" do
it "renders identifier as a barcodde" do
expect(subject.renderer_for(:identifier, {})).to be BarcodeAttributeRenderer
Expand Down
7 changes: 7 additions & 0 deletions spec/views/records/edit_fields/_identifier.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
end
end

context 'with an ephemera folder' do
let(:form) { Hyrax::EphemeraFolderForm.new(EphemeraFolder.new, nil, nil) }
it 'auto-focuses the barcode field' do
expect(rendered).to have_selector 'input#ephemera_folder_identifier[@autofocus="autofocus"]'
end
end

context 'with an ephemera box' do
let(:form) { Hyrax::ScannedResourceForm.new(ScannedResource.new, nil, nil) }
it 'auto-focuses the barcode field' do
Expand Down
49 changes: 49 additions & 0 deletions spec/views/records/edit_fields/_language.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'rails_helper'
include SimpleForm::ActionViewExtensions::FormHelper

RSpec.describe 'records/edit_fields/_language.html.erb' do
let(:vocabulary) { nil }
before do
Qa::Authorities::Local.registry.instance_variable_get(:@hash).delete("languages")
allow(view).to receive(:f).and_return(simple_form_for(form))
allow(view).to receive(:key).and_return(:language)
vocabulary
render
end
after do
Qa::Authorities::Local.registry.instance_variable_get(:@hash).delete("languages")
end

context 'with an ephemera folder' do
let(:form) { Hyrax::EphemeraBoxForm.new(EphemeraFolder.new, nil, nil) }
context "when there is no language vocabulary" do
it "doesn't create a select box" do
expect(rendered).not_to have_selector "select"
end
end
context "when there is a language vocabulary" do
let(:vocabulary) do
Vocabulary.create!(label: "languages").tap do |vocab|
VocabularyTerm.create!(vocabulary: vocab, label: "English")
VocabularyTerm.create!(vocabulary: vocab, label: "Japanese")
end
end
it "creates a select box" do
expect(rendered).to have_select("Language", options: ['', 'English', 'Japanese'])
end
end
end

context 'with a scanned resource' do
let(:form) { Hyrax::ScannedResourceForm.new(ScannedResource.new, nil, nil) }
let(:vocabulary) do
Vocabulary.create!(label: "languages").tap do |vocab|
VocabularyTerm.create!(vocabulary: vocab, label: "English")
VocabularyTerm.create!(vocabulary: vocab, label: "Japanese")
end
end
it "doesn't create a select box" do
expect(rendered).not_to have_selector "select"
end
end
end

0 comments on commit d082463

Please sign in to comment.