Skip to content

Commit

Permalink
Use the presenter to handle document fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 5, 2022
1 parent 657ff47 commit de4ac34
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 163 deletions.
58 changes: 0 additions & 58 deletions app/helpers/arclight_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,64 +203,6 @@ def ead_files(document)
end
end

##
# Defines custom helpers used for creating unique metadata blocks to render
Arclight::Engine.config.catalog_controller_field_accessors.each do |config_field|
##
# Mimics what document_show_fields from Blacklight does
# https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/configuration_helper_behavior.rb#L35-L38
define_method(:"document_#{config_field}s") do |_document = nil|
blacklight_config.send(:"#{config_field}s")
end

##
# Mimics what render_document_show_field_label from Blacklight does
# https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/blacklight_helper_behavior.rb#L136-L156
define_method(:"render_document_#{config_field}_label") do |*args|
options = args.extract_options!
document = args.first

field = options[:field]

t(:'blacklight.search.show.label', label: send(:"document_#{config_field}_label", document, field))
end

##
# Mimics what document_show_field_label from Blacklight does
# https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/configuration_helper_behavior.rb#L67-L74
define_method(:"document_#{config_field}_label") do |document, field|
field_config = send(:"document_#{config_field}s", document)[field]
field_config ||= Blacklight::Configuration::NullField.new(key: field)

field_config.display_label('show')
end

##
# Mimics what should_render_show_field? from Blacklight does
# https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/blacklight_helper_behavior.rb#L84-L92
define_method(:"should_render_#{config_field}?") do |document, field_config|
should_render_field?(field_config, document) && document_has_value?(document, field_config)
end
end

##
# Calls the method for a configured field
def generic_document_fields(config_field)
send(:"document_#{config_field}s")
end

##
# Calls the method for a configured field
def generic_should_render_field?(config_field, document, field)
send(:"should_render_#{config_field}?", document, field)
end

##
# Calls the method for a configured field
def generic_render_document_field_label(config_field, document, field: field_name)
send(:"render_document_#{config_field}_label", document, field: field)
end

def show_expanded?(document)
!original_document?(document) && within_original_tree?(document)
end
Expand Down
38 changes: 23 additions & 15 deletions app/presenters/arclight/show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,40 @@ def heading
end

def with_field_group(group)
self.field_group = group
self
if block_given?
old_group = self.field_group

begin
self.field_group = group
yield
ensure
self.field_group = old_group if block_given?
end
else
self.field_group = group
self
end
end

def fields_have_content?(field_accessor)
generic_document_fields(field_accessor).any? do |_, field|
generic_should_render_field?(field_accessor, field)
with_field_group(field_accessor) do
fields_to_render.any?
end
end

##
# Calls the method for a configured field
def generic_should_render_field?(config_field, field)
view_context.public_send(:"should_render_#{config_field}?", document, field)
end

private

##
# Calls the method for a configured field
def generic_document_fields(config_field)
view_context.public_send(:"document_#{config_field}s")
# @return [Hash<String,Configuration::Field>] all the fields for this index view
def fields
if field_group
configuration["#{field_group}s"] || []
else
super
end
end

def field_group
@field_group || 'show_field'
@field_group
end

attr_writer :field_group
Expand Down
13 changes: 4 additions & 9 deletions app/views/catalog/_access_contents.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
<% doc_presenter = document_presenter(document).with_field_group(field_accessor) %>
<div class="container">
<dl class="row dl-invert">
<% generic_document_fields(field_accessor).each do |field_name, field| %>
<% if generic_should_render_field?(field_accessor, document, field) %>
<dt class="blacklight-<%= field_name.parameterize %> col-md-2">
<%= generic_render_document_field_label(field_accessor, document, field: field_name).upcase %>
</dt>
<dd class="blacklight-<%= field_name.parameterize %> col-md-10">
<%= doc_presenter.field_value field %>
</dd>
<% end %>
<%# TODO: use col-md-2 / col-md-10 %>
<%# TODO: upcase labels? %>
<% doc_presenter.field_presenters.each do |field_presenter| %>
<%= render (field_presenter.component || Blacklight::MetadataFieldComponent).new(field: field_presenter, show: true) %>
<% end %>
</dl>
</div>
12 changes: 3 additions & 9 deletions app/views/catalog/_context_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@
<div id="collapse<%= card_index %>" class="collapse <%= 'show' if card_index.zero? %>" role="tabpanel" aria-labelledby="heading<%= card_index %>">
<div class="card-body">
<dl>
<% generic_document_fields(field_accessor).each do |field_name, field| %>
<% if generic_should_render_field?(field_accessor, document, field) %>
<dt class="blacklight-<%= field_name.parameterize %>">
<%= generic_render_document_field_label(field_accessor, document, field: field_name) %>
</dt>
<dd class="blacklight-<%= field_name.parameterize %>">
<%= doc_presenter.field_value field %>
</dd>
<% end %>
<%# TODO: No col-* css %>
<% doc_presenter.field_presenters.each do |field_presenter| %>
<%= render (field_presenter.component || Blacklight::MetadataFieldComponent).new(field: field_presenter, show: true) %>
<% end %>
</dl>
</div>
Expand Down
7 changes: 2 additions & 5 deletions app/views/catalog/_custom_metadata.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
</h3>

