Sha256: ffc5c68f634109ef5cf8fab2a87c80b86effc63d6f87510234cc5ad830067317

Contents?: true

Size: 691 Bytes

Versions: 3

Compression:

Stored size: 691 Bytes

Contents

# frozen_string_literal: true

class Gutentag::TaggedWith::Query
  def initialize(model, values, match)
    @model  = model
    @values = Array values
    @match  = match
  end

  def call
    model.where "#{model_id} #{operator} (#{query.to_sql})"
  end

  private

  attr_reader :model, :values, :match

  def model_id
    "#{model.quoted_table_name}.#{model.quoted_primary_key}"
  end

  def query
    return taggable_ids_query if match_any_or_none? || values.length == 1

    taggable_ids_query.having("COUNT(*) = #{values.length}").group(:taggable_id)
  end

  def operator
    match == :none ? "NOT IN" : "IN"
  end

  def match_any_or_none?
    %i[any none].include?(match)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gutentag-2.6.2 lib/gutentag/tagged_with/query.rb
gutentag-2.6.1 lib/gutentag/tagged_with/query.rb
gutentag-2.6.0 lib/gutentag/tagged_with/query.rb