From f8afabd51ce4d07f8124811e0aa941aca4aa1731 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 13 Jun 2017 14:28:52 -0500 Subject: [PATCH] commentaries: trim whitespace from fields. --- app/models/artist_commentary.rb | 8 ++++++++ test/unit/artist_commentary_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/models/artist_commentary.rb b/app/models/artist_commentary.rb index 77df69f13..a68a70618 100644 --- a/app/models/artist_commentary.rb +++ b/app/models/artist_commentary.rb @@ -4,6 +4,7 @@ class ArtistCommentary < ActiveRecord::Base attr_accessor :remove_commentary_tag, :remove_commentary_request_tag, :remove_commentary_check_tag attr_accessor :add_commentary_tag, :add_commentary_request_tag, :add_commentary_check_tag attr_accessible :post_id, :original_description, :original_title, :translated_description, :translated_title, :remove_commentary_tag, :remove_commentary_request_tag, :add_commentary_tag, :add_commentary_request_tag, :add_commentary_check_tag, :remove_commentary_check_tag + before_validation :trim_whitespace validates_uniqueness_of :post_id belongs_to :post has_many :versions, lambda {order("artist_commentary_versions.id ASC")}, :class_name => "ArtistCommentaryVersion", :dependent => :destroy, :foreign_key => :post_id, :primary_key => :post_id @@ -55,6 +56,13 @@ class ArtistCommentary < ActiveRecord::Base extend SearchMethods + def trim_whitespace + self.original_title = original_title.gsub(/\A[[:space:]]+|[[:space:]]+\z/, "") + self.translated_title = translated_title.gsub(/\A[[:space:]]+|[[:space:]]+\z/, "") + self.original_description = original_description.gsub(/\A[[:space:]]+|[[:space:]]+\z/, "") + self.translated_description = translated_description.gsub(/\A[[:space:]]+|[[:space:]]+\z/, "") + end + def original_present? original_title.present? || original_description.present? end diff --git a/test/unit/artist_commentary_test.rb b/test/unit/artist_commentary_test.rb index 69a29a602..c7f83dbb0 100644 --- a/test/unit/artist_commentary_test.rb +++ b/test/unit/artist_commentary_test.rb @@ -52,5 +52,28 @@ class ArtistCommentaryTest < ActiveSupport::TestCase assert_equal("foo", @artcomm.versions.last.original_title) end end + + context "when updated" do + setup do + @post = FactoryGirl.create(:post) + @artcomm = FactoryGirl.create(:artist_commentary, post_id: @post.id) + @artcomm.reload + end + + should "trim whitespace from all fields" do + # \u00A0 - nonbreaking space. + @artcomm.update( + original_title: " foo\u00A0\t\n", + original_description: " foo\u00A0\t\n", + translated_title: " foo\u00A0\t\n", + translated_description: " foo\u00A0\n", + ) + + assert_equal("foo", @artcomm.original_title) + assert_equal("foo", @artcomm.original_description) + assert_equal("foo", @artcomm.translated_title) + assert_equal("foo", @artcomm.translated_description) + end + end end end