ransack 2.1.1 → 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) 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/test.yml +120 -0
  5. data/.gitignore +1 -0
  6. data/CHANGELOG.md +54 -1
  7. data/CONTRIBUTING.md +11 -9
  8. data/Gemfile +21 -17
  9. data/README.md +119 -52
  10. data/lib/polyamorous/{activerecord_5.2.1_ruby_2 → activerecord_5.2_ruby_2}/join_association.rb +2 -9
  11. data/lib/polyamorous/{activerecord_5.2.1_ruby_2 → activerecord_5.2_ruby_2}/join_dependency.rb +25 -3
  12. data/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb +11 -0
  13. data/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +1 -0
  14. data/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +80 -0
  15. data/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +1 -0
  16. data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +74 -0
  17. data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +93 -0
  18. data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +1 -0
  19. data/lib/{polyamorous.rb → polyamorous/polyamorous.rb} +3 -4
  20. data/lib/ransack.rb +2 -2
  21. data/lib/ransack/adapters/active_record/base.rb +4 -0
  22. data/lib/ransack/adapters/active_record/context.rb +67 -68
  23. data/lib/ransack/adapters/active_record/ransack/constants.rb +17 -2
  24. data/lib/ransack/adapters/active_record/ransack/context.rb +2 -6
  25. data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +12 -5
  26. data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -1
  27. data/lib/ransack/configuration.rb +17 -1
  28. data/lib/ransack/constants.rb +2 -3
  29. data/lib/ransack/context.rb +19 -18
  30. data/lib/ransack/helpers/form_builder.rb +5 -11
  31. data/lib/ransack/helpers/form_helper.rb +1 -1
  32. data/lib/ransack/locale/az.yml +1 -1
  33. data/lib/ransack/locale/ca.yml +70 -0
  34. data/lib/ransack/locale/es.yml +22 -22
  35. data/lib/ransack/locale/fa.yml +70 -0
  36. data/lib/ransack/locale/fi.yml +71 -0
  37. data/lib/ransack/locale/sk.yml +70 -0
  38. data/lib/ransack/nodes/condition.rb +8 -0
  39. data/lib/ransack/nodes/grouping.rb +1 -1
  40. data/lib/ransack/predicate.rb +2 -1
  41. data/lib/ransack/search.rb +3 -1
  42. data/lib/ransack/translate.rb +115 -115
  43. data/lib/ransack/version.rb +1 -1
  44. data/ransack.gemspec +5 -21
  45. data/spec/helpers/polyamorous_helper.rb +3 -8
  46. data/spec/{ransack → polyamorous}/join_association_spec.rb +7 -0
  47. data/spec/{ransack → polyamorous}/join_dependency_spec.rb +18 -7
  48. data/spec/{ransack → polyamorous}/join_spec.rb +0 -0
  49. data/spec/ransack/adapters/active_record/base_spec.rb +7 -3
  50. data/spec/ransack/adapters/active_record/context_spec.rb +60 -17
  51. data/spec/ransack/configuration_spec.rb +10 -0
  52. data/spec/ransack/predicate_spec.rb +54 -2
  53. data/spec/ransack/search_spec.rb +127 -15
  54. data/spec/spec_helper.rb +4 -0
  55. data/spec/support/schema.rb +14 -1
  56. metadata +31 -137
  57. data/.travis.yml +0 -37
  58. data/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +0 -2
  59. data/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +0 -2
  60. data/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +0 -32
  61. data/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +0 -112
  62. data/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb +0 -32
  63. data/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +0 -113
@@ -1,3 +1,3 @@
1
1
  module Ransack
2
- VERSION = '2.1.1'
2
+ VERSION = '2.4.1'
3
3
  end
@@ -11,31 +11,15 @@ Gem::Specification.new do |s|
11
11
  s.homepage = "https://github.com/activerecord-hackery/ransack"
