ransack 2.1.1 → 2.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) 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/.gitignore +1 -0
  8. data/.rubocop.yml +44 -0
  9. data/CHANGELOG.md +55 -1
  10. data/CONTRIBUTING.md +13 -11
  11. data/Gemfile +23 -17
  12. data/README.md +119 -52
  13. data/bug_report_templates/test-ransack-scope-and-column-same-name.rb +78 -0
  14. data/bug_report_templates/test-ransacker-arel-present-predicate.rb +71 -0
  15. data/docs/img/create_release.png +0 -0
  16. data/docs/release_process.md +20 -0
  17. data/lib/polyamorous/{activerecord_5.2.1_ruby_2 → activerecord_5.2_ruby_2}/join_association.rb +2 -9
  18. data/lib/polyamorous/{activerecord_5.2.1_ruby_2 → activerecord_5.2_ruby_2}/join_dependency.rb +25 -3
  19. data/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb +11 -0
  20. data/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +1 -0
  21. data/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +80 -0
  22. data/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +1 -0
  23. data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +74 -0
  24. data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +93 -0
  25. data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +1 -0
  26. data/lib/polyamorous/activerecord_6.2_ruby_2/join_association.rb +1 -0
  27. data/lib/polyamorous/activerecord_6.2_ruby_2/join_dependency.rb +1 -0
  28. data/lib/polyamorous/activerecord_6.2_ruby_2/reflection.rb +1 -0
  29. data/lib/{polyamorous.rb → polyamorous/polyamorous.rb} +4 -5
  30. data/lib/ransack.rb +3 -3
  31. data/lib/ransack/adapters/active_record/base.rb +4 -0
  32. data/lib/ransack/adapters/active_record/context.rb +67 -68
  33. data/lib/ransack/adapters/active_record/ransack/constants.rb +18 -3
  34. data/lib/ransack/adapters/active_record/ransack/context.rb +2 -6
  35. data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +12 -5
  36. data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -1
  37. data/lib/ransack/configuration.rb +17 -1
  38. data/lib/ransack/constants.rb +3 -5
  39. data/lib/ransack/context.rb +19 -18
  40. data/lib/ransack/helpers.rb +1 -1
  41. data/lib/ransack/helpers/form_builder.rb +8 -14
  42. data/lib/ransack/helpers/form_helper.rb +1 -1
  43. data/lib/ransack/locale/az.yml +1 -1
  44. data/lib/ransack/locale/ca.yml +70 -0
  45. data/lib/ransack/locale/es.yml +22 -22
  46. data/lib/ransack/locale/fa.yml +70 -0
  47. data/lib/ransack/locale/fi.yml +71 -0
  48. data/lib/ransack/locale/sk.yml +70 -0
  49. data/lib/ransack/nodes/attribute.rb +1 -1
  50. data/lib/ransack/nodes/condition.rb +7 -1
  51. data/lib/ransack/nodes/grouping.rb +1 -1
  52. data/lib/ransack/nodes/sort.rb +1 -1
  53. data/lib/ransack/nodes/value.rb +1 -1
  54. data/lib/ransack/predicate.rb +2 -1
  55. data/lib/ransack/search.rb +3 -1
  56. data/lib/ransack/translate.rb +115 -115
  57. data/lib/ransack/version.rb +1 -1
  58. data/ransack.gemspec +8 -23
  59. data/spec/blueprints/articles.rb +1 -1
  60. data/spec/blueprints/comments.rb +1 -1
  61. data/spec/blueprints/notes.rb +1 -1
  62. data/spec/blueprints/tags.rb +1 -1
  63. data/spec/console.rb +5 -5
  64. data/spec/helpers/polyamorous_helper.rb +3 -8
  65. data/spec/helpers/ransack_helper.rb +1 -1
  66. data/spec/{ransack → polyamorous}/join_association_spec.rb +7 -0
  67. data/spec/{ransack → polyamorous}/join_dependency_spec.rb +18 -7
  68. data/spec/{ransack → polyamorous}/join_spec.rb +0 -0
  69. data/spec/ransack/adapters/active_record/base_spec.rb +9 -6
  70. data/spec/ransack/adapters/active_record/context_spec.rb +60 -18
  71. data/spec/ransack/configuration_spec.rb +10 -0
  72. data/spec/ransack/helpers/form_helper_spec.rb +16 -16
  73. data/spec/ransack/nodes/grouping_spec.rb +2 -2
  74. data/spec/ransack/predicate_spec.rb +54 -2
  75. data/spec/ransack/search_spec.rb +127 -15
  76. data/spec/spec_helper.rb +10 -5
  77. data/spec/support/schema.rb +14 -3
  78. metadata +41 -137
  79. data/.travis.yml +0 -37
  80. data/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +0 -2
  81. data/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +0 -2
  82. data/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +0 -32
  83. data/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +0 -112
  84. data/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb +0 -32
  85. data/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +0 -113
