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 --
LIKEin place ofILIKE, and the wrong grouping strategy. %w[PostgreSQL PostGIS].freeze
Class Method Summary collapse
-
.connection ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
The connection tags are read and written through.
-
.escape_like(str) ⇒ String
Escapes the SQL pattern wildcards in a string so it matches literally.
-
.like_operator ⇒ String
The case-insensitive pattern operator for the current adapter.
-
.sha_prefix(string) ⇒ String
A short digest of a string, used to keep generated SQL aliases unique and within the identifier length databases allow.
-
.using_mysql? ⇒ TrueClass, FalseClass
Whether tags are stored in MySQL.
-
.using_postgresql? ⇒ TrueClass, FalseClass
Whether tags are stored in PostgreSQL.
Class Method Details
.connection ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
The connection tags are read and written through.
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.
77 78 79 |
# File 'lib/make_taggable/utils.rb', line 77 def escape_like(str) str.gsub(/[!%_]/) { |x| "!" + x } end |
.like_operator ⇒ String
The case-insensitive pattern operator for the current adapter.
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.
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.
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.
36 37 38 |
# File 'lib/make_taggable/utils.rb', line 36 def using_postgresql? !!connection && POSTGRESQL_ADAPTER_NAMES.include?(connection.adapter_name) end |