Sha256: 311c1df1a47e9356747887b74d0e0594f068634447267dcdf5ce62ee6472bd28

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Logux
  module Model
    class Updater
      def initialize(model:, attributes:, logux_order: Logux.generate_action_id)
        @model = model
        @logux_order = logux_order
        @attributes = attributes
      end

      def updated_attributes
        newer_updates.merge(logux_fields_updated_at: fields_updated_at)
      end

      private

      def fields_updated_at
        @fields_updated_at ||=
          newer_updates.slice(*tracked_fields)
                       .keys
                       .reduce(@model.logux_fields_updated_at) do |acc, attr|
                         acc.merge(attr => @logux_order)
                       end
      end

      def newer_updates
        @newer_updates ||= @attributes.reject do |attr, _|
          field_updated_at = @model.logux.updated_at(attr)
          field_updated_at && field_updated_at > @logux_order
        end
      end

      def tracked_fields
        @model.class.logux_crdt_mapped_attributes
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
logux_rails-0.2.0 lib/logux/model/updater.rb
logux_rails-0.1.0 lib/logux/model/updater.rb