Sha256: 68dcf9ae552a6651be2093af2a64efd9c307808357ae4a1eb32356f4a3e2a8b4

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 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__)]
                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

5 entries across 5 versions & 1 rubygems

Version Path
rom-sql-1.0.0.beta2 lib/rom/plugins/relation/sql/auto_combine.rb
rom-sql-1.0.0.beta1 lib/rom/plugins/relation/sql/auto_combine.rb
rom-sql-0.9.1 lib/rom/plugins/relation/sql/auto_combine.rb
rom-sql-0.9.0 lib/rom/plugins/relation/sql/auto_combine.rb
rom-sql-0.8.0 lib/rom/plugins/relation/sql/auto_combine.rb