Skip to content

Instantly share code, notes, and snippets.

@AmShaegar13
Created April 11, 2024 09:14
Show Gist options
  • Save AmShaegar13/efd2eb4a0be3bf3e3d9ec44817c5198b to your computer and use it in GitHub Desktop.
Save AmShaegar13/efd2eb4a0be3bf3e3d9ec44817c5198b to your computer and use it in GitHub Desktop.
[BUG] ActiveRecord::normalizes can break `cont` predicate
# test-ransack-scope-and-column-same-name.rb
# This is a stand-alone test case.
# Run it in your console with: `ruby test-ransack-scope-and-column-same-name.rb`
# If you change the gem dependencies, run it with:
# `rm gemfile* && ruby test-ransack-scope-and-column-same-name.rb`
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
# Rails last release
gem 'rails'
gem 'sqlite3'
gem 'ransack', github: 'activerecord-hackery/ransack'
GEMFILE
system 'bundle install'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'ransack'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Display versions.
message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{
::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{
::ActiveRecord::Base.connection.adapter_name}"
line = '=' * message.length
puts line, message, line
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, null: false
end
end
class User < ActiveRecord::Base
normalizes :name, with: ->(name) { name.gsub(/[^a-z0-9]/, '_') }
def self.ransackable_attributes(auth_object = nil)
%w[name]
end
end
class BugTest < Minitest::Test
def test_normalized_attribute_using_ransack_with_cont
sql = User.ransack({ name_cont: 'foo' }).result.to_sql
assert_includes(sql, "LIKE '%foo%'")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment