Sha256: 7b0b5ffcc6e450079e0c4ae15491e2f593b3722de9a9d81540bf2daad973b183

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module LB
  class Persistence
    module Relation
      # Helper for joining relations
      module Joins
        def left_join_all_qualified(joins)
          left_join_all(qualified, joins)
        end

        def left_join_all(base, joins)
          join_all(:left_join, base, joins)
        end

        def join_all(type, base, joins)
          joins.each do |relation, on|
            base = base.__send__(type, relation, qualify_on(on))
          end
          base
        end

        def qualify_on(on)
          on.map do |source, target|
            source = source.qualified if source.respond_to?(:qualified)
            target = target.qualified if target.respond_to?(:qualified)
            [source, target]
          end.to_h
        end

        def prefixed(relation,
                     prefix = Dry::Core::Inflector
                       .singularize(relation.schema.name.dataset))
          relation.qualified.prefix(prefix).schema.attributes
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lb-persistence-0.2.0 lib/lb/persistence/relation/joins.rb
lb-persistence-0.1.0 lib/lb/persistence/relation/joins.rb
lb-persistence-0.0.9 lib/lb/persistence/relation/joins.rb