Skip to content

Instantly share code, notes, and snippets.

@jonatack
Last active July 30, 2016 13:47
Show Gist options
  • Save jonatack/4630764193a074acba05 to your computer and use it in GitHub Desktop.
Save jonatack/4630764193a074acba05 to your computer and use it in GitHub Desktop.
Failing test for Ransack issue #616: https://github.com/activerecord-hackery/ransack/issues/616
# 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 master
gem 'rails', github: 'rails/rails'
# Rails last release
# gem 'rails'
gem 'sqlite3'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
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.boolean :active, null: false, default: true
end
end
class User < ActiveRecord::Base
scope :active, ->(bool = true) { where(active: bool) }
private
def self.ransackable_scopes(auth_object = nil)
%i(active)
end
end
class BugTest < Minitest::Test
def test_active_scope
sql = User.ransack({ active: true }).result.to_sql
puts sql
assert_equal(
"SELECT \"users\".* FROM \"users\" WHERE \"users\".\"active\" = 't'", sql
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment