ransack icon indicating copy to clipboard operation
ransack copied to clipboard

`ransack_alias` sometimes does not work correctly

Open itsalongstory opened this issue 4 years ago • 1 comments

test_ransack.rb

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  gem 'activerecord', '~> 6.1', '>= 6.1.4', require: "active_record"
  gem 'sqlite3', '~> 1.4'
  gem 'ransack', '~> 2.4', '>= 2.4.2'
  gem 'minitest', '~> 5.14', '>= 5.14.2', require: "minitest/autorun"
end

ActiveRecord::Base.establish_connection(
  adapter:  "sqlite3",
  database: "./test_ransack_alias"
)

ActiveRecord::Schema.define do
  drop_table(:fees, if_exists: true)

  create_table :fees do |t|
    t.integer :amount
  end
end


class Fee < ActiveRecord::Base
  ransack_alias :amount_a, :amount
end

class MyTest < Minitest::Test
  # success
  def test_1
    query_params = { amount_not_eq: 1 }
    assert_equal "SELECT \"fees\".* FROM \"fees\" WHERE \"fees\".\"amount\" != 1", Fee.ransack(query_params).result.to_sql
  end

  # success
  def test_2
    query_params = { amount_not_eq: 1, amount_a_not_eq: 2 }
    assert_equal "SELECT \"fees\".* FROM \"fees\" WHERE (\"fees\".\"amount\" != 1 AND \"fees\".\"amount\" != 2)", Fee.ransack(query_params).result.to_sql
  end

  # failure
  def test_3
    query_params = { amount_a_not_eq: 2, amount_not_eq: 1 }
    assert_equal "SELECT \"fees\".* FROM \"fees\" WHERE (\"fees\".\"amount\" != 2 AND \"fees\".\"amount\" != 1)", Fee.ransack(query_params).result.to_sql
  end
end
deploy@local-vm:~$ ruby test_ransack.rb
Fetching gem metadata from https://gems.ruby-china.com/........
Resolving dependencies...
Using concurrent-ruby 1.1.9
Using i18n 1.8.10
Using minitest 5.14.4
Using tzinfo 2.0.4
Using zeitwerk 2.4.2
Using activesupport 6.1.4
Using activemodel 6.1.4
Using activerecord 6.1.4
Using bundler 2.2.19
Using ransack 2.4.2
Using sqlite3 1.4.2
-- drop_table(:fees, {:if_exists=>true})
   -> 0.0094s
-- create_table(:fees)
   -> 0.0052s
Run options: --seed 9735

# Running:

..F

Finished in 0.008001s, 374.9565 runs/s, 374.9565 assertions/s.

  1) Failure:
MyTest#test_3 [test_ransack.rb:45]:
--- expected
+++ actual
@@ -1 +1 @@
-"SELECT \"fees\".* FROM \"fees\" WHERE (\"fees\".\"amount\" != 2 AND \"fees\".\"amount\" != 1)"
+"SELECT \"fees\".* FROM \"fees\" WHERE \"fees\".\"amount\" != 1"


3 runs, 3 assertions, 1 failures, 0 errors, 0 skips

itsalongstory avatar Jul 04 '21 15:07 itsalongstory

gem 'ransack', github: 'itsalongstory/ransack', branch: 'issue_1239'

Fixed this.

itsalongstory avatar Aug 02 '24 01:08 itsalongstory