Sha256: 2a76ed2dd373165ff84becd2496924284e18dea01c3c416365f5f9f9d74aef48

Contents?: true

Size: 850 Bytes

Versions: 5

Compression:

Stored size: 850 Bytes

Contents

require "active_record/version"
require "pluck_map/attribute"

module PluckMap
  module Relationships
    class Many < Base
    protected

      def build_select
        node = PluckMap::JsonSubqueryAggregate.new(scope, attributes.to_json_array)

        # On Rails 4.2, `pluck` can't accept Arel nodes
        if ActiveRecord.version.segments.take(2) == [4,2]
          Arel.sql(scope.connection.visitor.compile(node))
        else
          node
        end
      end

      def build_map
        lambda do |results|
          return [] if results.nil?
          results = JSON.parse(results) if results.is_a?(String)
          results.map do |values|
            attributes.each_with_object({}) do |attribute, hash|
              hash[attribute.name] = attribute.exec(values)
            end
          end
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pluck_map-2.0.0 lib/pluck_map/relationships/many.rb
pluck_map-2.0.0.rc1 lib/pluck_map/relationships/many.rb
pluck_map-1.0.0 lib/pluck_map/relationships/many.rb
pluck_map-1.0.0.rc2 lib/pluck_map/relationships/many.rb
pluck_map-1.0.0.rc1 lib/pluck_map/relationships/many.rb