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 = '' Diffy::Diff.new(before, after).each do |line| next unless line =~ /^[\+-]/ content += line end content end end