Sha256: 0dd92120e8dd3024f431ff0fa616033c3facd688614939b7e8b9aa482be8ac72

Contents?: true

Size: 737 Bytes

Versions: 1

Compression:

Stored size: 737 Bytes

Contents

# frozen_string_literal: true

require 'singleton'

module Metka
  class AnyTagsQuery
    include Singleton

    def call(model, column_name, tag_list)
      column_cast = Arel::Nodes::NamedFunction.new(
        'CAST',
        [model.arel_table[column_name].as('text[]')]
      )

      value = Arel::Nodes::SqlLiteral.new(
        # In Rails 5.2 and above Sanitanization moved to public level, but still we have to support 4.2 and 5.0 and 5.1
        ActiveRecord::Base.send(:sanitize_sql_for_conditions, ['ARRAY[?]', tag_list.to_a])
      )

      value_cast = Arel::Nodes::NamedFunction.new(
        'CAST',
        [value.as('text[]')]
      )

      Arel::Nodes::InfixOperation.new('&&', column_cast, value_cast)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metka-0.1.2 lib/metka/query_builder/any_tags_query.rb