Sha256: b912adcf3243e7e9e28634ff5a7e9c9111d0426c7f9411b5dd32bb286004c917

Contents?: true

Size: 689 Bytes

Versions: 1

Compression:

Stored size: 689 Bytes

Contents

module Polymorph
  class Relation < SimpleDelegator

    def initialize(query, source_types:, fields:)
      @source_types, @fields = source_types, fields
      super(query)
    end

    def count
      self.to_a.length
    end

    def pluck(field)
      self.to_a.map(&field)
    end

    def where(hash = {})
      fields = hash.slice(*@fields)
      fields.map do |field|
        %{(
          #{@source_types.map { |type| "#{type}.#{field[0]} = :#{field[0]}" }.join(" OR ")}
        )}
      end.join(" AND ")
      rebuild(super(clause, fields))
    end

    private

    def rebuild(query)
      self.class.new(query, source_types: @source_types, fields: @fields)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-polymorph-0.1.0 lib/polymorph/relation.rb