Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating allinson flex features #231

Merged
merged 9 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/concerns/iiif_print/solr/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ def file_set_ids
def any_highlighting?
response&.[]('highlighting')&.[](id)&.present?
end

def solr_document
self
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module IiifManifestPresenterBehavior
attr_accessor :base_url

def manifest_metadata
@manifest_metadata ||= IiifPrint.manifest_metadata_from(work: model, presenter: self)
# ensure we are using a SolrDocument
@manifest_metadata ||= IiifPrint.manifest_metadata_from(work: model.solr_document, presenter: self)
end

def search_service
Expand Down
50 changes: 31 additions & 19 deletions lib/iiif_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,38 +154,38 @@ def self.default_fields(fields: config.metadata_fields)
end
end

def self.fields_for_allinson_flex(fields: allinson_flex_fields)
fields.map do |field|
##
# @param fields [Array<IiifPrint::Field>]
def self.fields_for_allinson_flex(fields: allinson_flex_fields, sort_order: IiifPrint.config.iiif_metadata_field_presentation_order)
fields = sort_af_fields!(fields, sort_order: sort_order)
fields.each_with_object({}) do |field, hash|
# filters out admin_only fields
next if field.indexing&.include?('admin_only')

# WARNING: This is assuming A LOT
# This is taking the Allinson Flex fields that have the same name and only
# using the first one while discarding the rest. There currently no way to
# controller which one(s) are discarded but this fits for the moment.
next if hash.key?(field.name)

