Sha256: 2baa8b1c2707872bb3419dd420edb50cd7517c6bbb7dd6908449979761f674af

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module CableReady
  module Updatable
    class CollectionsRegistry
      def initialize
        @registered_collections = []
      end

      def register(collection)
        @registered_collections << collection
      end

      def broadcast_for!(model, operation)
        @registered_collections.select { |c| c[:options][:on].include?(operation) }
          .each do |collection|
          resource = find_resource_for_update(collection, model)
          next if resource.nil?

          collection[:klass].cable_ready_update_collection(resource, collection[:name], model) if collection[:options][:if].call(resource)
        end
      end

      private

      def find_resource_for_update(collection, model)
        raise ArgumentError, "Could not find inverse_of for #{collection[:name]}" unless collection[:inverse_association]

        resource = model
        resource = resource.send(collection[:through_association].underscore) if collection[:through_association]
        resource.send(collection[:inverse_association].underscore)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cable_ready-5.0.0.pre10 app/models/concerns/cable_ready/updatable/collections_registry.rb