From 409041b9a62e81be6f99bae6ebc9717e12f54b01 Mon Sep 17 00:00:00 2001 From: Katie Date: Thu, 8 Feb 2018 15:43:22 -0500 Subject: [PATCH] Add new attribute renderer for line breaks --- Gemfile | 1 + Gemfile.lock | 3 ++- .../hyrax/renderers/note_renderer.rb | 19 +++++++++++++++++ app/views/hyrax/base/_attribute_rows.html.erb | 2 +- spec/rails_helper.rb | 2 ++ .../hyrax/renderers/note_renderer_spec.rb | 21 +++++++++++++++++++ 6 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 app/renderers/hyrax/renderers/note_renderer.rb create mode 100644 spec/renderers/hyrax/renderers/note_renderer_spec.rb diff --git a/Gemfile b/Gemfile index 74116d64d..69efec446 100644 --- a/Gemfile +++ b/Gemfile @@ -68,6 +68,7 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'rb-readline' gem 'byebug' + gem 'equivalent-xml' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 62d051760..ef510f983 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -942,6 +942,7 @@ DEPENDENCIES devise devise-guests (~> 0.5) devise-multi_auth! + equivalent-xml factory_bot_rails fcrepo_wrapper hydra-remote_identifier! @@ -983,4 +984,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.16.0 + 1.16.1 diff --git a/app/renderers/hyrax/renderers/note_renderer.rb b/app/renderers/hyrax/renderers/note_renderer.rb new file mode 100644 index 000000000..3d97c1c3b --- /dev/null +++ b/app/renderers/hyrax/renderers/note_renderer.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Hyrax + module Renderers + class NoteRenderer < AttributeRenderer + def attribute_value_to_html(value) + if microdata_value_attributes(field).present? + "#{li_value(value)}" + else + li_value(value) + end + end + + def li_value(value) + auto_link(ERB::Util.h(simple_format(value))) + end + end + end +end diff --git a/app/views/hyrax/base/_attribute_rows.html.erb b/app/views/hyrax/base/_attribute_rows.html.erb index b2284d0e3..5a4c0139d 100644 --- a/app/views/hyrax/base/_attribute_rows.html.erb +++ b/app/views/hyrax/base/_attribute_rows.html.erb @@ -14,7 +14,7 @@ <%= presenter.attribute_to_html(:issn) %> <%= presenter.attribute_to_html(:time_period) %> <%= presenter.attribute_to_html(:required_software) %> -<%= presenter.attribute_to_html(:note) %> +<%= presenter.attribute_to_html(:note, render_as: :note) %> <%= presenter.attribute_to_html(:genre) %> <%= presenter.attribute_to_html(:geo_subject, render_as: :faceted, label: "Geographic Subject") %> <%= presenter.attribute_to_html(:degree) %> diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index ed3a76237..0ee650852 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -53,6 +53,8 @@ ActiveRecord::Migration.maintain_test_schema! require 'shoulda/matchers' +require 'equivalent-xml' +require 'equivalent-xml/rspec_matchers' Shoulda::Matchers.configure do |config| config.integrate do |with| with.test_framework :rspec diff --git a/spec/renderers/hyrax/renderers/note_renderer_spec.rb b/spec/renderers/hyrax/renderers/note_renderer_spec.rb new file mode 100644 index 000000000..77f6a3a09 --- /dev/null +++ b/spec/renderers/hyrax/renderers/note_renderer_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Hyrax::Renderers::NoteRenderer do + let(:field) { :note } + let(:renderer) { described_class.new(field, "Line 1\nLine 2\n\nLine 4") } + + describe '#attribute_to_html' do + let(:subject) { Nokogiri::HTML(renderer.render) } + let(:expected) { Nokogiri::HTML(tr_content) } + + let(:tr_content) do + "Note\n" \ + "\n" + end + + it { expect(subject).to be_equivalent_to(expected) } + end +end