From 58825b8ed323a5d3eeec0cc20209f16d4c309c64 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 29 Dec 2020 00:20:43 +0900 Subject: [PATCH 1/5] Add `test-ransack-scope-and-column-same-name.rb` This file is located at https://gist.github.com/jonatack/63048bc5062a84ba9e09 Co-authored-by: jonatack --- ...test-ransack-scope-and-column-same-name.rb | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 bug_report_templates/test-ransack-scope-and-column-same-name.rb diff --git a/bug_report_templates/test-ransack-scope-and-column-same-name.rb b/bug_report_templates/test-ransack-scope-and-column-same-name.rb new file mode 100644 index 000000000..47f91665c --- /dev/null +++ b/bug_report_templates/test-ransack-scope-and-column-same-name.rb @@ -0,0 +1,79 @@ +# 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 :activated, -> (boolean = true) { where(active: boolean) } + + private + + def self.ransackable_scopes(auth_object = nil) + %i(activated) + end +end + +class BugTest < Minitest::Test + def test_activated_scope_equals_true + sql = User.ransack({ activated: true }).result.to_sql + puts sql + assert_equal( + "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"active\" = 't'", sql + ) + end + + def test_activated_scope_equals_false + sql = User.ransack({ activated: false }).result.to_sql + puts sql + assert_equal( + "SELECT \"users\".* FROM \"users\"", sql + ) + end +end From 084085dd828e1fa2923030f85cbac814ef0294a1 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 29 Dec 2020 00:23:37 +0900 Subject: [PATCH 2/5] Add `test-ransack-scope-and-column-same-name.rb` This file is located at https://gist.github.com/jonatack/5df41a0edb53b7bad989 Co-authored-by: jonatack --- .../test-ransacker-arel-present-predicate.rb | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 bug_report_templates/test-ransacker-arel-present-predicate.rb diff --git a/bug_report_templates/test-ransacker-arel-present-predicate.rb b/bug_report_templates/test-ransacker-arel-present-predicate.rb new file mode 100644 index 000000000..db4946859 --- /dev/null +++ b/bug_report_templates/test-ransacker-arel-present-predicate.rb @@ -0,0 +1,72 @@ +# test-ransacker-arel-present-predicate.rb + +# Run it in your console with: `ruby test-ransacker-arel-present-predicate.rb` + +# If you change the gem dependencies, run it with: +# `rm gemfile* && ruby test-ransacker-arel-present-predicate.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 :projects, force: true do |t| + t.string :name + t.string :number + end +end + +class Project < ActiveRecord::Base + ransacker :name do + Arel.sql('projects.name') + end + + ransacker :number do |parent| + parent.table[:number] + end +end + +class BugTest < Minitest::Test + def test_ransackers + sql = Project.ransack({number_present: 1}).result.to_sql + puts sql + assert_equal "SELECT \"projects\".* FROM \"projects\" WHERE (\"projects\".\"number\" IS NOT NULL AND \"projects\".\"number\" != '')", sql + + sql = Project.ransack({name_present: 1}).result.to_sql + puts sql + assert_equal "SELECT \"projects\".* FROM \"projects\" WHERE (projects.name IS NOT NULL AND projects.name != '')", sql + end +end From 3ab938897fe74c47c166ce97d26daf625326d0f0 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 29 Dec 2020 00:28:16 +0900 Subject: [PATCH 3/5] Update templates - Ransack does not Rails master branch aka 6.2.0.alpha yet - polyamorous is now part of ransack - sqlite3 adapter default boolean changed from `'t'/'f'` to `1/0` Refer https://github.com/rails/rails/commit/52e050ed00b023968fecda82f19a858876a7c435 - Apply RuboCop auto correct --- .../test-ransack-scope-and-column-same-name.rb | 5 ++--- .../test-ransacker-arel-present-predicate.rb | 7 +++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/bug_report_templates/test-ransack-scope-and-column-same-name.rb b/bug_report_templates/test-ransack-scope-and-column-same-name.rb index 47f91665c..197cf24cd 100644 --- a/bug_report_templates/test-ransack-scope-and-column-same-name.rb +++ b/bug_report_templates/test-ransack-scope-and-column-same-name.rb @@ -12,14 +12,13 @@ source 'https://rubygems.org' # Rails master - gem 'rails', github: 'rails/rails' + gem 'rails', github: 'rails/rails', branch: '6-1-stable' # Rails last release # gem 'rails' gem 'sqlite3' gem 'ransack', github: 'activerecord-hackery/ransack' - gem 'polyamorous', github: 'activerecord-hackery/polyamorous' GEMFILE system 'bundle install' @@ -65,7 +64,7 @@ def test_activated_scope_equals_true sql = User.ransack({ activated: true }).result.to_sql puts sql assert_equal( - "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"active\" = 't'", sql + "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"active\" = 1", sql ) end diff --git a/bug_report_templates/test-ransacker-arel-present-predicate.rb b/bug_report_templates/test-ransacker-arel-present-predicate.rb index db4946859..6939c204d 100644 --- a/bug_report_templates/test-ransacker-arel-present-predicate.rb +++ b/bug_report_templates/test-ransacker-arel-present-predicate.rb @@ -10,14 +10,13 @@ source 'https://rubygems.org' # Rails master - gem 'rails', github: 'rails/rails' + gem 'rails', github: 'rails/rails', branch: '6-1-stable' # Rails last release # gem 'rails' gem 'sqlite3' gem 'ransack', github: 'activerecord-hackery/ransack' - gem 'polyamorous', github: 'activerecord-hackery/polyamorous' GEMFILE system 'bundle install' @@ -61,11 +60,11 @@ class Project < ActiveRecord::Base class BugTest < Minitest::Test def test_ransackers - sql = Project.ransack({number_present: 1}).result.to_sql + sql = Project.ransack({ number_present: 1 }).result.to_sql puts sql assert_equal "SELECT \"projects\".* FROM \"projects\" WHERE (\"projects\".\"number\" IS NOT NULL AND \"projects\".\"number\" != '')", sql - sql = Project.ransack({name_present: 1}).result.to_sql + sql = Project.ransack({ name_present: 1 }).result.to_sql puts sql assert_equal "SELECT \"projects\".* FROM \"projects\" WHERE (projects.name IS NOT NULL AND projects.name != '')", sql end From 9fb739d09cb645e489960b96b2b0a1bdf18d28b3 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 29 Dec 2020 00:59:34 +0900 Subject: [PATCH 4/5] Update bug report template files location --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06b95e93e..b01c81449 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -26,8 +26,8 @@ Steps: Ransack and not in your code or another gem. 4. **Report the issue** by providing the link to a self-contained - gist like [this](https://gist.github.com/jonatack/63048bc5062a84ba9e09) or - [this](https://gist.github.com/jonatack/5df41a0edb53b7bad989). Please use + gist like [this](https://github.com/activerecord-hackery/ransack/blob/run_bug_report_templates/bug_report_templates/test-ransack-scope-and-column-same-name.rb) or + [this](https://github.com/activerecord-hackery/ransack/blob/run_bug_report_templates/bug_report_templates/test-ransacker-arel-present-predicate.rb). Please use these code examples as a bug-report template for your Ransack issue! If you do not provide a self-contained gist and would like your issue to be reviewed, do provide at a minimum: From 6c031298c7da08eb46ff30b9c502ee6668e50b6c Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Tue, 29 Dec 2020 01:06:34 +0900 Subject: [PATCH 5/5] Run bug report template files at GitHub Actions --- .github/workflows/test.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d22ae3a7f..f13abd6cd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -136,3 +136,19 @@ jobs: run: bundle install - name: Run tests run: bundle exec rspec + + bug-report-templates: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.0.0 + - name: Install dependencies + run: bundle install + - name: Run bug report templates + run: | + ruby bug_report_templates/test-ransacker-arel-present-predicate.rb + ruby bug_report_templates/test-ransack-scope-and-column-same-name.rb + rm Gemfile Gemfile.lock