Sha256: 8952f658d03e1a6119608b290ad401adcef0e7a8824e8acd61fe2099bb20ef8b

Contents?: true

Size: 893 Bytes

Versions: 6

Compression:

Stored size: 893 Bytes

Contents

# The QueryAssociationSplitter takes in a query and removes any .includes, and
# returns the new query, and an hash of association parts.
#
# Example:
#  QueryAssociationSplitter.split([['where', {name: 'Bob'}], ['includes', [:posts, [:posts, :comments], :links]]])
#    => ['where', {name: 'Bob'}], {:posts=>{:comments=>{}}, :links=>{}}]
module Volt
  class QueryAssociationSplitter
    def self.split(query)
      new_query = []
      associations = {}
      query.each do |method, *args|
        method = method.to_sym
        if method == :includes
          args.flatten(1).each do |path|
            path = [path].flatten

            node = associations
            path.each do |part|
              node = (node[part] ||= {})
            end
          end
        else
          new_query << [method, *args]
        end
      end

      return new_query, associations
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
volt-0.9.7.pre8 lib/volt/queries/query_association_splitter.rb
volt-0.9.7.pre7 lib/volt/queries/query_association_splitter.rb
volt-0.9.7.pre6 lib/volt/queries/query_association_splitter.rb
volt-0.9.7.pre5 lib/volt/queries/query_association_splitter.rb
volt-0.9.7.pre3 lib/volt/queries/query_association_splitter.rb
volt-0.9.7.pre2 lib/volt/queries/query_association_splitter.rb