-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overriding the MetadataConfiguration model and controller along with
extending the Blacklight rendering pipeline in order to support persisting Sir Trevor <textarea> blocks for custom metadata fields
- Loading branch information
1 parent
e25e8be
commit 881a773
Showing
14 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ | |
/coverage/* | ||
/public/uploads | ||
|
||
*# | ||
*\.\#* | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/controllers/pomegranate/metadata_configurations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
module Pomegranate | ||
class MetadataConfigurationsController < Spotlight::MetadataConfigurationsController | ||
private | ||
|
||
def exhibit_configuration_index_params | ||
views = @blacklight_configuration.default_blacklight_config.view.keys | [:show] | ||
|
||
@blacklight_configuration.blacklight_config.index_fields.keys.each_with_object({}) do |element, result| | ||
result[element] = (%i[enabled label weight text_area] | views) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Joins values using configured value or linebreak | ||
class CustomFieldRendering < Blacklight::Rendering::AbstractStep | ||
include ActionView::Helpers::TextHelper | ||
|
||
def render | ||
rendered_values = Array.wrap(values) | ||
|
||
if config.text_area == "1" | ||
begin | ||
parsed = JSON.parse(values) | ||
data = parsed["data"] | ||
rendered_values = data.map { |d| d["data"]["text"] } | ||
rescue JSON::ParserError | ||
# In the case that this is not a JSON data parameter, do not parse the values | ||
rendered_values = Array.wrap(values) | ||
end | ||
end | ||
options = config.separator_options || {} | ||
rendered = rendered_values.to_sentence(options) | ||
|
||
next_step(rendered) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
spec/controllers/pomegranate/metadata_configurations_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'rails_helper' | ||
|
||
describe Pomegranate::MetadataConfigurationsController, type: :controller do | ||
let(:exhibit) { FactoryBot.create(:exhibit) } | ||
let(:user) { FactoryBot.create(:exhibit_admin, exhibit: exhibit) } | ||
|
||
before { sign_in user } | ||
|
||
describe 'PATCH update' do | ||
it 'updates metadata fields' do | ||
blacklight_config = Blacklight::Configuration.new | ||
blacklight_config.add_index_field %w[a b c d e f] | ||
allow(CatalogController).to receive_messages(blacklight_config: blacklight_config) | ||
patch :update, params: { | ||
exhibit_id: exhibit, | ||
blacklight_configuration: { | ||
index_fields: { | ||
c: { enabled: true, show: true, text_area: "0" }, | ||
d: { enabled: true, show: true, text_area: "0" }, | ||
e: { enabled: true, list: true, text_area: "0" }, | ||
f: { enabled: true, list: true, text_area: "1" } | ||
} | ||
} | ||
} | ||
|
||
assigns[:exhibit].tap do |saved| | ||
expect(saved.blacklight_configuration.index_fields).to include 'c', 'd', 'e', 'f' | ||
expect(saved.blacklight_configuration.index_fields['c']).to include text_area: "0" | ||
expect(saved.blacklight_configuration.index_fields['d']).to include text_area: "0" | ||
expect(saved.blacklight_configuration.index_fields['e']).to include text_area: "0" | ||
expect(saved.blacklight_configuration.index_fields['f']).to include text_area: "1" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'rails_helper' | ||
|
||
describe ApplicationHelper, type: :helper do | ||
let(:exhibit) { FactoryBot.create(:exhibit) } | ||
let(:field) { instance_double(Spotlight::CustomField) } | ||
let(:blacklight_config) { Blacklight::Configuration.new } | ||
|
||
before do | ||
blacklight_config.index_fields['foo_tesim'] = OpenStruct.new('text_area' => "1") | ||
allow(field).to receive(:field).and_return("foo_tesim") | ||
allow(exhibit).to receive_messages(blacklight_config: blacklight_config) | ||
end | ||
describe '#text_area?' do | ||
let(:output) { helper.text_area?(field, exhibit) } | ||
|
||
it "determines whether or not a field should be rendered as a text_area" do | ||
expect(output).to be true | ||
end | ||
end | ||
|
||
describe '#text_area_value' do | ||
let(:sidecar) { instance_double(Spotlight::SolrDocumentSidecar) } | ||
let(:output) { helper.text_area_value(field, sidecar) } | ||
let(:data) do | ||
[{ type: "text", data: { text: "testing note", format: "html" } }] | ||
end | ||
|
||
before do | ||
allow(sidecar).to receive(:data).and_return('foo_tesim' => { data: data }) | ||
end | ||
it "normalizes the text area value" do | ||
expect(output).to eq(data: data) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe CustomFieldRendering do | ||
subject(:rendered) { custom_field_rendering.render } | ||
|
||
let(:document) { instance_double(SolrDocument) } | ||
let(:context) { double } | ||
let(:options) { double } | ||
let(:stack) { [Blacklight::Rendering::Terminator] } | ||
let(:custom_field_rendering) do | ||
described_class.new(values, field_config, document, context, options, stack) | ||
end | ||
|
||
describe "#render" do | ||
let(:values) do | ||
"{\"data\":[{\"type\":\"text\",\"data\":{\"text\":\"testing note\",\"format\":\"html\"}}]}" | ||
end | ||
let(:field_config) { Blacklight::Configuration::NullField.new(text_area: "1", separator_options: nil) } | ||
|
||
it "parses the JSON and extracts the text" do | ||
expect(rendered).to eq "testing note" | ||
end | ||
|
||
context "when the JSON is invalid" do | ||
let(:values) do | ||
"@#123" | ||
end | ||
|
||
it "renders the original string" do | ||
expect(rendered).to eq values | ||
end | ||
end | ||
end | ||
end |