12
12
  s.summary = %q{Object-based searching for Active Record and Mongoid (currently).}
13
13
  s.description = %q{Ransack is the successor to the MetaSearch gem. It improves and expands upon MetaSearch's functionality, but does not have a 100%-compatible API.}
14
- s.required_ruby_version = '>= 1.9'
14
+ s.required_ruby_version = '>= 2.3'
15
15
  s.license = 'MIT'
16
16
 
17
- s.rubyforge_project = "ransack"
18
-
19
- s.add_dependency 'actionpack', '>= 5.0'
20
- s.add_dependency 'activerecord', '>= 5.0'
21
- s.add_dependency 'activesupport', '>= 5.0'
17
+ s.add_dependency 'activerecord', '>= 5.2.4'
18
+ s.add_dependency 'activesupport', '>= 5.2.4'
22
19
  s.add_dependency 'i18n'
23
- s.add_development_dependency 'rspec', '~> 3'
24
- s.add_development_dependency 'machinist', '~> 1.0.6'
25
- s.add_development_dependency 'faker', '~> 0.9.5'
26
- s.add_development_dependency 'sqlite3', '~> 1.3.3'
27
- s.add_development_dependency 'pg', '~> 0.21'
28
- s.add_development_dependency 'mysql2', '0.3.20'
29
- s.add_development_dependency 'pry', '0.10'
30
20
 
31
21
  s.files = `git ls-files`.split("\n")
32
-
33
- s.test_files = `git ls-files -- {test,spec,features}/*`
34
- .split("\n")
35
-
36
- s.executables = `git ls-files -- bin/*`
37
- .split("\n")
38
- .map { |f| File.basename(f) }
39
-
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
40
24
  s.require_paths = ["lib"]
41
25
  end
@@ -3,18 +3,13 @@ module PolyamorousHelper
3
3
  Polyamorous::JoinAssociation.new reflection, children, klass
4
4
  end
5
5
 
6
- if ActiveRecord::VERSION::STRING > "5.2.0"
6
+ if ActiveRecord.version >= ::Gem::Version.new("6.0.0.rc1")
7
7
  def new_join_dependency(klass, associations = {})
8
- Polyamorous::JoinDependency.new klass, klass.arel_table, associations
9
- end
10
- elsif ActiveRecord::VERSION::STRING == "5.2.0"
11
- def new_join_dependency(klass, associations = {})
12
- alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(klass.connection, klass.table_name, [])
13
- Polyamorous::JoinDependency.new klass, klass.arel_table, associations, alias_tracker
8
+ Polyamorous::JoinDependency.new klass, klass.arel_table, associations, Polyamorous::InnerJoin
14
9
  end
15
10
  else
16
11
  def new_join_dependency(klass, associations = {})
17
- Polyamorous::JoinDependency.new klass, associations, []
12
+ Polyamorous::JoinDependency.new klass, klass.arel_table, associations
18
13
  end
19
14
  end
20
15
 
@@ -10,6 +10,13 @@ module Polyamorous
10
10
  new_join_association(reflection, parent.children, Article)
11
11
  }
12
12
 
13
+ subject { new_join_association(reflection, parent.children, Person) }
14
+
15
+ it 'respects polymorphism on equality test' do
16
+ expect(subject).to eq new_join_association(reflection, parent.children, Person)
17
+ expect(subject).not_to eq new_join_association(reflection, parent.children, Article)
18
+ end
19
+
13
20
  it 'leaves the orginal reflection intact for thread safety' do
14
21
  reflection.instance_variable_set(:@klass, Article)
15
22
  join_association
@@ -8,8 +8,8 @@ module Polyamorous
8
8
 
9
9
  specify { expect(subject.send(:join_root).drop(1).size)
10
10
  .to eq(2) }
11
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type))
12
- .to be_all { Polyamorous::InnerJoin } }
11
+ specify { expect(subject.send(:join_root).drop(1).map(&:join_type).uniq)
12
+ .to eq [Polyamorous::InnerJoin] }
13
13
  end
