diff --git a/app/models/concerns/post_normalizations.rb b/app/models/concerns/post_normalizations.rb new file mode 100644 index 000000000..2533a1a4a --- /dev/null +++ b/app/models/concerns/post_normalizations.rb @@ -0,0 +1,9 @@ +module PostNormalizations + extend ActiveSupport::Concern + + class_methods do + def normalize_newlines(text) + text.encode(text.encoding, universal_newline: true).strip + end + end +end diff --git a/app/models/post.rb b/app/models/post.rb index 1d3012a9b..fa5bb9ba8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,6 +1,7 @@ class Post < ApplicationRecord include CommunityRelated include Lockable + include PostNormalizations include PostValidations include SoftDeletable include Timestamped diff --git a/app/models/post_history.rb b/app/models/post_history.rb index 2190ac642..5e6d25d33 100644 --- a/app/models/post_history.rb +++ b/app/models/post_history.rb @@ -1,4 +1,5 @@ class PostHistory < ApplicationRecord + include PostNormalizations include PostRelated include EditsValidations @@ -11,11 +12,7 @@ class PostHistory < ApplicationRecord scope :of_type, ->(name) { joins(:post_history_type).where(post_history_types: { name: name }) } scope :on_undeleted, -> { joins(:post).where(posts: { deleted: false }) } - normalize_newlines = lambda { |text| - text.encode(text.encoding, universal_newline: true) - } - normalizes :before_state, with: normalize_newlines - normalizes :after_state, with: normalize_newlines + normalizes :before_state, :after_state, with: ->(text) { normalize_newlines(text) } def before_tags tags.where(post_history_tags: { relationship: 'before' }) diff --git a/app/views/post_history/_diff.html.erb b/app/views/post_history/_diff.html.erb index 77eb63d7b..fa5b7944d 100644 --- a/app/views/post_history/_diff.html.erb +++ b/app/views/post_history/_diff.html.erb @@ -1,7 +1,9 @@