Skip to content
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
58 changes: 53 additions & 5 deletions app/controllers/concerns/hyrax/flexible_catalog_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def load_flexible_schema
properties_hash = current_profile['properties']
properties_hash.each do |itemprop, prop|
label = display_label_for(itemprop, prop)

view_options = prop['view']
indexing = prop['indexing']
next if indexing.nil?

Expand All @@ -31,14 +33,35 @@ def load_flexible_schema
name = blacklight_config.index_fields.keys.detect { |key| key.start_with?(itemprop) }
name ||= "#{itemprop}_tesim"

# for properties that DO exist in the CatalogController
if blacklight_config.index_fields[name].present?
if label
blacklight_config.index_fields[name].label = I18n.t(label, default: label)
blacklight_config.index_fields[name].custom_label = true
end
blacklight_config.index_fields[name].itemprop = itemprop

if require_view_helper_method?(view_options)
# add or update the helper method so linked fields will render correctly in the index view
blacklight_config.index_fields[name].helper_method = view_option_for_helper_method(view_options)
# the helper method for index_field_link needs the field name
blacklight_config.index_fields[name].field_name = itemprop
end
else
# for properties that DO NOT exist in the catalog controller
if require_view_helper_method?(view_options)
# add the view helper method to the arguments hash when creating a property
index_args[:helper_method] = view_option_for_helper_method(view_options)
# the helper method for index_field_link needs the field name
index_args[:field_name] = itemprop
end
# if a property in the metadata profile doesn't exist in the CatalogController, add it
blacklight_config.add_index_field(name, index_args)

# all index fields get this property so an admin can hide a property from the catalog search results
# by adding the name of the property via admin dashboard > Settings > Accounts > Hidden index fields
# NOTE: it is likely this will be handled by the metadata profile in the future
blacklight_config.index_fields[name].if = :render_optionally?
end

qf = blacklight_config.search_fields['all_fields'].solr_parameters[:qf]
Expand All @@ -49,20 +72,40 @@ def load_flexible_schema

if facetable?(indexing, itemprop)
name = "#{itemprop}_sim"
facet_args = { label: }
if indexing.include?("admin_only")
facet_args[:if] = lambda { |context, _field_config, _document| context.try(:current_user)&.admin? }
end

unless blacklight_config.facet_fields[name].present?
facet_args = { label: }
if indexing.include?("admin_only")
facet_args[:if] = lambda { |context, _field_config, _document| context.try(:current_user)&.admin? }
end
blacklight_config.add_facet_field(name, **facet_args)
end
else
# if the property does not have facetable in the indexing section of the metadata profile, remove the facet field from the blacklight config
name = "#{itemprop}_sim"
blacklight_config.facet_fields.delete(name)
end
end
end

private

# Returns true if the view options require a helper method to render the linked field correctly in the index view
# @param view_options [Hash] the view options ex: {"render_as"=>"linked", "html_dl"=>true}
# @return [Boolean] to determine if the view_option_for_helper_method should be called
def require_view_helper_method?(view_options)
view_options.present? && %w[external_link linked rights_statement].include?(view_options.dig('render_as'))
end

# Returns the helper method that will render the linked field correctly in the index view
# @param view_options [Hash] the view options ex: {"render_as"=>"linked", "html_dl"=>true}
# @return [Symbol] helper method from Hyrax::HyraxHelperBehavior
def view_option_for_helper_method(view_options)
render_as = view_options.dig('render_as')
return :iconify_auto_link if render_as == 'external_link'
return :index_field_link if render_as == 'linked'
return :rights_statement_links if render_as == 'rights_statement'
end

def display_label_for(field_name, config)
display_label = config.fetch('display_label', {})&.with_indifferent_access || {}
display_label = { default: display_label } if display_label.is_a?(String)
Expand Down Expand Up @@ -101,5 +144,10 @@ def initialize
self.class.load_flexible_schema
super
end

# Hook to allow optional rendering at the app level
def render_optionally?
true
end
end
end
Loading
Loading