14
14
 
15
15
  context 'with has_many :through association' do
@@ -38,8 +38,8 @@ module Polyamorous
38
38
  .to eq 2 }
39
39
  specify { expect(subject.send(:join_root).drop(1).map(&:join_type))
40
40
  .to eq [Polyamorous::OuterJoin, Polyamorous::OuterJoin] }
41
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type))
42
- .to be_all { Polyamorous::OuterJoin } }
41
+ specify { expect(subject.send(:join_root).drop(1).map(&:join_type).uniq)
42
+ .to eq [Polyamorous::OuterJoin] }
43
43
  end
44
44
 
45
45
  context 'with polymorphic belongs_to join' do
@@ -59,8 +59,19 @@ module Polyamorous
59
59
 
60
60
  specify { expect(subject.send(:join_root).drop(1).size)
61
61
  .to eq 2 }
62
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type))
63
- .to be_all { Polyamorous::InnerJoin } }
62
+ specify { expect(subject.send(:join_root).drop(1).map(&:join_type).uniq)
63
+ .to eq [Polyamorous::InnerJoin] }
64
+ specify { expect(subject.send(:join_root).drop(1).first.table_name)
65
+ .to eq 'people' }
66
+ specify { expect(subject.send(:join_root).drop(1)[1].table_name)
67
+ .to eq 'comments' }
68
+ end
69
+
70
+ context 'with polymorphic belongs_to join and nested join' do
71
+ subject { new_join_dependency Note,
72
+ new_join(:notable, :outer, Person) => :comments }
73
+ specify { expect(subject.send(:join_root).drop(1).size).to eq 2 }
74
+ specify { expect(subject.send(:join_root).drop(1).map(&:join_type)).to eq [Polyamorous::OuterJoin, Polyamorous::InnerJoin] }
64
75
  specify { expect(subject.send(:join_root).drop(1).first.table_name)
65
76
  .to eq 'people' }
