Skip to content

Commit

Permalink
Merge pull request #1821 from uclibs/bug/#1613-note-field-line-breaks
Browse files Browse the repository at this point in the history
Add new attribute renderer for line breaks
  • Loading branch information
crowesn authored Feb 16, 2018
2 parents de7a6a6 + 409041b commit 78fceaa
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ DEPENDENCIES
devise
devise-guests (~> 0.5)
devise-multi_auth!
equivalent-xml
factory_bot_rails
fcrepo_wrapper
hydra-remote_identifier!
Expand Down
19 changes: 19 additions & 0 deletions app/renderers/hyrax/renderers/note_renderer.rb
Original file line number Diff line number Diff line change
@@ -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?
"<div#{html_attributes(microdata_value_attributes(field))}>#{li_value(value)}</div>"
else
li_value(value)
end
end

def li_value(value)
auto_link(ERB::Util.h(simple_format(value)))
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/hyrax/base/_attribute_rows.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
Expand Down
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/renderers/hyrax/renderers/note_renderer_spec.rb
Original file line number Diff line number Diff line change
@@ -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
"<tr><th>Note</th>\n" \
"<td><ul class='tabular'><li class=\"attribute note\"><p>Line 1\n" \
"<br>Line 2</p>\n\n" \
"<p>Line 4</p></li></ul></td></tr>\n"
end

it { expect(subject).to be_equivalent_to(expected) }
end
end

0 comments on commit 78fceaa

Please sign in to comment.