Sha256: 580a2957de5bc8b2fe256febc4b22f7b4cc7e65979c266f0d3b2a7233f389ab2

Contents?: true

Size: 777 Bytes

Versions: 1

Compression:

Stored size: 777 Bytes

Contents

# frozen_string_literal: true

module Esse::ActiveRecord
  module Callbacks
    class UpdateLazyAttribute < Callback
      attr_reader :attribute_name

      def initialize(attribute_name:, **kwargs, &block)
        @attribute_name = attribute_name
        super(**kwargs, &block)
      end

      def call(model)
        related_ids = Esse::ArrayUtils.wrap(block_result || model.id)
        return true if related_ids.empty?

        repo.update_documents_attribute(attribute_name, related_ids, options)

        true
      end
    end

    register_callback(:update_lazy_attribute, :create, UpdateLazyAttribute)
    register_callback(:update_lazy_attribute, :update, UpdateLazyAttribute)
    register_callback(:update_lazy_attribute, :destroy, UpdateLazyAttribute)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
esse-active_record-0.3.8 lib/esse/active_record/callbacks/update_lazy_attribute.rb