66
77
  specify { expect(subject.send(:join_root).drop(1)[1].table_name)
@@ -68,7 +79,7 @@ module Polyamorous
68
79
  end
69
80
 
70
81
  context '#left_outer_join in Rails 5 overrides join type specified',
71
- if: ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR < 2 do
82
+ if: ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MAJOR < 6 && ActiveRecord::VERSION::MINOR < 2 do
72
83
 
73
84
  let(:join_type_class) do
74
85
  new_join_dependency(
@@ -122,6 +122,10 @@ module Ransack
122
122
  expect { Person.ransack('') }.to_not raise_error
123
123
  end
124
124
 
125
+ it 'raises exception if ransack! called with unknown condition' do
126
+ expect { Person.ransack!(unknown_attr_eq: 'Ernie') }.to raise_error
127
+ end
128
+
125
129
  it 'does not modify the parameters' do
126
130
  params = { name_eq: '' }
127
131
  expect { Person.ransack(params) }.not_to change { params }
@@ -143,14 +147,12 @@ module Ransack
143
147
  it 'removes redundant joins from top query' do
144
148
  s = Article.ransack(tags_name_not_eq: "Fantasy")
145
149
  sql = s.result.to_sql
146
-
147
150
  expect(sql).to_not include('LEFT OUTER JOIN')
148
151
  end
149
152
 
150
153
  it 'handles != for single values' do
151
154
  s = Article.ransack(tags_name_not_eq: "Fantasy")
152
155
  articles = s.result.to_a
153
-
154
156
  expect(articles).to include marco
155
157
  expect(articles).to_not include arthur
156
158
  end
@@ -267,10 +269,12 @@ module Ransack
267
269
  # end
268
270
 
269
271
  it 'creates ransack attributes' do
272
+ person = Person.create!(name: 'Aric Smith')
273
+
270
274
  s = Person.ransack(reversed_name_eq: 'htimS cirA')
271
275
  expect(s.result.size).to eq(1)
272
276
 
273
- expect(s.result.first).to eq Person.where(name: 'Aric Smith').first
277
+ expect(s.result.first).to eq person
274
278
  end
275
279
 
276
280
  it 'can be accessed through associations' do
@@ -40,6 +40,66 @@ module Ransack
40
40
  end
41
41
  end
42
42
 
43
+ describe '#build_correlated_subquery' do
44
+ it 'build correlated subquery for Root STI model' do
45
+ search = Search.new(Person, { articles_title_not_eq: 'some_title' }, context: subject)
46
+ attribute = search.conditions.first.attributes.first
47
+ constraints = subject.build_correlated_subquery(attribute.parent).constraints
48
+ constraint = constraints.first
49
+
50
+ expect(constraints.length).to eql 1
51
+ expect(constraint.left.name).to eql 'person_id'
52
+ expect(constraint.left.relation.name).to eql 'articles'
53
+ expect(constraint.right.name).to eql 'id'
54
+ expect(constraint.right.relation.name).to eql 'people'
55
+ end
56
+
57
+ it 'build correlated subquery for Child STI model when predicate is not_eq' do
58
+ search = Search.new(Person, { story_articles_title_not_eq: 'some_title' }, context: subject)
59
+ attribute = search.conditions.first.attributes.first
60
+ constraints = subject.build_correlated_subquery(attribute.parent).constraints
61
+ constraint = constraints.first
62
+
63
+ expect(constraints.length).to eql 1
64
+ expect(constraint.left.relation.name).to eql 'articles'
65
+ expect(constraint.left.name).to eql 'person_id'
66
+ expect(constraint.right.relation.name).to eql 'people'
67
+ expect(constraint.right.name).to eql 'id'
68
+ end
69
+
70
+ it 'build correlated subquery for Child STI model when predicate is eq' do
71
+ search = Search.new(Person, { story_articles_title_not_eq: 'some_title' }, context: subject)
72
+ attribute = search.conditions.first.attributes.first
73
+ constraints = subject.build_correlated_subquery(attribute.parent).constraints
74
+ constraint = constraints.first
75
+
76
+ expect(constraints.length).to eql 1
77
+ expect(constraint.left.relation.name).to eql 'articles'
78
+ expect(constraint.left.name).to eql 'person_id'
79
+ expect(constraint.right.relation.name).to eql 'people'
80
+ expect(constraint.right.name).to eql 'id'
81
+ end
82
+
83
+ it 'build correlated subquery for multiple conditions (default scope)' do
84
+ search = Search.new(Person, { comments_body_not_eq: 'some_title'})
85
+
86
+ # Was
87
+ # SELECT "people".* FROM "people" WHERE "people"."id" NOT IN (
88
+ # SELECT "comments"."disabled" FROM "comments"
89
+ # WHERE "comments"."disabled" = "people"."id"
90
+ # AND NOT ("comments"."body" != 'some_title')
91
+ # ) ORDER BY "people"."id" DESC
92
+ # Should Be
93
+ # SELECT "people".* FROM "people" WHERE "people"."id" NOT IN (
94
+ # SELECT "comments"."person_id" FROM "comments"
95
+ # WHERE "comments"."person_id" = "people"."id"
96
+ # AND NOT ("comments"."body" != 'some_title')
97
+ # ) ORDER BY "people"."id" DESC
98
+
99
+ expect(search.result.to_sql).to match /.comments.\..person_id. = .people.\..id./
100
+ end
101
+ end
102
+
43
103
  describe 'sharing context across searches' do
44
104
  let(:shared_context) { Context.for(Person) }
45
105
 
@@ -50,23 +110,6 @@ module Ransack
50
110
  context: shared_context)
51
111
  end
52
112
 
53
- describe '#join_associations', if: AR_version <= '4.0' do
54
- it 'returns dependent join associations for all searches run
55
- against the context' do
56
- parents, children = shared_context.join_associations
57
-
58
- expect(children.aliased_table_name).to eq "children_people"
59
- expect(parents.aliased_table_name).to eq "parents_people"
60
- end
61
-
62
- it 'can be rejoined to execute a valid query' do
63
- parents, children = shared_context.join_associations
64
-
65
- expect { Person.joins(parents).joins(children).to_a }
66
- .to_not raise_error
67
- end
68
- end
69
-
70
113
  describe '#join_sources' do
71
114
  it 'returns dependent arel join nodes for all searches run against
72
115
  the context' do
@@ -173,5 +173,15 @@ module Ransack
173
173
  .to eq false
174
174
  end
175
175
  end
176
+
177
+ it "PG's sort option", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
178
+ default = Ransack.options.clone
179
+
180
+ Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_first }
181
+
182
+ expect(Ransack.options[:postgres_fields_sort_option]).to eq :nulls_first
183
+
184
+ Ransack.options = default
185
+ end
176
186
  end
