Sha256: 20c48a4494625267cf14142a5b121679797ac7812be52075b5d867ce59da7ef4

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

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]) 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

4 entries across 4 versions & 1 rubygems

Version Path
cable_ready-5.0.0.pre7 app/models/concerns/cable_ready/updatable/collections_registry.rb
cable_ready-5.0.0.pre6 app/models/concerns/cable_ready/updatable/collections_registry.rb
cable_ready-5.0.0.pre5 app/models/concerns/cable_ready/updatable/collections_registry.rb
cable_ready-5.0.0.pre4 app/models/concerns/cable_ready/updatable/collections_registry.rb