ファイル
btrc-hub/backend/app/services/version_recorder.rb
T

116 行
3.3 KiB
Ruby

class VersionRecorder
EVENT_TYPES = ['create', 'update', 'discard', 'restore'].freeze
def self.current_event
ActiveSupport::IsolatedExecutionState[:version_event]
end
def self.with_event event_type:, created_by_user:, reason: nil, event: nil
previous = current_event
ActiveSupport::IsolatedExecutionState[:version_event] =
previous || event || Event.new(event_type:, created_by_user:, reason:)
yield
ensure
ActiveSupport::IsolatedExecutionState[:version_event] = previous
end
def self.record_operation! event_type:, created_by_user:, reason: nil, savepoint_flg: false
with_event(event_type:, created_by_user:, reason:) do
ApplicationRecord.transaction(requires_new: savepoint_flg) { yield }
end
end
def initialize record:, event_type:, created_by_user:, event: nil
@record = record
@event_type = event_type.to_s
@event = event
@created_by_user = created_by_user
validate_event_type!
end
def record!
raise "#{ record_class.name } must be persisted" unless @record.persisted?
ApplicationRecord.transaction do
@record = record_class.unscoped.lock.find(@record.id)
latest = latest_version
validate_version_sequence!(latest)
attrs = snapshot_attributes
if @event_type == 'update' && latest && same_snapshot?(latest, attrs)
return latest
end
version = version_class.create!(
base_attributes(latest).merge(record_key => @record).merge(attrs))
update_record_version_no!(version.version_no)
version
end
end
private
def latest_version = versions.order(version_no: :desc).lock.first
def versions = @record.public_send(version_association)
def base_attributes latest
{ version_no: (latest&.version_no || 0) + 1,
event_type: @event_type,
event: @event || self.class.current_event || Event.new(
event_type: "#{ record_class.table_name }.#{ @event_type }",
created_by_user: @created_by_user),
created_by_user: @created_by_user,
created_at: Time.current }
end
def update_record_version_no! version_no
return unless tracks_version_no_on_record?
@record.update_columns(version_no:)
@record.version_no = version_no
end
def tracks_version_no_on_record? = true
def validate_version_sequence! latest
if !(latest) && @event_type != 'create'
raise "#{ version_class.name } first event must be create"
end
if @event_type == 'create' && latest
raise "#{ version_class.name } create event already exists"
end
return if !(latest) || !(tracks_version_no_on_record?)
if @record.version_no != latest.version_no
raise ("#{ record_class.name }##{ @record.id } version_no is #{ @record.version_no }, " +
"but latest #{ version_class.name } version_no is #{ latest.version_no }")
end
end
def same_snapshot? version, attrs
attrs.all? { |k, v| version.public_send(k) == v }
end
def validate_event_type!
return if event_types.include?(@event_type)
raise ArgumentError, "Invalid event_type: #{ @event_type }"
end
def version_class = raise NotImplementedError
def version_association = raise NotImplementedError
def record_key = raise NotImplementedError
def snapshot_attributes = raise NotImplementedError
def record_class = @record.class
def event_types = self.class::EVENT_TYPES
end