Module: MakeTaggable::Utils

Defined in:
lib/make_taggable/utils.rb

Overview

Database differences the rest of the library needs to work around.

Constant Summary collapse

POSTGRESQL_ADAPTER_NAMES =

Adapter names that are PostgreSQL as far as SQL generation is concerned.

PostGIS is the PostgreSQL adapter with spatial types layered on top. It reports its own adapter name, so it has to be named here or the library treats a PostGIS application as though it were on MySQL -- LIKE in place of ILIKE, and the wrong grouping strategy.

Returns:

  • (Array<String>)

    frozen

%w[PostgreSQL PostGIS].freeze

Class Method Summary collapse

Class Method Details

.connectionActiveRecord::ConnectionAdapters::AbstractAdapter

The connection tags are read and written through.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)


25
26
27
# File 'lib/make_taggable/utils.rb', line 25

def connection
  MakeTaggable::Tag.connection
end

.escape_like(str) ⇒ String

Escapes the SQL pattern wildcards in a string so it matches literally.

Pair it with an ESCAPE '!' clause.

Parameters:

  • str (String)

    the value to escape

Returns:

  • (String)

    with !, % and _ escaped



77
78
79
# File 'lib/make_taggable/utils.rb', line 77

def escape_like(str)
  str.gsub(/[!%_]/) { |x| "!" + x }
end

.like_operatorString

The case-insensitive pattern operator for the current adapter.

Returns:

  • (String)

    "ILIKE" on PostgreSQL and PostGIS, otherwise "LIKE"



65
66
67
# File 'lib/make_taggable/utils.rb', line 65

def like_operator
  using_postgresql? ? "ILIKE" : "LIKE"
end

.sha_prefix(string) ⇒ String

A short digest of a string, used to keep generated SQL aliases unique and within the identifier length databases allow.

Parameters:

  • string (String)

    the value to digest

Returns:

  • (String)

    the first seven characters of the SHA1 hex digest



56
57
58
# File 'lib/make_taggable/utils.rb', line 56

def sha_prefix(string)
  Digest::SHA1.hexdigest(string)[0..6]
end

.using_mysql?TrueClass, FalseClass

Whether tags are stored in MySQL.

Returns:

  • (TrueClass, FalseClass)


45
46
47
# File 'lib/make_taggable/utils.rb', line 45

def using_mysql?
  connection && connection.adapter_name == "Mysql2"
end

.using_postgresql?TrueClass, FalseClass

Whether tags are stored in PostgreSQL.

True for PostGIS as well, which is PostgreSQL underneath.

Returns:

  • (TrueClass, FalseClass)


36
37
38
# File 'lib/make_taggable/utils.rb', line 36

def using_postgresql?
  !!connection && POSTGRESQL_ADAPTER_NAMES.include?(connection.adapter_name)
end