Sha256: d557c6634033a4de6dfa153441ec1b4ae2b610f1c154acb925ff1dd13278f47e

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module ROM
  module Plugins
    module Relation
      module SQL
        module AutoCombine
          # @api private
          def self.included(klass)
            super
            klass.class_eval do
              include(InstanceInterface)
              extend(ClassInterface)
            end
          end

          module ClassInterface
            def inherited(klass)
              super
              klass.auto_curry :for_combine
              klass.auto_curry :preload
            end
          end

          module InstanceInterface
            # Default methods for fetching combined relation
            #
            # This method is used by default by `combine`
            #
            # @return [SQL::Relation]
            #
            # @api private
            def for_combine(spec)
              source_key, target_key, target =
                case spec
                when ROM::SQL::Association
                  [*spec.join_keys(__registry__).flatten, spec.call(__registry__, self)]
                else
                  [*spec.flatten, self]
                end

              target.preload(source_key, target_key)
            end

            # @api private
            def preload(source_key, target_key, source)
              where(target_key => source.pluck(source_key.to_sym))
            end
          end
        end
      end
    end
  end
end

ROM.plugins do
  adapter :sql do
    register :auto_combine, ROM::Plugins::Relation::SQL::AutoCombine, type: :relation
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-1.0.0.beta3 lib/rom/plugins/relation/sql/auto_combine.rb