ransack 2.3.0 → 2.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/SECURITY.md +12 -0
  4. data/.github/workflows/cronjob.yml +105 -0
  5. data/.github/workflows/rubocop.yml +20 -0
  6. data/.github/workflows/test.yml +154 -0
  7. data/.rubocop.yml +44 -0
  8. data/CHANGELOG.md +31 -1
  9. data/CONTRIBUTING.md +13 -11
  10. data/Gemfile +19 -5
  11. data/README.md +119 -54
  12. data/bug_report_templates/test-ransack-scope-and-column-same-name.rb +78 -0
  13. data/bug_report_templates/test-ransacker-arel-present-predicate.rb +71 -0
  14. data/docs/img/create_release.png +0 -0
  15. data/docs/release_process.md +20 -0
  16. data/{polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2 → lib/polyamorous/activerecord_5.2_ruby_2}/join_association.rb +4 -2
  17. data/{polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2 → lib/polyamorous/activerecord_5.2_ruby_2}/join_dependency.rb +0 -2
  18. data/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb +11 -0
  19. data/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +1 -0
  20. data/{polyamorous/lib → lib}/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +0 -1
  21. data/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +1 -0
  22. data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +74 -0
  23. data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +93 -0
  24. data/{polyamorous/lib → lib}/polyamorous/activerecord_6.1_ruby_2/reflection.rb +0 -1
  25. data/lib/polyamorous/activerecord_6.2_ruby_2/join_association.rb +1 -0
  26. data/lib/polyamorous/activerecord_6.2_ruby_2/join_dependency.rb +1 -0
  27. data/lib/polyamorous/activerecord_6.2_ruby_2/reflection.rb +1 -0
  28. data/{polyamorous/lib → lib}/polyamorous/join.rb +0 -0
  29. data/{polyamorous/lib → lib/polyamorous}/polyamorous.rb +3 -8
  30. data/{polyamorous/lib → lib}/polyamorous/swapping_reflection_class.rb +0 -0
  31. data/{polyamorous/lib → lib}/polyamorous/tree_node.rb +0 -0
  32. data/lib/ransack.rb +3 -4
  33. data/lib/ransack/adapters/active_record/base.rb +4 -0
  34. data/lib/ransack/adapters/active_record/context.rb +51 -80
  35. data/lib/ransack/adapters/active_record/ransack/constants.rb +13 -1
  36. data/lib/ransack/adapters/active_record/ransack/context.rb +2 -6
  37. data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +11 -4
  38. data/lib/ransack/configuration.rb +17 -1
  39. data/lib/ransack/constants.rb +2 -5
  40. data/lib/ransack/helpers.rb +1 -1
  41. data/lib/ransack/helpers/form_builder.rb +8 -14
  42. data/lib/ransack/locale/sk.yml +70 -0
  43. data/lib/ransack/nodes/attribute.rb +1 -1
  44. data/lib/ransack/nodes/condition.rb +7 -1
  45. data/lib/ransack/nodes/grouping.rb +1 -1
  46. data/lib/ransack/nodes/sort.rb +1 -1
  47. data/lib/ransack/nodes/value.rb +1 -1
  48. data/lib/ransack/predicate.rb +2 -1
  49. data/lib/ransack/search.rb +3 -1
  50. data/lib/ransack/translate.rb +3 -3
  51. data/lib/ransack/version.rb +1 -1
  52. data/ransack.gemspec +8 -23
  53. data/spec/blueprints/articles.rb +1 -1
  54. data/spec/blueprints/comments.rb +1 -1
  55. data/spec/blueprints/notes.rb +1 -1
  56. data/spec/blueprints/tags.rb +1 -1
  57. data/spec/console.rb +5 -5
  58. data/spec/helpers/polyamorous_helper.rb +1 -10
  59. data/spec/helpers/ransack_helper.rb +1 -1
  60. data/spec/{ransack → polyamorous}/join_association_spec.rb +7 -0
  61. data/spec/{ransack → polyamorous}/join_dependency_spec.rb +0 -0
  62. data/spec/{ransack → polyamorous}/join_spec.rb +0 -0
  63. data/spec/ransack/adapters/active_record/base_spec.rb +9 -6
  64. data/spec/ransack/adapters/active_record/context_spec.rb +19 -18
  65. data/spec/ransack/configuration_spec.rb +10 -0
  66. data/spec/ransack/helpers/form_helper_spec.rb +16 -16
  67. data/spec/ransack/nodes/grouping_spec.rb +2 -2
  68. data/spec/ransack/predicate_spec.rb +39 -1
  69. data/spec/ransack/search_spec.rb +105 -17
  70. data/spec/spec_helper.rb +9 -5
  71. data/spec/support/schema.rb +8 -3
  72. metadata +41 -177
  73. data/.travis.yml +0 -49
  74. data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +0 -2
  75. data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +0 -2
  76. data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +0 -31
  77. data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +0 -112
  78. data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb +0 -31
  79. data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +0 -112
  80. data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/reflection.rb +0 -12
  81. data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb +0 -2
  82. data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +0 -2
  83. data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +0 -2
  84. data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +0 -2
  85. data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +0 -2
  86. data/polyamorous/lib/polyamorous/version.rb +0 -3
  87. data/polyamorous/polyamorous.gemspec +0 -35
