Sha256: 5d36763f7992ad5921a3c89bf81c343d7c366c9fab51a82f63a27b3f4db23eaf

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Logux
  module Model
    class UpdatesDeprecator
      EVENT = 'logux.insecure_update'

      class << self
        def watch(args = {}, &block)
          new(args).watch(&block)
        end
      end

      def initialize(level: :warn)
        @level = level
      end

      def watch(&block)
        callback = lambda(&method(:handle_insecure_update))
        ActiveSupport::Notifications.subscribed(callback, EVENT, &block)
      end

      private

      # rubocop:disable Naming/UncommunicativeMethodParamName
      def handle_insecure_update(_, _, _, _, args)
        model = args[:model]

        attributes = model.changed.map(&:to_sym) - [:logux_fields_updated_at]
        insecure_attributes =
          attributes & model.class.logux_crdt_mapped_attributes
        return if insecure_attributes.empty?

        notify_about_insecure_update(insecure_attributes)
      end
      # rubocop:enable Naming/UncommunicativeMethodParamName

      def notify_about_insecure_update(insecure_attributes)
        pluralized_attributes = 'attribute'.pluralize(insecure_attributes.count)

        message = <<~TEXT
          Logux tracked #{pluralized_attributes} (#{insecure_attributes.join(', ')}) should be updated using model.logux.update(...)
        TEXT

        case @level
        when :warn
          ActiveSupport::Deprecation.warn(message)
        when :error
          raise InsecureUpdateError, message
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logux_rails-0.1.0 lib/logux/model/updates_deprecator.rb