class Gutentag::TaggedWith::Query

Attributes

match[R]
model[R]
values[R]

Public Class Methods

new(model, values, match) click to toggle source
# File lib/gutentag/tagged_with/query.rb, line 4
def initialize(model, values, match)
  @model  = model
  @values = Array values
  @match  = match
end

Public Instance Methods

call() click to toggle source
# File lib/gutentag/tagged_with/query.rb, line 10
def call
  model.where "#{model_id} #{operator} (#{query.to_sql})"
end

Private Instance Methods

match_any_or_none?() click to toggle source
# File lib/gutentag/tagged_with/query.rb, line 32
def match_any_or_none?
  %i[any none].include?(match)
end
model_id() click to toggle source
# File lib/gutentag/tagged_with/query.rb, line 18
def model_id
  "#{model.quoted_table_name}.#{model.quoted_primary_key}"
end
operator() click to toggle source
# File lib/gutentag/tagged_with/query.rb, line 28
def operator
  match == :none ? "NOT IN" : "IN"
end
query() click to toggle source
# File lib/gutentag/tagged_with/query.rb, line 22
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