data/Gemfile CHANGED
@@ -5,16 +5,28 @@ gem 'rake'
5
5
 
6
6
  rails = ENV['RAILS'] || '6-0-stable'
7
7
 
8
- gem 'pry'
8
+ rails_version = case rails
9
+ when /\// # A path
10
+ File.read(File.join(rails, "RAILS_VERSION"))
11
+ when /^v/ # A tagged version
12
+ rails.gsub(/^v/, '')
13
+ else
14
+ rails
15
+ end
9
16
 
10
- # Provide timezone information on Windows
11
- gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
17
+ gem 'faker', '~> 2.0'
18
+ gem 'sqlite3', ::Gem::Version.new(rails_version == 'main' ? '6.2.0.alpha' : rails_version) >= ::Gem::Version.new('6-0-stable') ? '~> 1.4.1' : '~> 1.3.3'
19
+ gem 'pg', '~> 1.0'
20
+ gem 'pry', '~> 0.12.2'
21
+ gem 'byebug'
12
22
 
13
23
  case rails
14
24
  when /\// # A path
15
25
  gem 'activesupport', path: "#{rails}/activesupport"
26
+ gem 'activemodel', path: "#{rails}/activemodel"
16
27
  gem 'activerecord', path: "#{rails}/activerecord", require: false
17
28
  gem 'actionpack', path: "#{rails}/actionpack"
29
+ gem 'actionview', path: "#{rails}/actionview"
18
30
  when /^v/ # A tagged version
19
31
  git 'https://github.com/rails/rails.git', :tag => rails do
20
32
  gem 'activesupport'
@@ -33,7 +45,9 @@ end
33
45
  gem 'mysql2', '~> 0.5.2'
34
46
 
35
47
  group :test do
36
- # TestUnit was removed from Ruby 2.2 but still needed for testing Rails 3.x.
37
- gem 'test-unit', '~> 3.0' if RUBY_VERSION >= '2.2'
48
+ gem 'machinist', '~> 1.0.6'
49
+ gem 'rspec', '~> 3'
38
50
  gem 'simplecov', :require => false
39
51
  end
52
+
53
+ gem 'rubocop', require: false
data/README.md CHANGED
@@ -1,19 +1,9 @@
1
1
  # ![Ransack](./logo/ransack-h.png "Ransack")
2
2
 
