Sha256: d2aa1ca264e5beb8c42e68e20b2421cb14bdfa79935dbf1e194f5a870608e93e

Contents?: true

Size: 863 Bytes

Versions: 7

Compression:

Stored size: 863 Bytes

Contents

# frozen_string_literal: true
module Kasket
  module SelectManagerMixin
    def to_kasket_query(klass, binds = [])
      begin
        query = Kasket::Visitor.new(klass, binds).accept(ast)
      rescue TypeError # unsupported object in ast
        return nil
      end

      return nil if query.nil? || query == :unsupported
      return nil if query[:attributes].blank?

      query[:index] = query[:attributes].map(&:first)

      if query[:limit]
        return nil if query[:limit] > 1
        # return nil if !query[:index].include?(:id)
      end

      if query[:index].size > 1 && query[:attributes].any? { |_attribute, value| value.is_a?(Array) }
        return nil
      end

      query[:key] = klass.kasket_key_for(query[:attributes])
      query[:key] << '/first' if query[:limit] == 1 && !query[:index].include?(:id)

      query
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kasket-4.8.0 lib/kasket/select_manager_mixin.rb
kasket-4.7.0 lib/kasket/select_manager_mixin.rb
kasket-4.6.0 lib/kasket/select_manager_mixin.rb
kasket-4.5.1 lib/kasket/select_manager_mixin.rb
kasket-4.5.0 lib/kasket/select_manager_mixin.rb
kasket-4.4.5 lib/kasket/select_manager_mixin.rb
kasket-4.4.4 lib/kasket/select_manager_mixin.rb