Sha256: 909aef30d80f78f5081314a9a0ad9cbfacf418c64ca99d42fc3fc79f730c1284

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

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

          # @api private
          module ClassInterface
            # @api private
            def inherited(klass)
              super
              klass.auto_curry :for_combine
              klass.auto_curry :preload
            end
          end

          # @api private
          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)
              target_pks = source.pluck(source_key.to_sym).uniq
              target_pks.uniq!

              where(target_key => target_pks)
            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

4 entries across 4 versions & 1 rubygems

Version Path
rom-sql-1.0.2 lib/rom/plugins/relation/sql/auto_combine.rb
rom-sql-1.0.1 lib/rom/plugins/relation/sql/auto_combine.rb
rom-sql-1.0.0 lib/rom/plugins/relation/sql/auto_combine.rb
rom-sql-1.0.0.rc2 lib/rom/plugins/relation/sql/auto_combine.rb