You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
48 lines
1.1 KiB
5 years ago
|
class CreateDiffRecordService < ApplicationService
|
||
|
attr_reader :user, :obj, :column_name, :after, :before
|
||
|
|
||
|
def initialize(user, obj, column_name, before, after)
|
||
|
@user = user
|
||
|
@obj = obj
|
||
|
@before = before
|
||
|
@after = after
|
||
|
@column_name = column_name
|
||
|
end
|
||
|
|
||
|
def call
|
||
|
ActiveRecord::Base.transaction do
|
||
|
diff_record = DiffRecord.create!(user: user, container: obj, column_name: column_name)
|
||
|
diff_record.create_diff_record_content!(content: diff_content)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def diff_content
|
||
|
content = ''
|
||
|
|
||
|
arr = []
|
||
|
index = 0
|
||
|
fragment_size = 1
|
||
|
Diffy::Diff.new(before, after).each do |line|
|
||
|
unless line.include?("\\ 文件尾没有 newline 字符")
|
||
|
unless line =~ /^[\+-]/
|
||
|
if arr.empty? && index < fragment_size
|
||
|
content += line
|
||
|
index += 1
|
||
|
else
|
||
|
index = 0
|
||
|
arr << line
|
||
|
arr.shift if arr.size > fragment_size
|
||
|
end
|
||
|
next
|
||
|
end
|
||
|
|
||
|
content += arr.join('') if arr.present?
|
||
|
content += line
|
||
|
arr.clear
|
||
|
end
|
||
|
end
|
||
|
content
|
||
|
end
|
||
|
end
|