3
- [![Build Status](https://travis-ci.org/activerecord-hackery/ransack.svg)](https://travis-ci.org/activerecord-hackery/ransack)
3
+ [![Build Status](https://github.com/activerecord-hackery/ransack/workflows/test/badge.svg)](https://github.com/activerecord-hackery/ransack/actions)
4
4
  [![Gem Version](https://badge.fury.io/rb/ransack.svg)](http://badge.fury.io/rb/ransack)
5
5
  [![Code Climate](https://codeclimate.com/github/activerecord-hackery/ransack/badges/gpa.svg)](https://codeclimate.com/github/activerecord-hackery/ransack)
6
- [![Backers on Open Collective](https://opencollective.com/ransack/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/ransack/sponsors/badge.svg)](#sponsors)
7
-
8
- Ransack is a rewrite of [MetaSearch](https://github.com/activerecord-hackery/meta_search)
9
- created by [Ernie Miller](http://twitter.com/erniemiller)
10
- and developed/maintained for years by
11
- [Jon Atack](http://twitter.com/jonatack) and
12
- [Ryan Bigg](http://twitter.com/ryanbigg) with the help of a great group of
13
- [contributors](https://github.com/activerecord-hackery/ransack/graphs/contributors). Ransack's logo is designed by [Anıl Kılıç](https://github.com/anilkilic).
14
- While it supports many of the same features as MetaSearch, its underlying
15
- implementation differs greatly from MetaSearch,
16
- and backwards compatibility is not a design goal.
6
+ [![Backers on Open Collective](https://opencollective.com/ransack/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/ransack/sponsors/badge.svg)](#sponsors)
17
7
 
18
8
  Ransack enables the creation of both
19
9
  [simple](http://ransack-demo.herokuapp.com) and
@@ -25,25 +15,9 @@ or controller layer, you're probably not looking for Ransack (or MetaSearch,
25
15
  for that matter). Try [Squeel](https://github.com/activerecord-hackery/squeel)
26
16
  instead.
27
17
 
28
- If you're viewing this at
29
- [github.com/activerecord-hackery/ransack](https://github.com/activerecord-hackery/ransack),
30
- you're reading the documentation for the master branch with the latest features.
31
- [View documentation for the last release (2.0.0).](https://github.com/activerecord-hackery/ransack/tree/v2.0.0)
32
-
33
- ## Rails 6 Support
34
-
35
- Rails 6 is right around the corner, but not yet released. Ransack master branch supports ```6.0.0.rc2```. Full Rails 6 support will be provided after the official Rails 6 release. See [this issue](https://github.com/activerecord-hackery/ransack/issues/1032) for details.
36
-
37
18
  ## Getting started
38
19
 
39
- Ransack is compatible with Rails 6.0.0.rc2, 5.0, 5.1 and 5.2 on Ruby 2.2 and later.
40
- If you are using Rails <5.0 use the 1.8 line of Ransack.
41
- If you are using Ruby 1.8 or an earlier JRuby and run into compatibility
42
- issues, you can use an earlier version of Ransack, say, up to 1.3.0.
43
-
44
- Ransack works out-of-the-box with Active Record and also features limited
45
- support for Mongoid 4 and 5 (without associations, further details
46
- [below](https://github.com/activerecord-hackery/ransack#mongoid)).
20
+ Ransack is supported for Rails 6.1, 6.0, 5.2 on Ruby 2.6.6 and later.
47
21
 
48
22
  In your Gemfile, for the last officially released gem:
49
23
 
@@ -67,27 +41,14 @@ gem 'ransack', github: 'activerecord-hackery/ransack'
67
41
 
68
42
  ## Usage
69
43
 
70
- Ransack can be used in one of two modes, simple or advanced.
44
+ Ransack can be used in one of two modes, simple or advanced. For
45
+ searching/filtering not requiring complex boolean logic, Ransack's simple
46
+ mode should meet your needs.
71
47
 
72
- ### Simple Mode
48
+ If you're coming from MetaSearch (Ransack's predecessor), refer to the
49
+ [Updating From MetaSearch](#updating-from-metasearch) section
73
50
 
74
- This mode works much like MetaSearch, for those of you who are familiar with
75
- it, and requires very little setup effort.
76
-
77
- If you're coming from MetaSearch, things to note:
78
-
79
- 1. The default param key for search params is now `:q`, instead of `:search`.
80
- This is primarily to shorten query strings, though advanced queries (below)
81
- will still run afoul of URL length limits in most browsers and require a
82
- switch to HTTP POST requests. This key is [configurable](https://github.com/activerecord-hackery/ransack/wiki/Configuration).
83
-
84
- 2. `form_for` is now `search_form_for`, and validates that a Ransack::Search
85
- object is passed to it.
86
-
87
- 3. Common ActiveRecord::Relation methods are no longer delegated by the
88
- search object. Instead, you will get your search results (an
89
- ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to
90
- `Ransack#result`.
51
+ ### Simple Mode
91
52
 
92
53
  #### In your controller
93
54
 
@@ -110,6 +71,20 @@ def index
110
71
  end
111
72
  ```
112
73
 
74
+ ##### Default search parameter
75
+
76
+ Ransack uses a default `:q` param key for search params. This may be changed by
77
+ setting the `search_key` option in a Ransack initializer file (typically
78
+ `config/initializers/ransack.rb`):
79
+
80
+ ```
81
+ Ransack.configure do |c|
82
+ # Change default search parameter key name.
83
+ # Default key name is :q
84
+ c.search_key = :query
85
+ end
86
+ ```
87
+
113
88
  #### In your view
114
89
 
115
90
  The two primary Ransack view helpers are `search_form_for` and `sort_link`,
@@ -276,6 +251,20 @@ the order indicator arrow by passing `hide_indicator: true` in the sort link:
276
251
  default_order: { last_name: 'asc', first_name: 'desc' }) %>
277
252
  ```
278
253
 
254
+ #### PostgreSQL's sort option
255
+
256
+ The `NULLS FIRST` and `NULLS LAST` options can be used to determine whether nulls appear before or after non-null values in the sort ordering.
257
+
258
+ You may want to configure it like this:
259
+
260
+ ```rb
261
+ Ransack.configure do |c|
262
+ c.postgres_fields_sort_option = :nulls_first # or :nulls_last
263
+ end
264
+ ```
265
+
266
+ See this feature: https://www.postgresql.org/docs/13/queries-order.html
267
+
279
268
  ### Advanced Mode
280
269
 
281
270
  "Advanced" searches (ab)use Rails' nested attributes functionality in order to
@@ -463,13 +452,10 @@ List of all possible predicates
463
452
  | `*_lteq_any` | Less than or equal to any | |
464
453
  | `*_gt_any` | Greater than any | |
465
454
  | `*_gteq_any` | Greater than or equal to any | |
466
- | `*_matches_any` | `*_does_not_match_any` | same as above but with `LIKE` |
467
455
  | `*_lt_all` | Less than all | SQL: `col < value1 AND col < value2` |
468
456
  | `*_lteq_all` | Less than or equal to all | |
469
457
  | `*_gt_all` | Greater than all | |
470
458
  | `*_gteq_all` | Greater than or equal to all | |
471
- | `*_matches_all` | Matches all | same as above but with `LIKE` |
472
- | `*_does_not_match_all` | Does not match all | |
473
459
  | `*_not_eq_all` | none of values in a set | |
474
460
  | `*_start` | Starts with | SQL: `col LIKE 'value%'` |
475
461
  | `*_not_start` | Does not start with | |
@@ -489,6 +475,12 @@ List of all possible predicates
489
475
  | `*_not_cont` | Does not contain |
490
476
  | `*_not_cont_any` | Does not contain any of | |
491
477
  | `*_not_cont_all` | Does not contain all of | |
478
+ | `*_i_cont` | Contains value with case insensitive | uses `ILIKE` |
479
+ | `*_i_cont_any` | Contains any of values with case insensitive | |
480
+ | `*_i_cont_all` | Contains all of values with case insensitive | |
481
+ | `*_not_i_cont` | Does not contain with case insensitive |
482
+ | `*_not_i_cont_any` | Does not contain any of values with case insensitive | |
483
+ | `*_not_i_cont_all` | Does not contain all of values with case insensitive | |
492
484
  | `*_true` | is true | |
493
485
  | `*_false` | is false | |
494
486
 
@@ -693,6 +685,43 @@ Trying it out in `rails console`:
693
685
 
694
686
  That's it! Now you know how to whitelist/blacklist various elements in Ransack.
695
687
 
688
+ ### Handling unknown predicates or attributes
689
+
690
+ By default, Ransack will ignore any unknown predicates or attributes:
691
+
692
+ ```ruby
693
+ Article.ransack(unknown_attr_eq: 'Ernie').result.to_sql
694
+ => SELECT "articles".* FROM "articles"
695
+ ```
696
+
697
+ Ransack may be configured to raise an error if passed an unknown predicate or
698
+ attributes, by setting the `ignore_unknown_conditions` option to `false` in your
699
+ Ransack initializer file at `config/initializers/ransack.rb`:
700
+
701
+ ```ruby
702
+ Ransack.configure do |c|
703
+ # Raise errors if a query contains an unknown predicate or attribute.
704
+ # Default is true (do not raise error on unknown conditions).
705
+ c.ignore_unknown_conditions = false
706
+ end
707
+ ```
708
+
709
+ ```ruby
710
+ Article.ransack(unknown_attr_eq: 'Ernie')
711
+ # ArgumentError (Invalid search term unknown_attr_eq)
712
+ ```
713
+
714
+ As an alternative to setting a global configuration option, the `.ransack!`
715
+ class method also raises an error if passed an unknown condition:
716
+
717
+ ```ruby
718
+ Article.ransack!(unknown_attr_eq: 'Ernie')
719
+ # ArgumentError: Invalid search term unknown_attr_eq
720
+ ```
721
+
722
+ This is equivilent to the `ignore_unknown_conditions` configuration option,
723
+ except it may be applied on a case-by-case basis.
724
+
696
725
  ### Using Scopes/Class Methods
697
726
 
698
727
  Continuing on from the preceding section, searching by scopes requires defining
@@ -755,7 +784,7 @@ To turn this off on a per-scope basis Ransack adds the following method to
755
784
  Add the scope you wish to bypass this behavior to ransackable_scopes_skip_sanitize_args:
756
785
 
757
786
  ```ruby
758
- def ransackable_scopes_skip_sanitize_args
787
+ def self.ransackable_scopes_skip_sanitize_args
759
788
  [:scope_to_skip_sanitize_args]
760
789
  end
761
790
  ```
@@ -889,6 +918,28 @@ en:
889
918
  title: Old Ransack Namespaced Title
890
919
  ```
891
920
 
921
+ ### Updating From MetaSearch
922
+
923
+ Ransack works much like MetaSearch, for those of you who are familiar with
924
+ it, and requires very little setup effort.
925
+
926
+ If you're coming from MetaSearch, things to note:
927
+
928
+ 1. The default param key for search params is now `:q`, instead of `:search`.
929
+ This is primarily to shorten query strings, though advanced queries (below)
930
+ will still run afoul of URL length limits in most browsers and require a
931
+ switch to HTTP POST requests. This key is
932
+ [configurable](default-search-parameter) via setting the `search_key` option
933
+ in your Ransack intitializer file.
934
+
935
+ 2. `form_for` is now `search_form_for`, and validates that a Ransack::Search
936
+ object is passed to it.
937
+
938
+ 3. Common ActiveRecord::Relation methods are no longer delegated by the
939
+ search object. Instead, you will get your search results (an
940
+ ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to
941
+ `Ransack#result`.
942
+
892
943
  ## Mongoid
893
944
 
894
945
  Mongoid support has been moved to its own gem at [ransack-mongoid](https://github.com/activerecord-hackery/ransack-mongoid).
@@ -926,6 +977,7 @@ In other words: `Major.Minor.Patch`.
926
977
 
927
978
  To support the project:
928
979
 
980
+ * Consider supporting via [Open Collective](https://opencollective.com/ransack/backers/badge.svg)
929
981
  * Use Ransack in your apps, and let us know if you encounter anything that's
930
982
  broken or missing. A failing spec to demonstrate the issue is awesome. A pull
931
983
  request with passing tests is even better!
@@ -941,6 +993,21 @@ fix bugs!
941
993
 
942
994
  This project exists thanks to all the people who contribute. <img src="https://opencollective.com/ransack/contributors.svg?width=890&button=false" />
943
995
 
996
+ Ransack is a rewrite of [MetaSearch](https://github.com/activerecord-hackery/meta_search)
997
+ created by [Ernie Miller](http://twitter.com/erniemiller)
998
+ and developed/maintained by:
999
+
1000
+ - [Greg Molnar](https://github.com/gregmolnar)
1001
+ - [Deivid Rodriguez](https://github.com/deivid-rodriguez)
1002
+ - [Sean Carroll](https://github.com/seanfcarroll)
1003
+ - [Jon Atack](http://twitter.com/jonatack)
1004
+ - [Ryan Bigg](http://twitter.com/ryanbigg)
1005
+ - a great group of [contributors](https://github.com/activerecord-hackery/ransack/graphs/contributors).
1006
+ - Ransack's logo is designed by [Anıl Kılıç](https://github.com/anilkilic).
1007
+
1008
+ While it supports many of the same features as MetaSearch, its underlying implementation differs greatly from MetaSearch, and backwards compatibility is not a design goal.
1009
+
1010
+
944
1011
 
945
1012
  ## Backers
946
1013
 
@@ -963,5 +1030,3 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
963
1030
  <a href="https://opencollective.com/ransack/sponsor/7/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/7/avatar.svg"></a>
964
1031
  <a href="https://opencollective.com/ransack/sponsor/8/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/8/avatar.svg"></a>
965
1032
  <a href="https://opencollective.com/ransack/sponsor/9/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/9/avatar.svg"></a>
966
-
967
-
@@ -0,0 +1,78 @@
1
+ # test-ransack-scope-and-column-same-name.rb
2
+
3
+ # This is a stand-alone test case.
4
+
5
+ # Run it in your console with: `ruby test-ransack-scope-and-column-same-name.rb`
6
+
7
+ # If you change the gem dependencies, run it with:
8
+ # `rm gemfile* && ruby test-ransack-scope-and-column-same-name.rb`
9
+
10
+ unless File.exist?('Gemfile')
11
+ File.write('Gemfile', <<-GEMFILE)
12
+ source 'https://rubygems.org'
13
+
14
+ # Rails master
15
+ gem 'rails', github: 'rails/rails', branch: '6-1-stable'
16
+
17
+ # Rails last release
18
+ # gem 'rails'
19
+
20
+ gem 'sqlite3'
21
+ gem 'ransack', github: 'activerecord-hackery/ransack'
22
+ GEMFILE
23
+
24
+ system 'bundle install'
25
+ end
26
+
27
+ require 'bundler'
28
+ Bundler.setup(:default)
29
+
30
+ require 'active_record'
31
+ require 'minitest/autorun'
32
+ require 'logger'
33
+ require 'ransack'
34
+
35
+ # This connection will do for database-independent bug reports.
36
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
37
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
38
+
39
+ # Display versions.
40
+ message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{
41
+ ::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{
42
+ ::ActiveRecord::Base.connection.adapter_name}"
43
+ line = '=' * message.length
44
+ puts line, message, line
45
+
46
+ ActiveRecord::Schema.define do
47
+ create_table :users, force: true do |t|
48
+ t.boolean :active, null: false, default: true
49
+ end
50
+ end
51
+
52
+ class User < ActiveRecord::Base
53
+ scope :activated, -> (boolean = true) { where(active: boolean) }
54
+
55
+ private
56
+
57
+ def self.ransackable_scopes(auth_object = nil)
58
+ %i(activated)
59
+ end
60
+ end
61
+
62
+ class BugTest < Minitest::Test
63
+ def test_activated_scope_equals_true
64
+ sql = User.ransack({ activated: true }).result.to_sql
65
+ puts sql
66
+ assert_equal(
67
+ "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"active\" = 1", sql
68
+ )
69
+ end
70
+
71
+ def test_activated_scope_equals_false
72
+ sql = User.ransack({ activated: false }).result.to_sql
73
+ puts sql
74
+ assert_equal(
75
+ "SELECT \"users\".* FROM \"users\"", sql
76
+ )
77
+ end
78
+ end
@@ -0,0 +1,71 @@
1
+ # test-ransacker-arel-present-predicate.rb
2
+
3
+ # Run it in your console with: `ruby test-ransacker-arel-present-predicate.rb`
4
+
5
+ # If you change the gem dependencies, run it with:
6
+ # `rm gemfile* && ruby test-ransacker-arel-present-predicate.rb`
7
+
8
+ unless File.exist?('Gemfile')
9
+ File.write('Gemfile', <<-GEMFILE)
10
+ source 'https://rubygems.org'
11
+
12
+ # Rails master
13
+ gem 'rails', github: 'rails/rails', branch: '6-1-stable'
14
+
15
+ # Rails last release
16
+ # gem 'rails'
17
+
18
+ gem 'sqlite3'
19
+ gem 'ransack', github: 'activerecord-hackery/ransack'
20
+ GEMFILE
21
+
22
+ system 'bundle install'
23
+ end
24
+
25
+ require 'bundler'
26
+ Bundler.setup(:default)
27
+
28
+ require 'active_record'
29
+ require 'minitest/autorun'
30
+ require 'logger'
31
+ require 'ransack'
32
+
33
+ # This connection will do for database-independent bug reports.
34
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
35
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
36
+
37
+ # Display versions.
38
+ message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{
39
+ ::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{
40
+ ::ActiveRecord::Base.connection.adapter_name}"
41
+ line = '=' * message.length
42
+ puts line, message, line
43
+
44
+ ActiveRecord::Schema.define do
45
+ create_table :projects, force: true do |t|
46
+ t.string :name
47
+ t.string :number
48
+ end
49
+ end
50
+
51
+ class Project < ActiveRecord::Base
52
+ ransacker :name do
53
+ Arel.sql('projects.name')
54
+ end
55
+
56
+ ransacker :number do |parent|
57
+ parent.table[:number]
58
+ end
59
+ end
60
+
61
+ class BugTest < Minitest::Test
62
+ def test_ransackers
63
+ sql = Project.ransack({ number_present: 1 }).result.to_sql
64
+ puts sql
65
+ assert_equal "SELECT \"projects\".* FROM \"projects\" WHERE (\"projects\".\"number\" IS NOT NULL AND \"projects\".\"number\" != '')", sql
66
+
67
+ sql = Project.ransack({ name_present: 1 }).result.to_sql
68
+ puts sql
69
+ assert_equal "SELECT \"projects\".* FROM \"projects\" WHERE (projects.name IS NOT NULL AND projects.name != '')", sql
70
+ end
71
+ end