Module: Kasket::SelectManagerMixin

Defined in:
lib/kasket/select_manager_mixin.rb

Instance Method Summary collapse

Instance Method Details

#to_kasket_query(klass, binds = []) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kasket/select_manager_mixin.rb', line 3

def to_kasket_query(klass, binds = [])
  query = Kasket::Visitor.new(klass, binds).accept(ast)

  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] != [:id]

  query
end