data/Gemfile CHANGED
@@ -3,45 +3,51 @@ gemspec
3
3
 
4
4
  gem 'rake'
5
5
 
6
- rails = ENV['RAILS'] || '5-0-stable'
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
- git 'git://github.com/rails/rails.git', :tag => rails do
31
+ git 'https://github.com/rails/rails.git', :tag => rails do
20
32
  gem 'activesupport'
21
33
  gem 'activemodel'
22
34
  gem 'activerecord', require: false
23
35
  gem 'actionpack'
24
36
  end
25
- if rails >= 'v5.2.0'
26
- gem 'mysql2', '~> 0.4.4'
27
- end
28
37
  else
29
- git 'git://github.com/rails/rails.git', :branch => rails do
38
+ git 'https://github.com/rails/rails.git', :branch => rails do
30
39
  gem 'activesupport'
31
40
  gem 'activemodel'
32
41
  gem 'activerecord', require: false
33
42
  gem 'actionpack'
34
43
  end
35
- if rails == '3-0-stable'
36
- gem 'mysql2', '< 0.3'
37
- end
38
- if rails == '5-2-stable'
39
- gem 'mysql2', '~> 0.4.4'
40
- end
41
44
  end
45
+ gem 'mysql2', '~> 0.5.2'
42
46
 
43
47
  group :test do
44
- # TestUnit was removed from Ruby 2.2 but still needed for testing Rails 3.x.
45
- gem 'test-unit', '~> 3.0' if RUBY_VERSION >= '2.2'
48
+ gem 'machinist', '~> 1.0.6'
49
+ gem 'rspec', '~> 3'
46
50
  gem 'simplecov', :require => false
47
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,21 +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
18
  ## Getting started
34
19
 
