From fba4c79d2abf6c9a0f02d95e88441240ea46e6bc Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Thu, 5 Jan 2017 09:43:40 +0100 Subject: [PATCH] Show the first paragraph of the description --- app/helpers/text_helper.rb | 9 +++++++++ .../legislation/processes/_process.html.erb | 2 +- spec/helpers/text_helper_spec.rb | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 app/helpers/text_helper.rb create mode 100644 spec/helpers/text_helper_spec.rb diff --git a/app/helpers/text_helper.rb b/app/helpers/text_helper.rb new file mode 100644 index 000000000..dda3ba721 --- /dev/null +++ b/app/helpers/text_helper.rb @@ -0,0 +1,9 @@ +module TextHelper + def first_paragraph(text) + if text.blank? + "" + else + text.strip.split("\n").first + end + end +end diff --git a/app/views/legislation/processes/_process.html.erb b/app/views/legislation/processes/_process.html.erb index 9eac89a7b..a5780f792 100644 --- a/app/views/legislation/processes/_process.html.erb +++ b/app/views/legislation/processes/_process.html.erb @@ -3,7 +3,7 @@

<%= link_to process.title, process %>

-

<%= process.description %>

+ <%= simple_format(first_paragraph(process.description)) %>
diff --git a/spec/helpers/text_helper_spec.rb b/spec/helpers/text_helper_spec.rb new file mode 100644 index 000000000..cc9300b7f --- /dev/null +++ b/spec/helpers/text_helper_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe TextHelper do + + describe "#first_paragraph" do + it "should return the first paragraph of a text" do + text = "\n\nThis is the first paragraph\n\nThis is the second paragraph\n" + expect(first_paragraph(text)).to eq("This is the first paragraph") + end + + it "should return blank if the text is blank" do + expect(first_paragraph("")).to eq("") + expect(first_paragraph(nil)).to eq("") + end + end + +end