<dl class="row dl-invert">
<% generic_document_fields(field_accessor).each do |field_name, field| %>
<% if generic_should_render_field?(field_accessor, document, field) %>
<dt class="blacklight-<%= field_name.parameterize %> col-md-3"><%= generic_render_document_field_label(field_accessor, document, field: field_name) %></dt>
<dd class="blacklight-<%= field_name.parameterize %> col-md-9"><%= doc_presenter.field_value field %></dd>
<% end %>
<% doc_presenter.field_presenters.each do |field_presenter| %>
<%= render (field_presenter.component || Blacklight::MetadataFieldComponent).new(field: field_presenter, show: true) %>
<% end %>
</dl>
<% end %>
8 changes: 3 additions & 5 deletions app/views/catalog/_show_upper_metadata_default.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
<dl class="al-metadata-section breadcrumb-item breadcrumb-item-<%= parents.length + 3 %>">
<% blacklight_config.show.component_metadata_partials.each do |metadata| %>
<% doc_presenter = document_presenter(document).with_field_group(metadata) %>
<% generic_document_fields(metadata).each do |field_name, field| %>
<% if generic_should_render_field?(metadata, document, field) %>
<dt class="blacklight-<%= field_name.parameterize %>"><%= generic_render_document_field_label(metadata, document, field: field_name) %></dt>
<dd class="blacklight-<%= field_name.parameterize %>"><%= doc_presenter.field_value field %></dd>
<% end %>
<%# TODO: remove col-* markup %>
<% doc_presenter.field_presenters.each do |field_presenter| %>
<%= render (field_presenter.component || Blacklight::MetadataFieldComponent).new(field: field_presenter, show: true) %>
<% end %>
<% end %>
</dl>
Expand Down
58 changes: 0 additions & 58 deletions spec/helpers/arclight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,64 +346,6 @@
end
end

describe 'custom field accessors' do
let(:accessors) { Arclight::Engine.config.catalog_controller_field_accessors }
let(:field) { :yolo }

describe '#document_config_fields' do
it do
accessors.each do |accessor|
expect(helper).to respond_to :"document_#{accessor}s"
end
end
end

describe '#render_document_config_field_label' do
it do
accessors.each do |accessor|
expect(helper).to respond_to :"render_document_#{accessor}_label"
end
end
end

describe '#document_config_field_label' do
it do
accessors.each do |accessor|
expect(helper).to respond_to :"document_#{accessor}_label"
end
end
end

describe '#should_render_config_field?' do
it do
accessors.each do |accessor|
expect(helper).to respond_to :"should_render_#{accessor}?"
end
end
end

describe '#generic_document_fields' do
it 'send along the method call' do
expect(helper).to receive_messages(document_yolos: nil)
helper.generic_document_fields(field)
end
end

describe '#generic_should_render_field?' do
it 'send along the method call' do
expect(helper).to receive_messages(should_render_yolo?: nil)
helper.generic_should_render_field?(field, 0, 1)
end
end

describe '#generic_render_document_field_label?' do
it 'send along the method call' do
expect(helper).to receive_messages(render_document_yolo_label: nil)
helper.generic_render_document_field_label(field, 0, field: 1)
end
end
end

describe '#hierarchy_component_context?' do
it 'requires a parameter to enable' do
allow(helper).to receive(:params).and_return(hierarchy_context: 'component')
Expand Down
8 changes: 4 additions & 4 deletions spec/presenters/arclight/show_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
end

describe '#with_field_group' do
it 'defaults to the show_field group when none is set' do
expect(presenter.send(:field_group)).to eq 'show_field'
it 'is nil when none is set' do
expect(presenter.send(:field_group)).to be_nil
end

it 'sets the field group based on the given field accessor (and returns the presenter)' do
Expand All @@ -46,9 +46,9 @@
describe '#fields_have_content?' do
before do
allow(view_context).to receive_messages(
should_render_field?: true,
document_background_fields: CatalogController.blacklight_config.background_fields
should_render_field?: true
)
config.add_background_field 'acqinfo_ssim', label: 'Acquisition Information'
end

context 'when the configured fields have content' do
Expand Down

0 comments on commit de4ac34

Please sign in to comment.