35
- Ransack is compatible with Rails 5.0, 5.1 and 5.2 on Ruby 2.2 and later.
36
- If you are using Rails <5.0 use the 1.8 line of Ransack.
37
- If you are using Ruby 1.8 or an earlier JRuby and run into compatibility
38
- issues, you can use an earlier version of Ransack, say, up to 1.3.0.
39
-
40
- Ransack works out-of-the-box with Active Record and also features limited
41
- support for Mongoid 4 and 5 (without associations, further details
42
- [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.
43
21
 
44
22
  In your Gemfile, for the last officially released gem:
45
23
 
@@ -63,27 +41,14 @@ gem 'ransack', github: 'activerecord-hackery/ransack'
63
41
 
64
42
  ## Usage
65
43
 
66
- 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.
67
47
 
68
- ### Simple Mode
48
+ If you're coming from MetaSearch (Ransack's predecessor), refer to the
49
+ [Updating From MetaSearch](#updating-from-metasearch) section
69
50
 
70
- This mode works much like MetaSearch, for those of you who are familiar with
71
- it, and requires very little setup effort.
72
-
73
- If you're coming from MetaSearch, things to note:
74
-
75
- 1. The default param key for search params is now `:q`, instead of `:search`.
76
- This is primarily to shorten query strings, though advanced queries (below)
77
- will still run afoul of URL length limits in most browsers and require a
78
- switch to HTTP POST requests. This key is [configurable](https://github.com/activerecord-hackery/ransack/wiki/Configuration).
79
-
80
- 2. `form_for` is now `search_form_for`, and validates that a Ransack::Search
81
- object is passed to it.
82
-
83
- 3. Common ActiveRecord::Relation methods are no longer delegated by the
84
- search object. Instead, you will get your search results (an
85
- ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to
86
- `Ransack#result`.
51
+ ### Simple Mode
87
52
 
88
53
  #### In your controller
89
54
 
@@ -106,6 +71,20 @@ def index
106
71
  end
107
72
  ```
108
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
+
109
88
  #### In your view
110
89
 
111
90
  The two primary Ransack view helpers are `search_form_for` and `sort_link`,
@@ -272,6 +251,20 @@ the order indicator arrow by passing `hide_indicator: true` in the sort link:
272
251
  default_order: { last_name: 'asc', first_name: 'desc' }) %>
273
252
  ```
274
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
+
275
268
  ### Advanced Mode
276
269
 
277
270
  "Advanced" searches (ab)use Rails' nested attributes functionality in order to
@@ -459,13 +452,10 @@ List of all possible predicates
459
452
  | `*_lteq_any` | Less than or equal to any | |
460
453
  | `*_gt_any` | Greater than any | |
461
454
  | `*_gteq_any` | Greater than or equal to any | |
462
- | `*_matches_any` | `*_does_not_match_any` | same as above but with `LIKE` |
463
455
  | `*_lt_all` | Less than all | SQL: `col < value1 AND col < value2` |
464
456
  | `*_lteq_all` | Less than or equal to all | |
465
457
  | `*_gt_all` | Greater than all | |
466
458
  | `*_gteq_all` | Greater than or equal to all | |
467
- | `*_matches_all` | Matches all | same as above but with `LIKE` |
468
- | `*_does_not_match_all` | Does not match all | |
469
459
  | `*_not_eq_all` | none of values in a set | |
470
460
  | `*_start` | Starts with | SQL: `col LIKE 'value%'` |
471
461
  | `*_not_start` | Does not start with | |
@@ -485,6 +475,12 @@ List of all possible predicates
485
475
  | `*_not_cont` | Does not contain |
486
476
  | `*_not_cont_any` | Does not contain any of | |
487
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 | |
488
484
  | `*_true` | is true | |
489
485
  | `*_false` | is false | |
490
486
 
@@ -689,6 +685,43 @@ Trying it out in `rails console`:
689
685
 
690
686
  That's it! Now you know how to whitelist/blacklist various elements in Ransack.
691
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
+
692
725
  ### Using Scopes/Class Methods
693
726
 
694
727
  Continuing on from the preceding section, searching by scopes requires defining
@@ -717,8 +750,6 @@ class Employee < ActiveRecord::Base
717
750
  %i(activated hired_since)
718
751
  end
719
752
  end
720
-
721
- private_class_method :ransackable_scopes
722
753
  end
723
754
 
724
755
  Employee.ransack({ activated: true, hired_since: '2013-01-01' })
@@ -753,7 +784,7 @@ To turn this off on a per-scope basis Ransack adds the following method to
753
784
  Add the scope you wish to bypass this behavior to ransackable_scopes_skip_sanitize_args:
754
785
 
755
786
  ```ruby
756
- def ransackable_scopes_skip_sanitize_args
787
+ def self.ransackable_scopes_skip_sanitize_args
757
788
  [:scope_to_skip_sanitize_args]
758
789
  end
759
790
  ```
@@ -887,6 +918,28 @@ en:
887
918
  title: Old Ransack Namespaced Title
888
919
  ```
889
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
+
890
943
  ## Mongoid
891
944
 
892
945
  Mongoid support has been moved to its own gem at [ransack-mongoid](https://github.com/activerecord-hackery/ransack-mongoid).
@@ -924,6 +977,7 @@ In other words: `Major.Minor.Patch`.
924
977
 
925
978
  To support the project:
926
979
 
980
+ * Consider supporting via [Open Collective](https://opencollective.com/ransack/backers/badge.svg)
927
981
  * Use Ransack in your apps, and let us know if you encounter anything that's
928
982
  broken or missing. A failing spec to demonstrate the issue is awesome. A pull
929
983
  request with passing tests is even better!
@@ -939,6 +993,21 @@ fix bugs!
939
993
 
940
994
  This project exists thanks to all the people who contribute. <img src="https://opencollective.com/ransack/contributors.svg?width=890&button=false" />
941
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
+
942
1011
 
943
1012
  ## Backers
944
1013
 
@@ -961,5 +1030,3 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
961
1030
  <a href="https://opencollective.com/ransack/sponsor/7/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/7/avatar.svg"></a>
962
1031
  <a href="https://opencollective.com/ransack/sponsor/8/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/8/avatar.svg"></a>
963
1032
  <a href="https://opencollective.com/ransack/sponsor/9/website" target="_blank"><img src="https://opencollective.com/ransack/sponsor/9/avatar.svg"></a>
964
-
965
-
@@ -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