177
187
  end
@@ -126,7 +126,7 @@ module Ransack
126
126
  (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
127
127
  /"people"."name" ILIKE '%\\%\\.\\_\\\\%'/
128
128
  elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
129
- /`people`.`name` LIKE '%\\\\%\\\\.\\\\_\\\\\\\\%'/
129
+ /`people`.`name` LIKE '%\\\\%.\\\\_\\\\\\\\%'/
130
130
  else
131
131
  /"people"."name" LIKE '%%._\\%'/
132
132
  end) do
@@ -145,7 +145,7 @@ module Ransack
145
145
  (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
146
146
  /"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/
147
147
  elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
148
- /`people`.`name` NOT LIKE '%\\\\%\\\\.\\\\_\\\\\\\\%'/
148
+ /`people`.`name` NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/
149
149
  else
150
150
  /"people"."name" NOT LIKE '%%._\\%'/
151
151
  end) do
@@ -159,6 +159,44 @@ module Ransack
159
159
  end
160
160
  end
161
161
 
162
+ describe 'i_cont' do
163
+ it_has_behavior 'wildcard escaping', :name_i_cont,
164
+ (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
165
+ /"people"."name" ILIKE '%\\%\\.\\_\\\\%'/
166
+ elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
167
+ /LOWER\(`people`.`name`\) LIKE '%\\\\%.\\\\_\\\\\\\\%'/
168
+ else
169
+ /LOWER\("people"."name"\) LIKE '%%._\\%'/
170
+ end) do
171
+ subject { @s }
172
+ end
173
+
174
+ it 'generates a LIKE query with LOWER(column) and value surrounded by %' do
175
+ @s.name_i_cont = 'Ric'
176
+ field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
177
+ expect(@s.result.to_sql).to match /[LOWER\(]?#{field}\)? I?LIKE '%ric%'/
178
+ end
179
+ end
180
+
181
+ describe 'not_i_cont' do
182
+ it_has_behavior 'wildcard escaping', :name_not_i_cont,
183
+ (if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
184
+ /"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/
185
+ elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
186
+ /LOWER\(`people`.`name`\) NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/
187
+ else
188
+ /LOWER\("people"."name"\) NOT LIKE '%%._\\%'/
189
+ end) do
190
+ subject { @s }
191
+ end
192
+
193
+ it 'generates a NOT LIKE query with LOWER(column) and value surrounded by %' do
194
+ @s.name_not_i_cont = 'Ric'
195
+ field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
196
+ expect(@s.result.to_sql).to match /[LOWER\(]?#{field}\)? NOT I?LIKE '%ric%'/
197
+ end
198
+ end
199
+
162
200
  describe 'start' do
163
201
  it 'generates a LIKE query with value followed by %' do
164
202
  @s.name_start = 'Er'
@@ -381,6 +419,20 @@ module Ransack
381
419
  end
382
420
  end
383
421
 
422
+ context "defining custom predicates" do
423
+ describe "with 'not_in' arel predicate" do
424
+ before do
425
+ Ransack.configure {|c| c.add_predicate "not_in_csv", arel_predicate: "not_in", formatter: proc { |v| v.split(",") } }
426
+ end
427
+
428
+ it 'generates a value IS NOT NULL query' do
429
+ @s.name_not_in_csv = ["a", "b"]
430
+ field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
431
+ expect(@s.result.to_sql).to match /#{field} NOT IN \('a', 'b'\)/
432
+ end
433
+ end
434
+ end
435
+
384
436
  private
385
437
 
386
438
  def test_boolean_equality_for(boolean_value)
@@ -20,6 +20,12 @@ module Ransack
20
20
  Search.new(Person, name_eq: 'foobar')
21
21
  end
22
22
 
23
+ it 'strip leading & trailing whitespace before building' do
24
+ expect_any_instance_of(Search).to receive(:build)
25
+ .with({ 'name_eq' => 'foobar' })
26
+ Search.new(Person, name_eq: ' foobar ')
27
+ end
28
+
23
29
  it 'removes empty suffixed conditions before building' do
24
30
  expect_any_instance_of(Search).to receive(:build).with({})
25
31
  Search.new(Person, name_eq_any: [''])
@@ -109,6 +115,43 @@ module Ransack
109
115
  expect(s.result.to_sql).to include 'published'
110
116
  end
111
117
 
118
+ # The failure/oversight in Ransack::Nodes::Condition#arel_predicate or deeper is beyond my understanding of the structures
119
+ it 'preserves (inverts) default scope and conditions for negative subqueries' do
120
+ # the positive case (published_articles_title_eq) is
121
+ # SELECT "people".* FROM "people"
122
+ # LEFT OUTER JOIN "articles" ON "articles"."person_id" = "people"."id"
123
+ # AND "articles"."published" = 't'
124
+ # AND ('default_scope' = 'default_scope')
125
+ # WHERE "articles"."title" = 'Test' ORDER BY "people"."id" DESC
126
+ #
127
+ # negative case was
128
+ # SELECT "people".* FROM "people" WHERE "people"."id" NOT IN (
129
+ # SELECT "articles"."person_id" FROM "articles"
130
+ # WHERE "articles"."person_id" = "people"."id"
131
+ # AND NOT ("articles"."title" != 'Test')
132
+ # ) ORDER BY "people"."id" DESC
133
+ #
134
+ # Should have been like
135
+ # SELECT "people".* FROM "people" WHERE "people"."id" NOT IN (
136
+ # SELECT "articles"."person_id" FROM "articles"
137
+ # WHERE "articles"."person_id" = "people"."id"
138
+ # AND "articles"."title" = 'Test' AND "articles"."published" = 't' AND ('default_scope' = 'default_scope')
139
+ # ) ORDER BY "people"."id" DESC
140
+ #
141
+ # With tenanting (eg default_scope with column reference), NOT IN should be like
142
+ # SELECT "people".* FROM "people" WHERE "people"."tenant_id" = 'tenant_id' AND "people"."id" NOT IN (
143
+ # SELECT "articles"."person_id" FROM "articles"
144
+ # WHERE "articles"."person_id" = "people"."id"
145
+ # AND "articles"."tenant_id" = 'tenant_id'
146
+ # AND "articles"."title" = 'Test' AND "articles"."published" = 't' AND ('default_scope' = 'default_scope')
147
+ # ) ORDER BY "people"."id" DESC
148
+
149
+ pending("spec should pass, but I do not know how/where to fix lib code")
150
+ s = Search.new(Person, published_articles_title_not_eq: 'Test')
151
+ expect(s.result.to_sql).to include 'default_scope'
152
+ expect(s.result.to_sql).to include 'published'
153
+ end
154
+
112
155
  it 'discards empty conditions' do
113
156
  s = Search.new(Person, children_name_eq: '')
114
157
  condition = s.base[:children_name_eq]
@@ -189,7 +232,7 @@ module Ransack
189
232
  context 'with an invalid condition' do
190
233
  subject { Search.new(Person, unknown_attr_eq: 'Ernie') }
191
234
 
192
- context 'when ignore_unknown_conditions is false' do
235
+ context 'when ignore_unknown_conditions configuration option is false' do
193
236
  before do
194
237
  Ransack.configure { |c| c.ignore_unknown_conditions = false }
195
238
  end
@@ -197,13 +240,39 @@ module Ransack
197
240
  specify { expect { subject }.to raise_error ArgumentError }
198
241
  end
199
242
 
200
- context 'when ignore_unknown_conditions is true' do
243
+ context 'when ignore_unknown_conditions configuration option is true' do
201
244
  before do
202
245
  Ransack.configure { |c| c.ignore_unknown_conditions = true }
203
246
  end
204
247
 
205
248
  specify { expect { subject }.not_to raise_error }
206
249
  end
250
+
251
+ subject(:with_ignore_unknown_conditions_false) {
252
+ Search.new(Person,
253
+ { unknown_attr_eq: 'Ernie' },
254
+ { ignore_unknown_conditions: false }
255
+ )
256
+ }
257
+
258
+ subject(:with_ignore_unknown_conditions_true) {
259
+ Search.new(Person,
260
+ { unknown_attr_eq: 'Ernie' },
261
+ { ignore_unknown_conditions: true }
262
+ )
263
+ }
264
+
265
+ context 'when ignore_unknown_conditions search parameter is absent' do
266
+ specify { expect { subject }.not_to raise_error }
267
+ end
268
+
269
+ context 'when ignore_unknown_conditions search parameter is false' do
270
+ specify { expect { with_ignore_unknown_conditions_false }.to raise_error ArgumentError }
271
+ end
272
+
273
+ context 'when ignore_unknown_conditions search parameter is true' do
274
+ specify { expect { with_ignore_unknown_conditions_true }.not_to raise_error }
275
+ end
207
276
  end
208
277
 
209
278
  it 'does not modify the parameters' do
@@ -220,6 +289,9 @@ module Ransack
220
289
  let(:children_people_name_field) {
221
290
  "#{quote_table_name("children_people")}.#{quote_column_name("name")}"
222
291
  }
292
+ let(:notable_type_field) {
293
+ "#{quote_table_name("notes")}.#{quote_column_name("notable_type")}"
294
+ }
223
295
  it 'evaluates conditions contextually' do
224
296
  s = Search.new(Person, children_name_eq: 'Ernie')
225
297
  expect(s.result).to be_an ActiveRecord::Relation
@@ -227,13 +299,34 @@ module Ransack
227
299
  children_people_name_field} = 'Ernie'/
228
300
  end
229
301
 
230
- # FIXME: Make this spec pass for Rails 4.1 / 4.2 / 5.0 and not just 4.0 by
231
- # commenting out lines 221 and 242 to run the test. Addresses issue #374.
232
- # https://github.com/activerecord-hackery/ransack/issues/374
233
- #
234
- it 'evaluates conditions for multiple `belongs_to` associations to the
235
- same table contextually' do
236
- skip "Make this spec pass for Rails >5.0"
302
+ it 'use appropriate table alias' do
303
+ skip "Rails 6 regressed here, but it's fixed in 6-0-stable since https://github.com/rails/rails/commit/f9ba52477ca288e7effa5f6794ae3df3f4e982bc" if ENV["RAILS"] == "v6.0.3"
304
+
305
+ s = Search.new(Person, {
306
+ name_eq: "person_name_query",
307
+ articles_title_eq: "person_article_title_query",
308
+ parent_name_eq: "parent_name_query",
309
+ parent_articles_title_eq: 'parents_article_title_query'
310
+ }).result
311
+
312
+ real_query = remove_quotes_and_backticks(s.to_sql)
313
+
314
+ expect(real_query)
315
+ .to match(%r{LEFT OUTER JOIN articles ON (\('default_scope' = 'default_scope'\) AND )?articles.person_id = people.id})
316
+ expect(real_query)
317
+ .to match(%r{LEFT OUTER JOIN articles articles_people ON (\('default_scope' = 'default_scope'\) AND )?articles_people.person_id = parents_people.id})
318
+
319
+ expect(real_query)
320
+ .to include "people.name = 'person_name_query'"
321
+ expect(real_query)
322
+ .to include "articles.title = 'person_article_title_query'"
323
+ expect(real_query)
324
+ .to include "parents_people.name = 'parent_name_query'"
325
+ expect(real_query)
326
+ .to include "articles_people.title = 'parents_article_title_query'"
327
+ end
328
+
329
+ it 'evaluates conditions for multiple `belongs_to` associations to the same table contextually' do
237
330
  s = Search.new(
238
331
  Recommendation,
239
332
  person_name_eq: 'Ernie',
@@ -248,7 +341,7 @@ module Ransack
248
341
  ON target_people_recommendations.id = recommendations.target_person_id
249
342
  LEFT OUTER JOIN people parents_people
250
343
  ON parents_people.id = target_people_recommendations.parent_id
251
- WHERE ((people.name = 'Ernie' AND parents_people.name = 'Test'))
344
+ WHERE (people.name = 'Ernie' AND parents_people.name = 'Test')
252
345
  SQL
253
346
  .squish
254
347
  expect(real_query).to eq expected_query
@@ -265,6 +358,7 @@ module Ransack
265
358
  s = Search.new(Note, notable_of_Person_type_name_eq: 'Ernie').result
266
359
  expect(s).to be_an ActiveRecord::Relation
267
360
  expect(s.to_sql).to match /#{people_name_field} = 'Ernie'/
361
+ expect(s.to_sql).to match /#{notable_type_field} = 'Person'/
268
362
  end
269
363
 
270
364
  it 'evaluates nested conditions' do
@@ -303,11 +397,8 @@ module Ransack
303
397
  { m: 'or', comments_body_cont: 'e', articles_comments_body_cont: 'e' }
304
398
  ]
305
399
  )
