-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a base Renderers module and namespace existing renderers iwthi…
…n it. Introduce documentation for the renderers noun. Refers to #766 namespace all renderers under CurationConcerns::Renderers align end properly move the spec tests into a renderers folder to match the same structure as the app/ dir
- Loading branch information
Carrick Rogers
committed
May 6, 2016
1 parent
3bb73e8
commit 4709bc6
Showing
8 changed files
with
157 additions
and
142 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
app/renderers/curation_concerns/renderers/attribute_renderer.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,100 @@ | ||
require "rails_autolink/helpers" | ||
|
||
module CurationConcerns | ||
module Renderers | ||
class AttributeRenderer | ||
include ActionView::Helpers::UrlHelper | ||
include ActionView::Helpers::TranslationHelper | ||
include ActionView::Helpers::TextHelper | ||
include ConfiguredMicrodata | ||
|
||
attr_reader :field, :values, :options | ||
|
||
# @param [Symbol] field | ||
# @param [Array] values | ||
# @param [Hash] options | ||
def initialize(field, values, options = {}) | ||
@field = field | ||
@values = values | ||
@options = options | ||
end | ||
|
||
# Draw the table row for the attribute | ||
def render | ||
markup = '' | ||
|
||
return markup if !values.present? && !options[:include_empty] | ||
markup << %(<tr><th>#{label}</th>\n<td><ul class='tabular'>) | ||
attributes = microdata_object_attributes(field).merge(class: "attribute #{field}") | ||
Array(values).each do |value| | ||
markup << "<li#{html_attributes(attributes)}>#{attribute_value_to_html(value.to_s)}</li>" | ||
end | ||
markup << %(</ul></td></tr>) | ||
markup.html_safe | ||
end | ||
|
||
# @return The human-readable label for this field. | ||
# @note This is a central location for determining the label of a field | ||
# name. Can be overridden if more complicated logic is needed. | ||
def label | ||
translate( | ||
:"blacklight.search.fields.show.#{field}", | ||
default: [:"blacklight.search.fields.#{field}", options.fetch(:label, field.to_s.humanize)]) | ||
end | ||
|
||
private | ||
|
||
def attribute_value_to_html(value) | ||
if field == :rights | ||
rights_attribute_to_html(value) | ||
elsif microdata_value_attributes(field).present? | ||
"<span#{html_attributes(microdata_value_attributes(field))}>#{li_value(value)}</span>" | ||
else | ||
li_value(value) | ||
end | ||
end | ||
|
||
def html_attributes(attributes) | ||
buffer = "" | ||
attributes.each do |k, v| | ||
buffer << " #{k}" | ||
buffer << %(="#{v}") unless v.blank? | ||
end | ||
buffer | ||
end | ||
|
||
def search_field | ||
options.fetch(:search_field, field) | ||
end | ||
|
||
def li_value(value) | ||
if options[:catalog_search_link] | ||
link_to(ERB::Util.h(value), search_path(value)) | ||
else | ||
auto_link(ERB::Util.h(value)) | ||
end | ||
end | ||
|
||
def search_path(value) | ||
Rails.application.routes.url_helpers.search_catalog_path( | ||
search_field: search_field, q: ERB::Util.h(value)) | ||
end | ||
|
||
## | ||
# Special treatment for license/rights. A URL from the Sufia gem's config/sufia.rb is stored in the descMetadata of the | ||
# curation_concern. If that URL is valid in form, then it is used as a link. If it is not valid, it is used as plain text. | ||
def rights_attribute_to_html(value) | ||
begin | ||
parsed_uri = URI.parse(value) | ||
rescue | ||
nil | ||
end | ||
if parsed_uri.nil? | ||
ERB::Util.h(value) | ||
else | ||
%(<a href=#{ERB::Util.h(value)} target="_blank">#{RightsService.label(value)}</a>) | ||
end | ||
end | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
app/renderers/curation_concerns/renderers/configured_microdata.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,42 @@ | ||
module CurationConcerns | ||
module Renderers | ||
module ConfiguredMicrodata | ||
def microdata?(field) | ||
return false unless CurationConcerns.config.display_microdata | ||
key = "curation_concerns.schema_org.#{field}.property" | ||
t(key, default: false) | ||
end | ||
|
||
def microdata_object?(field) | ||
return false unless CurationConcerns.config.display_microdata | ||
key = "curation_concerns.schema_org.#{field}.type" | ||
t(key, default: false) | ||
end | ||
|
||
def microdata_object_attributes(field) | ||
if microdata_object?(field) | ||
{ itemprop: microdata_property(field), itemscope: '', itemtype: microdata_type(field) } | ||
else | ||
{} | ||
end | ||
end | ||
|
||
def microdata_property(field) | ||
t("curation_concerns.schema_org.#{field}.property") | ||
end | ||
|
||
def microdata_type(field) | ||
t("curation_concerns.schema_org.#{field}.type") | ||
end | ||
|
||
def microdata_value_attributes(field) | ||
if microdata?(field) | ||
key = microdata_object?(field) ? :value : :property | ||
{ itemprop: t("curation_concerns.schema_org.#{field}.#{key}") } | ||
else | ||
{} | ||
end | ||
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,11 @@ | ||
module CurationConcerns | ||
# Module that will containing Renderers | ||
# @since 0.14.0 | ||
# Renderers are used to display Ruby objects to users. | ||
# Renderers take the Ruby object supplied by a | ||
# CurationConcerns::Presenter object Renderers are typically # used to respond to read requests from the controller. | ||
# Renderers may apply some level of HTML or may just supply | ||
# human readable values for object attributes. | ||
module Renderers | ||
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
4 changes: 2 additions & 2 deletions
4
...ation_concerns/attribute_renderer_spec.rb → ...erns/renderers/attribute_renderer_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