Sha256: e05af09b95c7e3cbc6d3aa0097d46e9b760f83e49a4def6a58d177ec87e61226

Contents?: true

Size: 755 Bytes

Versions: 3

Compression:

Stored size: 755 Bytes

Contents

require "rack/utils"

module Yaks
  module Behaviour
    module OptionalIncludes
      RACK_KEY = "yaks.optional_includes".freeze

      def associations
        super.select do |association|
          association.if != Undefined || include_association?(association)
        end
      end

      private

      def include_association?(association)
        includes = env.fetch(RACK_KEY) do
          query_string = env.fetch("QUERY_STRING", nil)
          query = Rack::Utils.parse_query(query_string)
          env[RACK_KEY] = query.fetch("include", "").split(",").map { |r| r.split(".") }
        end

        includes.any? do |relationship|
          relationship[mapper_stack.size].eql?(association.name.to_s)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
yaks-0.13.0 lib/yaks/behaviour/optional_includes.rb
yaks-0.12.0 lib/yaks/behaviour/optional_includes.rb
yaks-0.11.0 lib/yaks/behaviour/optional_includes.rb