Sha256: 86f14db8f487a3ad9418920183492a7cd4df6459085982bfe69134867b29191b

Contents?: true

Size: 716 Bytes

Versions: 2

Compression:

Stored size: 716 Bytes

Contents

# frozen_string_literal: true

module Metka
  class AllTagsQuery
    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

2 entries across 2 versions & 1 rubygems

Version Path
metka-0.1.1 lib/metka/query_builder/all_tags_query.rb
metka-0.1.0 lib/metka/query_builder/all_tags_query.rb