Skip to content

Commit

Permalink
Merge pull request #1191 from yahonda/run_bug_report_templates
Browse files Browse the repository at this point in the history
Move bug report templates into ransack repository and run templates at CI
  • Loading branch information
scarroll32 authored Jan 22, 2021
2 parents 134ea3c + 6c03129 commit 06421f7
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
78 changes: 78 additions & 0 deletions bug_report_templates/test-ransack-scope-and-column-same-name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 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', branch: '6-1-stable'
# 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.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\" = 1", 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
71 changes: 71 additions & 0 deletions bug_report_templates/test-ransacker-arel-present-predicate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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', branch: '6-1-stable'
# 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 :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

0 comments on commit 06421f7

Please sign in to comment.