commentaries: trim whitespace from fields.

This commit is contained in:
evazion
2017-06-13 14:28:52 -05:00
parent 1aafdc3928
commit f8afabd51c
2 changed files with 31 additions and 0 deletions

View File

@@ -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

View File

@@ -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