From 45a1cb947e58d98fa6c06291ccad5faf0412020b Mon Sep 17 00:00:00 2001 From: Lee Nave Date: Sat, 29 Jun 2024 08:23:26 -0700 Subject: [PATCH] Add support for Trilogy database adapter --- CHANGELOG.md | 2 ++ Gemfile | 1 + lib/ransack/constants.rb | 2 +- spec/ransack/adapters/active_record/base_spec.rb | 2 +- spec/ransack/predicate_spec.rb | 8 ++++---- spec/support/schema.rb | 11 +++++++++++ 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82b61284..53141ca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* Add support for Trilogy database adapter by @navels in https://github.com/activerecord-hackery/ransack/pull/1500 + ## 4.1.0 - 2023-10-23 ### 🚀 Features diff --git a/Gemfile b/Gemfile index 4b3b92e4..ec20d9b6 100644 --- a/Gemfile +++ b/Gemfile @@ -43,6 +43,7 @@ else end end gem 'mysql2' +gem 'trilogy' group :test do gem 'machinist', '~> 1.0.6' diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index 9202b4b0..6b26ed3a 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -162,7 +162,7 @@ module Constants # replace % \ to \% \\ def escape_wildcards(unescaped) case ActiveRecord::Base.connection.adapter_name - when "Mysql2".freeze + when "Mysql2".freeze, "Trilogy".freeze # Necessary for MySQL unescaped.to_s.gsub(/([\\%_])/, '\\\\\\1') when "PostgreSQL".freeze diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index d41cd6d0..b8039f2d 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -794,7 +794,7 @@ def without_application_record_method(method) end def rails7_and_mysql - ::ActiveRecord::VERSION::MAJOR >= 7 && ENV['DB'] == 'mysql' + ::ActiveRecord::VERSION::MAJOR >= 7 && ['mysql', 'trilogy'].include?(ENV['DB']) end end end diff --git a/spec/ransack/predicate_spec.rb b/spec/ransack/predicate_spec.rb index ae0972d9..b15a80e3 100644 --- a/spec/ransack/predicate_spec.rb +++ b/spec/ransack/predicate_spec.rb @@ -160,7 +160,7 @@ module Ransack it_has_behavior 'wildcard escaping', :name_cont, (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" /"people"."name" ILIKE '%\\%\\.\\_\\\\%'/ - elsif ActiveRecord::Base.connection.adapter_name == "Mysql2" + elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name) /`people`.`name` LIKE '%\\\\%.\\\\_\\\\\\\\%'/ else /"people"."name" LIKE '%%._\\%'/ @@ -179,7 +179,7 @@ module Ransack it_has_behavior 'wildcard escaping', :name_not_cont, (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" /"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/ - elsif ActiveRecord::Base.connection.adapter_name == "Mysql2" + elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name) /`people`.`name` NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/ else /"people"."name" NOT LIKE '%%._\\%'/ @@ -198,7 +198,7 @@ module Ransack it_has_behavior 'wildcard escaping', :name_i_cont, (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" /"people"."name" ILIKE '%\\%\\.\\_\\\\%'/ - elsif ActiveRecord::Base.connection.adapter_name == "Mysql2" + elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name) /LOWER\(`people`.`name`\) LIKE '%\\\\%.\\\\_\\\\\\\\%'/ else /LOWER\("people"."name"\) LIKE '%%._\\%'/ @@ -217,7 +217,7 @@ module Ransack it_has_behavior 'wildcard escaping', :name_not_i_cont, (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" /"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/ - elsif ActiveRecord::Base.connection.adapter_name == "Mysql2" + elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name) /LOWER\(`people`.`name`\) NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/ else /LOWER\("people"."name"\) NOT LIKE '%%._\\%'/ diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 555299c4..03194e7f 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -8,6 +8,17 @@ database: 'ransack', username: ENV.fetch("MYSQL_USERNAME") { "root" }, password: ENV.fetch("MYSQL_PASSWORD") { "" }, + host: ENV.fetch("MYSQL_HOST") { "localhost" }, + encoding: 'utf8' + ) +when 'trilogy' + # To test with trilogy: `DB=trilogy bundle exec rake spec` + ActiveRecord::Base.establish_connection( + adapter: 'trilogy', + database: 'ransack', + username: ENV.fetch("MYSQL_USERNAME") { "root" }, + password: ENV.fetch("MYSQL_PASSWORD") { "" }, + host: ENV.fetch("MYSQL_HOST") { "localhost" }, encoding: 'utf8' ) when 'pg', 'postgres', 'postgresql'