Skip to content

Commit

Permalink
Use the extracted normalizer throughout the codebase
Browse files Browse the repository at this point in the history
All controllers should normalize with the same normalizer behavior.

Connected to #362.
  • Loading branch information
Tom Johnson committed Apr 4, 2018
1 parent 8cb1567 commit 9716caa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 30 deletions.
22 changes: 5 additions & 17 deletions app/controllers/contribute_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ContributeController < ApplicationController
include GisHelper
include Tufts::Normalizer

before_action :load_deposit_type, only: [:new, :create]

Expand Down Expand Up @@ -31,25 +32,12 @@ def create
end
end

# Go through the params hash and normalize whitespace before handing it off to
# object creation
# @param [ActionController::Parameters] params
def normalize_whitespace(params)
# For keep_newline_fields, keep single newlines, compress 2+ newlines to 2,
# and otherwise strip whitespace as usual
keep_newline_fields = ['description', 'abstract']
params["contribution"].keys.each do |key|
next unless params["contribution"][key].class == String
params["contribution"][key] = if keep_newline_fields.include?(key)
params["contribution"][key].gsub(/[ \t]+?[\n]{2,}[ \t]+?/, "\n\n").gsub(/[ \t]+/, " ").strip
else
params["contribution"][key].gsub(/\s+/, " ").strip
end
end
end

protected

def hash_key_for_curation_concern
'contribution'
end

def load_deposit_type
@deposit_type = DepositType.where(id: params[:deposit_type]).first
# Redirect the user to the selection page if the deposit type is invalid or missing
Expand Down
5 changes: 2 additions & 3 deletions app/lib/tufts/normalizer.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module Tufts
module Normalizer
def strip_whitespace(param_value)
return nil if param_value.empty?
return nil if param_value.respond_to?(:empty?) && param_value.empty?

normalizer.strip_whitespace(param_value)
end

def strip_whitespace_keep_newlines(param_value)
return nil if param_value.empty?

return nil if param_value.respond_to?(:empty?) && param_value.empty?
normalizer.strip_whitespace(param_value, keep_newlines: true)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/hyrax/audios_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"accrual_policy" => "",
"creator" => " Name with Spaces ",
"bibliographic_citation" => [" bibliographic citation with spaces "],
"rights_holder" => ["blah spaces "],
"rights_holder" => ["blah \nspaces "],
"publisher" => ["Publisher "],
"description" => [" A short description \n with wonky spaces.\r\n\r\n\r\nBut keep two newlines between paragraphs. "],
"abstract" => [" A short description \n with wonky spaces.\r\n\r\n\r\nBut keep two newlines between paragraphs. "]
Expand Down
16 changes: 8 additions & 8 deletions spec/features/create_generic_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
expect(page).to have_no_content('Location')
# Fill out the form with everything
within('.generic_object_title') do
fill_in "Title", with: "Example \nTitle "
fill_in "Title", with: "Title \n with \t condensed spaces "
end
find(:xpath, '//option[contains(text(), "nowhere")]').select_option
fill_in 'Abstract', with: 'Abstract'
fill_in 'Abstract', with: " A short description with \t wonky spaces "
fill_in 'Accrual Policy', with: 'Accrual Policy'
fill_in 'Alternate Title', with: 'Alternate Title'
fill_in 'Audience', with: 'Audience'
fill_in 'Bibliographic Citation', with: 'Bibliographic Citation'
fill_in 'Bibliographic Citation', with: " bibliographic citation \n with spaces "
fill_in 'Contributor', with: 'Contributor'
fill_in 'Corporate Name', with: 'Corporate Name'
fill_in 'Created By', with: 'Created By'
fill_in 'Creator', with: 'Creator'
fill_in 'Creator', with: ' Name with Spaces '
fill_in 'Creator Department', with: 'Creator Department'
fill_in 'Date Accepted', with: 'Date Accepted'
fill_in 'Date Available', with: 'Date Available'
Expand Down Expand Up @@ -92,17 +92,17 @@
sleep(1)
find('#with_files_submit').click

expect(page).to have_content 'Example Title'
expect(page).to have_content 'Title with condensed spaces'
expect(page).to have_content 'nowhere'
expect(page).to have_content 'Abstract'
expect(page).to have_content 'A short description with wonky spaces'
expect(page).to have_content 'Accrual Policy'
expect(page).to have_content 'Alternate Title'
expect(page).to have_content 'Audience'
expect(page).to have_content 'Bibliographic Citation'
expect(page).to have_content 'bibliographic citation with spaces'
expect(page).to have_content 'Contributor'
expect(page).to have_content 'Corporate Name'
expect(page).to have_content 'Created By'
expect(page).to have_content 'Creator'
expect(page).to have_content 'Name with Spaces'
expect(page).to have_content 'Creator Department'
expect(page).to have_content 'Date Accepted'
expect(page).to have_content 'Date Available'
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/tufts/whitespace_normalizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

context 'when keeping newlines' do
it 'collapses whitespace into a single whitespace keeping \n' do
string = "moomin \n\r\n moomin \n\t\r"
string = "moomin \n\n\r\n \t moomin \n\t\r"

expect(normalizer.strip_whitespace(string, keep_newlines: true))
.to eq "moomin \n\n moomin"
Expand Down

0 comments on commit 9716caa

Please sign in to comment.