306
- if ActiveRecord::VERSION::MAJOR == 3
307
- all_or_load, uniq_or_distinct = :all, :uniq
308
- else
309
- all_or_load, uniq_or_distinct = :load, :distinct
310
- end
400
+
401
+ all_or_load, uniq_or_distinct = :load, :distinct
311
402
  expect(s.result.send(all_or_load).size)
312
403
  .to eq(9000)
313
404
  expect(s.result(distinct: true).size)
@@ -442,6 +533,27 @@ module Ransack
442
533
  @s.sorts = 'id asc'
443
534
  expect(@s.result.first.id).to eq 1
444
535
  end
536
+
537
+ it "PG's sort option", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
538
+ default = Ransack.options.clone
539
+
540
+ s = Search.new(Person, s: 'name asc')
541
+ expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" ASC"
542
+
543
+ Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_first }
544
+ s = Search.new(Person, s: 'name asc')
545
+ expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" ASC NULLS FIRST"
546
+ s = Search.new(Person, s: 'name desc')
547
+ expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" DESC NULLS LAST"
548
+
549
+ Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_last }
550
+ s = Search.new(Person, s: 'name asc')
551
+ expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" ASC NULLS LAST"
552
+ s = Search.new(Person, s: 'name desc')
553
+ expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" DESC NULLS FIRST"
554
+
555
+ Ransack.options = default
556
+ end
445
557
  end
446
558
 
447
559
  describe '#method_missing' do