Skip to content

Commit

Permalink
Change file name and use module to organize
Browse files Browse the repository at this point in the history
  • Loading branch information
mccalluc committed Dec 20, 2022
1 parent 138f5af commit 033274f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 53 deletions.
42 changes: 42 additions & 0 deletions app/lib/diff_tools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "diff/lcs"

module DiffTools
class SimpleDiff
def initialize(old_value, new_value)
old_value ||= ""
new_value ||= ""
@changes = ::Diff::LCS.sdiff(old_value, new_value).chunk(&:action).map do |action, changes|
{
action: action,
old: changes.map(&:old_element).join,
new: changes.map(&:new_element).join
}
end
end

def to_html
@changes.map do |chunk|
old_html = DiffTools.value_to_html(chunk[:old])
new_html = DiffTools.value_to_html(chunk[:new])
if chunk[:action] == "="
new_html
else
(old_html.empty? ? "" : "<del>#{old_html}</del>") + \
(new_html.empty? ? "" : "<ins>#{new_html}</ins>")
end
end.join
end
end

def self.value_to_html(value)
too_many_words_re = /
((?:\S+\s+){3}) # group 1: Three words, including trailing space
(.+) # group 2: Drop this
((?:\s+\S+){3}) # group 3: Three words, including leading space
/x
ellipsis = value.gsub(too_many_words_re, '\1...\3')
CGI.escapeHTML(ellipsis)
end
end
40 changes: 0 additions & 40 deletions app/lib/simple_diff.rb

This file was deleted.

4 changes: 2 additions & 2 deletions app/models/work_activity.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "../lib/simple_diff"
require_relative "../lib/diff_tools"

# rubocop:disable Metrics/ClassLength
class WorkActivity < ApplicationRecord
Expand Down Expand Up @@ -199,7 +199,7 @@ def created_at_html

def change_value_html(value)
if value["action"] == "changed"
SimpleDiff.new(value["from"], value["to"]).to_html
DiffTools::SimpleDiff.new(value["from"], value["to"]).to_html
else
"old change"
end
Expand Down
22 changes: 11 additions & 11 deletions spec/lib/diff_spec.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# frozen_string_literal: true

require_relative "../../app/lib/simple_diff"
require_relative "../../app/lib/diff_tools"

RSpec.describe SimpleDiff do
RSpec.describe DiffTools::SimpleDiff do
it "handles complete change" do
expect(SimpleDiff.new("cat", "dog").to_html).to eq "<del>cat</del><ins>dog</ins>"
expect(DiffTools::SimpleDiff.new("cat", "dog").to_html).to eq "<del>cat</del><ins>dog</ins>"
end
it "handles single character change" do
expect(SimpleDiff.new("cat", "bat").to_html).to eq "<del>c</del><ins>b</ins>at"
expect(DiffTools::SimpleDiff.new("cat", "bat").to_html).to eq "<del>c</del><ins>b</ins>at"
end
it "handles nil old" do
expect(SimpleDiff.new(nil, "dog").to_html).to eq "<ins>dog</ins>"
expect(DiffTools::SimpleDiff.new(nil, "dog").to_html).to eq "<ins>dog</ins>"
end
it "handles nil new" do
expect(SimpleDiff.new("dog", nil).to_html).to eq "<del>dog</del>"
expect(DiffTools::SimpleDiff.new("dog", nil).to_html).to eq "<del>dog</del>"
end
it "handles addition" do
expect(SimpleDiff.new("dog", "In the dog house").to_html).to eq "<ins>In the </ins>dog<ins> house</ins>"
expect(DiffTools::SimpleDiff.new("dog", "In the dog house").to_html).to eq "<ins>In the </ins>dog<ins> house</ins>"
end
it "handles deletion" do
expect(SimpleDiff.new("The cow jumped", "cow").to_html).to eq "<del>The </del>cow<del> jumped</del>"
expect(DiffTools::SimpleDiff.new("The cow jumped", "cow").to_html).to eq "<del>The </del>cow<del> jumped</del>"
end
it "handles mixed" do
expect(SimpleDiff.new("quick brown", "brown fox").to_html).to eq "<del>quick </del>brown<ins> fox</ins>"
expect(DiffTools::SimpleDiff.new("quick brown", "brown fox").to_html).to eq "<del>quick </del>brown<ins> fox</ins>"
end
it "encodes html" do
expect(SimpleDiff.new("1 < 2", "2 > 1").to_html).to eq "<del>1</del><ins>2</ins> <del>&lt;</del><ins>&gt;</ins> <del>2</del><ins>1</ins>"
expect(DiffTools::SimpleDiff.new("1 < 2", "2 > 1").to_html).to eq "<del>1</del><ins>2</ins> <del>&lt;</del><ins>&gt;</ins> <del>2</del><ins>1</ins>"
end
it "abbreviates really long strings" do
expect(SimpleDiff.new(
expect(DiffTools::SimpleDiff.new(
"This does not repeat the entire string if its just a tiny typo in the middle that changes.",
"This does not repeat the entire string if it's just a tiny typo in the middle that changes."
).to_html).to eq "This does not ... string if it<ins>&#39;</ins>s just a ... middle that changes."
Expand Down

0 comments on commit 033274f

Please sign in to comment.