Skip to content

Commit

Permalink
Create a base Renderers module and namespace existing renderers iwthi…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 142 deletions.
2 changes: 1 addition & 1 deletion app/presenters/curation_concerns/presents_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def attribute_to_html(field, options = {})
Rails.logger.warn("#{self.class} attempted to render #{field}, but no method exists with that name.")
return
end
AttributeRenderer.new(field, send(field), options).render
CurationConcerns::Renderers::AttributeRenderer.new(field, send(field), options).render
end

def permission_badge
Expand Down
98 changes: 0 additions & 98 deletions app/renderers/curation_concerns/attribute_renderer.rb

This file was deleted.

40 changes: 0 additions & 40 deletions app/renderers/curation_concerns/configured_microdata.rb

This file was deleted.

100 changes: 100 additions & 0 deletions app/renderers/curation_concerns/renderers/attribute_renderer.rb
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 app/renderers/curation_concerns/renderers/configured_microdata.rb
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
11 changes: 11 additions & 0 deletions app/renderers/renderers.rb
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

context 'with an existing field' do
before do
allow(CurationConcerns::AttributeRenderer).to receive(:new)
allow(CurationConcerns::Renderers::AttributeRenderer).to receive(:new)
.with(:title, "foo bar", {})
.and_return(renderer)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'

describe CurationConcerns::AttributeRenderer do
describe CurationConcerns::Renderers::AttributeRenderer do
let(:field) { :name }
let(:renderer) { described_class.new(field, ['Bob', 'Jessica']) }
let(:yml_path) { File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'locales', '*.{rb,yml}') }
let(:yml_path) { File.join(File.dirname(__FILE__), '..', '..', '..', 'fixtures', 'locales', '*.{rb,yml}') }
before do
I18n.load_path += Dir[File.join(yml_path)]
I18n.reload!
Expand Down

0 comments on commit 4709bc6

Please sign in to comment.