Sha256: e1c70af85d7759d713a726ca4909ab7bd77b4f10a976928614a64432f26b5a84

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'rom/associations/many_to_one'
require 'rom/sql/associations/core'
require 'rom/sql/associations/self_ref'

module ROM
  module SQL
    module Associations
      class ManyToOne < ROM::Associations::ManyToOne
        include Associations::Core
        include Associations::SelfRef

        # @api public
        #
        # rubocop:disable Metrics/AbcSize
        def call(target: self.target, preload: false)
          if preload
            schema = target.schema.qualified
            relation = target
          else
            right = source

            target_pk = target.schema.primary_key_name
            right_fk = target.foreign_key(source.name)

            target_schema = target.schema
            right_schema = right.schema.project_pk

            schema =
              if target.schema.key?(right_fk)
                target_schema
              else
                target_schema.merge(right_schema.project_fk(target_pk => right_fk))
              end.qualified

            relation = target.join(source_table, join_keys)
          end

          if view
            apply_view(schema, relation)
          else
            schema.(relation)
          end
        end
        # rubocop:enable Metrics/AbcSize

        # @api public
        def join(type, source = self.source, target = self.target)
          source.__send__(type, target.name.dataset, join_keys).qualified
        end

        # @api private
        def prepare(target)
          call(target: target, preload: true)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-3.7.0 lib/rom/sql/associations/many_to_one.rb