# currently only supports the faceted option
# Why the `render_as:`? This was originally derived from Hyku default attributes
# @see https://github.com/samvera/hyku/blob/c702844de4c003eaa88eb5a7514c7a1eae1b289e/app/views/hyrax/base/_attribute_rows.html.erb#L3
options = field.indexing.include?('facetable') ? { render_as: :faceted } : nil
Field.new(
hash[field.name] = Field.new(
name: field.name,
label: field.value,
options: options
options: field.indexing&.include?('facetable') ? { render_as: :faceted } : nil
)
end
end.values
end

CollectionFieldShim = Struct.new(:name, :value, :indexing, keyword_init: true)

##
# @return [Array<IiifPrint::Field>]
def self.allinson_flex_fields
return @allinson_flex_fields if defined?(@allinson_flex_fields)

# sql query method was refactored to active record
# original query:
# AllinsonFlex::ProfileProperty
# .find_by_sql(
# "SELECT DISTINCT allinson_flex_profile_texts.value AS label, " \
# "allinson_flex_profile_properties.name AS name " \
# "FROM allinson_flex_profile_properties " \
# "JOIN allinson_flex_profile_texts " \
# "ON allinson_flex_profile_properties.id = " \
# "allinson_flex_profile_texts.profile_property_id " \
# "WHERE allinson_flex_profile_texts.name = 'display_label'"
# )
# In this ActiveRecord query, allinson_flex_profile_properties.indexing was added
allinson_flex_relation = AllinsonFlex::ProfileProperty
.joins(:texts)
.where(allinson_flex_profile_texts: { name: 'display_label' })
Expand All @@ -198,4 +198,16 @@ def self.allinson_flex_fields
end
@allinson_flex_fields = flex_fields
end

##
# @param fields [Array<IiifPrint::Field>]
# @param sort_order [Array<Symbol>]
def self.sort_af_fields!(fields, sort_order:)
return fields if sort_order.blank?

fields.sort_by do |field|
sort_order_index = sort_order.index(field.name.to_sym)
sort_order_index.nil? ? sort_order.length : sort_order_index
end
end
end
23 changes: 23 additions & 0 deletions lib/iiif_print/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ def all_text_generator_function
IiifPrint::Data::WorkDerivatives.data(from: object, of_type: 'txt')
end
end

attr_writer :iiif_metadata_field_presentation_order
##
# This is the default sorter for the metadata. It will sort by the order of the keys specificied.
# By default, this is turned off as it returns nil. If you want to turn it on, you can set this
# this to an array of symbols the properties on the work.
#
# @example [:title, :description, :date_created]
# @return [Array<Symbol>]
def iiif_metadata_field_presentation_order
@iiif_metadata_field_presentation_order || nil
end

def questioning_authority_fields=(fields)
@questioning_authority_fields = Array.wrap(fields).map(&:to_s)
end

##
# This is used to explicitly set which fields should be rendered as a Questioning Authority in the UV.
# By default, we render `rights_statement` and `license` as QA fields.
def questioning_authority_fields
@questioning_authority_fields ||= ['rights_statement', 'license']
end
end
# rubocop:enable Metrics/ModuleLength
end
6 changes: 5 additions & 1 deletion lib/iiif_print/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def scrub(value)
def cast_to_value(field_name:, options:)
if options&.[](:render_as) == :faceted
faceted_values_for(field_name: field_name)
elsif options&.[](:render_as) == :rights_statement || options&.[](:render_as) == :license
elsif qa_field?(field_name: options&.dig(:render_as) || field_name)
authority_values_for(field_name: field_name)
else
make_link(values_for(field_name: field_name))
Expand All @@ -85,6 +85,10 @@ def faceted_values_for(field_name:)
end
end

def qa_field?(field_name:, questioning_authority_fields: IiifPrint.config.questioning_authority_fields)
questioning_authority_fields.include?(field_name.to_s)
end

def authority_values_for(field_name:)
authority = Qa::Authorities::Local.subauthority_for(field_name.to_s.pluralize)
values_for(field_name: field_name).map do |value|
Expand Down
26 changes: 26 additions & 0 deletions spec/iiif_print/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,30 @@
expect(function.parameters).to eq([[:keyreq, :object]])
end
end

describe "#iiif_metadata_field_presentation_order" do
subject { config.iiif_metadata_field_presentation_order }

it { is_expected.to be_a NilClass }
it "allows for an override" do
original = config.iiif_metadata_field_presentation_order
config.iiif_metadata_field_presentation_order = :title
expect(config.iiif_metadata_field_presentation_order).not_to eq original
end
end

describe "#questioning_authority_fields" do
subject { config.questioning_authority_fields }

it { is_expected.to be_a Array }
context "by default" do
it { is_expected.to eq ['rights_statement', 'license'] }
end

it "allows for an override" do
expect do
config.questioning_authority_fields = ['rights_statement', 'license', 'subject']
end.to change(config, :questioning_authority_fields).from(['rights_statement', 'license']).to(['rights_statement', 'license', 'subject'])
end
end
end
68 changes: 68 additions & 0 deletions spec/iiif_print_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,72 @@
end
end
end

describe ".fields_for_allinson_flex" do
subject { described_class.fields_for_allinson_flex(fields: fields, sort_order: sort_order) }
let(:sort_order) { [] }

context "when the fields include an admin only indexing property" do
let(:fields) do
[
IiifPrint::CollectionFieldShim.new(name: :title, value: "My Title"),
IiifPrint::CollectionFieldShim.new(name: :creator, value: "Hyrax, Sam", indexing: ["admin_only"])
]
end

it "does not include the admin only field" do
# We are mapping from one data structure to another
expect(subject.map(&:name)).to eq([fields.first.name])
end
end

context "when the fields include duplicate name properties" do
let(:fields) do
[
IiifPrint::CollectionFieldShim.new(name: :title, value: "My Title"),
IiifPrint::CollectionFieldShim.new(name: :title, value: "My Other Title")
]
end

it "does not include later duplicates" do
expect(subject.map(&:label)).to eq([fields.first.value])
end
end

context "when we provide a fields sort order" do
let(:fields) do
[
IiifPrint::CollectionFieldShim.new(name: :title, value: "My Title"),
IiifPrint::CollectionFieldShim.new(name: :creator, value: "Hyrax, Sam"),
IiifPrint::CollectionFieldShim.new(name: :date_created, value: "2023-05-02")
]
end
let(:sort_order) { [:creator, :title] }

it "returns the fields in the order specified and puts unspecified fields last" do
expect(subject.map(&:name)).to eq([:creator, :title, :date_created])
end
end
end

describe ".sort_af_fields!" do
let(:fields) { [:title, :creator, :date_created].map { |name| IiifPrint::Field.new(name: name) } }
subject(:sort_af_fields) { described_class.sort_af_fields!(fields, sort_order: sort_order) }

context "when the sort order is an empty array" do
let(:sort_order) { [] }

it "returns the fields in the order they were given" do
expect(sort_af_fields).to eq(fields)
end
end

context "when the sort order specifies some of the fields" do
let(:sort_order) { [:date_created, :title] }

it "returns the fields in the order specified and puts unspecified fields last" do
expect(sort_af_fields).to eq([:date_created, :title, :creator].map { |name| IiifPrint::Field.new(name: name) })
end
end
end
end