active_record_extended 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +22 -7
  3. data/lib/active_record_extended/arel/aggregate_function_name.rb +0 -0
  4. data/lib/active_record_extended/arel/nodes.rb +1 -1
  5. data/lib/active_record_extended/arel/predications.rb +0 -0
  6. data/lib/active_record_extended/arel/{sql_literal.rb → sql_literal_patch.rb} +2 -2
  7. data/lib/active_record_extended/arel/visitors/postgresql_decorator.rb +16 -12
  8. data/lib/active_record_extended/arel.rb +1 -1
  9. data/lib/active_record_extended/patch/array_handler_patch.rb +22 -0
  10. data/lib/active_record_extended/patch/relation_patch.rb +67 -0
  11. data/lib/active_record_extended/patch/where_clause_patch.rb +13 -0
  12. data/lib/active_record_extended/query_methods/any_of.rb +1 -1
  13. data/lib/active_record_extended/query_methods/either.rb +3 -3
  14. data/lib/active_record_extended/query_methods/{select.rb → foster_select.rb} +4 -4
  15. data/lib/active_record_extended/query_methods/inet.rb +0 -0
  16. data/lib/active_record_extended/query_methods/json.rb +2 -2
  17. data/lib/active_record_extended/query_methods/unionize.rb +5 -5
  18. data/lib/active_record_extended/query_methods/where_chain.rb +96 -92
  19. data/lib/active_record_extended/query_methods/window.rb +3 -3
  20. data/lib/active_record_extended/query_methods/with_cte.rb +3 -3
  21. data/lib/active_record_extended/utilities/order_by.rb +1 -1
  22. data/lib/active_record_extended/utilities/support.rb +1 -1
  23. data/lib/active_record_extended/version.rb +1 -1
  24. data/lib/active_record_extended.rb +55 -4
  25. metadata +21 -70
  26. data/lib/active_record_extended/active_record/relation_patch.rb +0 -50
  27. data/lib/active_record_extended/active_record.rb +0 -25
  28. data/lib/active_record_extended/patch/5_1/where_clause.rb +0 -11
  29. data/lib/active_record_extended/patch/5_2/where_clause.rb +0 -11
  30. data/lib/active_record_extended/predicate_builder/array_handler_decorator.rb +0 -20
  31. data/spec/active_record_extended_spec.rb +0 -7
  32. data/spec/query_methods/any_of_spec.rb +0 -131
  33. data/spec/query_methods/array_query_spec.rb +0 -64
  34. data/spec/query_methods/either_spec.rb +0 -70
  35. data/spec/query_methods/hash_query_spec.rb +0 -45
  36. data/spec/query_methods/inet_query_spec.rb +0 -112
  37. data/spec/query_methods/json_spec.rb +0 -157
  38. data/spec/query_methods/select_spec.rb +0 -115
  39. data/spec/query_methods/unionize_spec.rb +0 -165
  40. data/spec/query_methods/window_spec.rb +0 -51
  41. data/spec/query_methods/with_cte_spec.rb +0 -50
  42. data/spec/spec_helper.rb +0 -28
  43. data/spec/sql_inspections/any_of_sql_spec.rb +0 -41
  44. data/spec/sql_inspections/arel/aggregate_function_name_spec.rb +0 -41
  45. data/spec/sql_inspections/arel/array_spec.rb +0 -63
  46. data/spec/sql_inspections/arel/inet_spec.rb +0 -66
  47. data/spec/sql_inspections/contains_sql_queries_spec.rb +0 -47
  48. data/spec/sql_inspections/either_sql_spec.rb +0 -71
  49. data/spec/sql_inspections/json_sql_spec.rb +0 -82
  50. data/spec/sql_inspections/unionize_sql_spec.rb +0 -124
  51. data/spec/sql_inspections/window_sql_spec.rb +0 -98
  52. data/spec/sql_inspections/with_cte_sql_spec.rb +0 -95
  53. data/spec/support/database_cleaner.rb +0 -15
  54. data/spec/support/models.rb +0 -80
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordExtended
4
- VERSION = "2.1.0"
4
+ VERSION = "3.0.0"
5
5
  end
@@ -1,10 +1,61 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_record_extended/version"
4
- require "active_record_extended/utilities/support"
5
- require "active_record_extended/utilities/order_by"
6
- require "active_record_extended/active_record"
7
- require "active_record_extended/arel"
4
+
5
+ require "active_record"
6
+ require "active_record/relation"
7
+ require "active_record/relation/merger"
8
+ require "active_record/relation/query_methods"
8
9
 
9
10
  module ActiveRecordExtended
11
+ extend ActiveSupport::Autoload
12
+
13
+ AR_VERSION_GTE_6_1 = Gem::Requirement.new(">= 6.1").satisfied_by?(ActiveRecord.gem_version)
14
+
15
+ module Utilities
16
+ extend ActiveSupport::Autoload
17
+
18
+ eager_autoload do
19
+ autoload :OrderBy
20
+ autoload :Support
21
+ end
22
+ end
23
+
24
+ module Patch
25
+ extend ActiveSupport::Autoload
26
+
27
+ eager_autoload do
28
+ autoload :ArrayHandlerPatch
29
+ autoload :RelationPatch
30
+ autoload :WhereClausePatch
31
+ end
32
+ end
33
+
34
+ module QueryMethods
35
+ extend ActiveSupport::Autoload
36
+
37
+ eager_autoload do
38
+ autoload :AnyOf
39
+ autoload :Either
40
+ autoload :FosterSelect
41
+ autoload :Inet
42
+ autoload :Json
43
+ autoload :Unionize
44
+ autoload :WhereChain
45
+ autoload :Window
46
+ autoload :WithCTE
47
+ end
48
+ end
49
+
50
+ def self.eager_load!
51
+ super
52
+ ActiveRecordExtended::Utilities.eager_load!
53
+ ActiveRecordExtended::Patch.eager_load!
54
+ ActiveRecordExtended::QueryMethods.eager_load!
55
+ end
56
+ end
57
+
58
+ ActiveSupport.on_load(:active_record) do
59
+ require "active_record_extended/arel"
60
+ ActiveRecordExtended.eager_load!
10
61
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_extended
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Protacio-Karaszi
8
8
  - Dan McClain
9
9
  - Olivier El Mekki
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-01-20 00:00:00.000000000 Z
13
+ date: 2022-08-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: '5.1'
21
+ version: '5.2'
22
22
  - - "<"
23
23
  - !ruby/object:Gem::Version
24
24
  version: 7.1.0
@@ -28,7 +28,7 @@ dependencies:
28
28
  requirements:
29
29
  - - ">="
30
30
  - !ruby/object:Gem::Version
31
- version: '5.1'
31
+ version: '5.2'
32
32
  - - "<"
33
33
  - !ruby/object:Gem::Version
34
34
  version: 7.1.0
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: '1.16'
69
+ version: '2.2'
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
72
  version: '3.0'
@@ -76,7 +76,7 @@ dependencies:
76
76
  requirements:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: '1.16'
79
+ version: '2.2'
80
80
  - - "<"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '1.6'
89
+ version: '2.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '1.6'
96
+ version: '2.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -147,22 +147,20 @@ extra_rdoc_files: []
147
147
  files:
148
148
  - README.md
149
149
  - lib/active_record_extended.rb
150
- - lib/active_record_extended/active_record.rb
151
- - lib/active_record_extended/active_record/relation_patch.rb
152
150
  - lib/active_record_extended/arel.rb
153
151
  - lib/active_record_extended/arel/aggregate_function_name.rb
154
152
  - lib/active_record_extended/arel/nodes.rb
155
153
  - lib/active_record_extended/arel/predications.rb
156
- - lib/active_record_extended/arel/sql_literal.rb
154
+ - lib/active_record_extended/arel/sql_literal_patch.rb
157
155
  - lib/active_record_extended/arel/visitors/postgresql_decorator.rb
158
- - lib/active_record_extended/patch/5_1/where_clause.rb
159
- - lib/active_record_extended/patch/5_2/where_clause.rb
160
- - lib/active_record_extended/predicate_builder/array_handler_decorator.rb
156
+ - lib/active_record_extended/patch/array_handler_patch.rb
157
+ - lib/active_record_extended/patch/relation_patch.rb
158
+ - lib/active_record_extended/patch/where_clause_patch.rb
161
159
  - lib/active_record_extended/query_methods/any_of.rb
162
160
  - lib/active_record_extended/query_methods/either.rb
161
+ - lib/active_record_extended/query_methods/foster_select.rb
163
162
  - lib/active_record_extended/query_methods/inet.rb
164
163
  - lib/active_record_extended/query_methods/json.rb
165
- - lib/active_record_extended/query_methods/select.rb
166
164
  - lib/active_record_extended/query_methods/unionize.rb
167
165
  - lib/active_record_extended/query_methods/where_chain.rb
168
166
  - lib/active_record_extended/query_methods/window.rb
@@ -170,35 +168,12 @@ files:
170
168
  - lib/active_record_extended/utilities/order_by.rb
171
169
  - lib/active_record_extended/utilities/support.rb
172
170
  - lib/active_record_extended/version.rb
173
- - spec/active_record_extended_spec.rb
174
- - spec/query_methods/any_of_spec.rb
175
- - spec/query_methods/array_query_spec.rb
176
- - spec/query_methods/either_spec.rb
177
- - spec/query_methods/hash_query_spec.rb
178
- - spec/query_methods/inet_query_spec.rb
179
- - spec/query_methods/json_spec.rb
180
- - spec/query_methods/select_spec.rb
181
- - spec/query_methods/unionize_spec.rb
182
- - spec/query_methods/window_spec.rb
183
- - spec/query_methods/with_cte_spec.rb
184
- - spec/spec_helper.rb
185
- - spec/sql_inspections/any_of_sql_spec.rb
186
- - spec/sql_inspections/arel/aggregate_function_name_spec.rb
187
- - spec/sql_inspections/arel/array_spec.rb
188
- - spec/sql_inspections/arel/inet_spec.rb
189
- - spec/sql_inspections/contains_sql_queries_spec.rb
190
- - spec/sql_inspections/either_sql_spec.rb
191
- - spec/sql_inspections/json_sql_spec.rb
192
- - spec/sql_inspections/unionize_sql_spec.rb
193
- - spec/sql_inspections/window_sql_spec.rb
194
- - spec/sql_inspections/with_cte_sql_spec.rb
195
- - spec/support/database_cleaner.rb
196
- - spec/support/models.rb
197
171
  homepage: https://github.com/georgekaraszi/ActiveRecordExtended
198
172
  licenses:
199
173
  - MIT
200
- metadata: {}
201
- post_install_message:
174
+ metadata:
175
+ rubygems_mfa_required: 'true'
176
+ post_install_message:
202
177
  rdoc_options: []
203
178
  require_paths:
204
179
  - lib
@@ -206,39 +181,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
206
181
  requirements:
207
182
  - - ">="
208
183
  - !ruby/object:Gem::Version
209
- version: '2.4'
184
+ version: '2.5'
210
185
  required_rubygems_version: !ruby/object:Gem::Requirement
211
186
  requirements:
212
187
  - - ">="
213
188
  - !ruby/object:Gem::Version
214
189
  version: '0'
215
190
  requirements: []
216
- rubygems_version: 3.1.4
217
- signing_key:
191
+ rubygems_version: 3.3.7
192
+ signing_key:
218
193
  specification_version: 4
219
194
  summary: Adds extended functionality to Activerecord Postgres implementation
220
- test_files:
221
- - spec/active_record_extended_spec.rb
222
- - spec/query_methods/any_of_spec.rb
223
- - spec/query_methods/array_query_spec.rb
224
- - spec/query_methods/either_spec.rb
225
- - spec/query_methods/hash_query_spec.rb
226
- - spec/query_methods/inet_query_spec.rb
227
- - spec/query_methods/json_spec.rb
228
- - spec/query_methods/select_spec.rb
229
- - spec/query_methods/unionize_spec.rb
230
- - spec/query_methods/window_spec.rb
231
- - spec/query_methods/with_cte_spec.rb
232
- - spec/spec_helper.rb
233
- - spec/sql_inspections/any_of_sql_spec.rb
234
- - spec/sql_inspections/arel/aggregate_function_name_spec.rb
235
- - spec/sql_inspections/arel/array_spec.rb
236
- - spec/sql_inspections/arel/inet_spec.rb
237
- - spec/sql_inspections/contains_sql_queries_spec.rb
238
- - spec/sql_inspections/either_sql_spec.rb
239
- - spec/sql_inspections/json_sql_spec.rb
240
- - spec/sql_inspections/unionize_sql_spec.rb
241
- - spec/sql_inspections/window_sql_spec.rb
242
- - spec/sql_inspections/with_cte_sql_spec.rb
243
- - spec/support/database_cleaner.rb
244
- - spec/support/models.rb
195
+ test_files: []
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_record_extended/query_methods/window"
4
- require "active_record_extended/query_methods/unionize"
5
- require "active_record_extended/query_methods/json"
6
-
7
- module ActiveRecordExtended
8
- module RelationPatch
9
- module QueryDelegation
10
- delegate :with, :define_window, :select_window, :foster_select, to: :all
11
- delegate(*::ActiveRecordExtended::QueryMethods::Unionize::UNIONIZE_METHODS, to: :all)
12
- delegate(*::ActiveRecordExtended::QueryMethods::Json::JSON_QUERY_METHODS, to: :all)
13
- end
14
-
15
- module Merger
16
- def normal_values
17
- super + [:union, :define_window]
18
- end
19
-
20
- def merge
21
- merge_ctes!
22
- super
23
- end
24
-
25
- def merge_ctes!
26
- return unless other.with_values?
27
-
28
- if other.recursive_value? && !relation.recursive_value?
29
- relation.with!(:chain).recursive(other.cte)
30
- else
31
- relation.with!(other.cte)
32
- end
33
- end
34
- end
35
-
36
- module ArelBuildPatch
37
- def build_arel(*aliases)
38
- super.tap do |arel|
39
- build_windows(arel) if window_values?
40
- build_unions(arel) if union_values?
41
- build_with(arel) if with_values?
42
- end
43
- end
44
- end
45
- end
46
- end
47
-
48
- ActiveRecord::Relation.prepend(ActiveRecordExtended::RelationPatch::ArelBuildPatch)
49
- ActiveRecord::Relation::Merger.prepend(ActiveRecordExtended::RelationPatch::Merger)
50
- ActiveRecord::Querying.prepend(ActiveRecordExtended::RelationPatch::QueryDelegation)
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_record"
4
- require "active_record/relation"
5
- require "active_record/relation/merger"
6
- require "active_record/relation/query_methods"
7
-
8
- require "active_record_extended/predicate_builder/array_handler_decorator"
9
-
10
- require "active_record_extended/active_record/relation_patch"
11
-
12
- require "active_record_extended/query_methods/where_chain"
13
- require "active_record_extended/query_methods/with_cte"
14
- require "active_record_extended/query_methods/unionize"
15
- require "active_record_extended/query_methods/any_of"
16
- require "active_record_extended/query_methods/either"
17
- require "active_record_extended/query_methods/inet"
18
- require "active_record_extended/query_methods/json"
19
- require "active_record_extended/query_methods/select"
20
-
21
- if Gem::Requirement.new("~> 5.1.0").satisfied_by?(ActiveRecord.gem_version)
22
- require "active_record_extended/patch/5_1/where_clause"
23
- else
24
- require "active_record_extended/patch/5_2/where_clause"
25
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecordExtended
4
- module WhereClause
5
- def modified_predicates(&block)
6
- ::ActiveRecord::Relation::WhereClause.new(predicates.map(&block), binds)
7
- end
8
- end
9
- end
10
-
11
- ActiveRecord::Relation::WhereClause.prepend(ActiveRecordExtended::WhereClause)
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecordExtended
4
- module WhereClause
5
- def modified_predicates(&block)
6
- ::ActiveRecord::Relation::WhereClause.new(predicates.map(&block))
7
- end
8
- end
9
- end
10
-
11
- ActiveRecord::Relation::WhereClause.prepend(ActiveRecordExtended::WhereClause)
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_record/relation/predicate_builder"
4
- require "active_record/relation/predicate_builder/array_handler"
5
-
6
- module ActiveRecordExtended
7
- module ArrayHandlerDecorator
8
- def call(attribute, value)
9
- cache = ActiveRecord::Base.connection.schema_cache
10
- if cache.data_source_exists?(attribute.relation.name)
11
- column = cache.columns(attribute.relation.name).detect { |col| col.name.to_s == attribute.name.to_s }
12
- return attribute.eq(value) if column.try(:array)
13
- end
14
-
15
- super(attribute, value)
16
- end
17
- end
18
- end
19
-
20
- ActiveRecord::PredicateBuilder::ArrayHandler.prepend(ActiveRecordExtended::ArrayHandlerDecorator)
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe ActiveRecordExtended do
4
- it "has a version number" do
5
- expect(ActiveRecordExtended::VERSION).not_to be nil
6
- end
7
- end
@@ -1,131 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe "Active Record Any / None of Methods" do
6
- let!(:one) { User.create!(personal_id: 1) }
7
- let!(:two) { User.create!(personal_id: 2) }
8
- let!(:three) { User.create!(personal_id: 3) }
9
-
10
- let!(:tag_one) { Tag.create!(user_id: one.id) }
11
- let!(:tag_two) { Tag.create!(user_id: two.id) }
12
- let!(:tag_three) { Tag.create!(user_id: three.id) }
13
-
14
- describe "where.any_of/1" do
15
- it "Should return queries that match any of the outlined queries" do
16
- query = User.where.any_of({ personal_id: 1 }, { personal_id: 2 })
17
- expect(query).to include(one, two)
18
- expect(query).to_not include(three)
19
- end
20
-
21
- it "Should accept where query predicates" do
22
- personal_one = User.where(personal_id: 1)
23
- personal_two = User.where(personal_id: 2)
24
- query = User.where.any_of(personal_one, personal_two)
25
-
26
- expect(query).to include(one, two)
27
- expect(query).to_not include(three)
28
- end
29
-
30
- it "Should accept query strings" do
31
- personal_one = User.where(personal_id: 1)
32
-
33
- query = User.where.any_of(personal_one, "personal_id > 2")
34
- expect(query).to include(one, three)
35
- expect(query).to_not include(two)
36
-
37
- query = User.where.any_of(["personal_id >= ?", 2])
38
- expect(query).to include(two, three)
39
- expect(query).to_not include(one)
40
- end
41
-
42
- context "Relationship queries" do
43
- it "Finds records that are queried from two or more has_many associations" do
44
- user_one_tag = Tag.create!(user_id: one.id)
45
- user_two_tag = Tag.create!(user_id: two.id)
46
- query = Tag.where.any_of(one.hm_tags, two.hm_tags)
47
-
48
- expect(query).to include(tag_one, tag_two, user_one_tag, user_two_tag)
49
- expect(query).to_not include(tag_three)
50
- end
51
-
52
- it "Finds records that are dynamically joined" do
53
- user_one_tag = Tag.where(users: { id: one.id }).includes(:user).references(:user)
54
- user_two_tag = Tag.where(users: { id: two.id }).joins(:user)
55
- query = Tag.where.any_of(user_one_tag, user_two_tag)
56
-
57
- expect(query).to include(tag_one, tag_two)
58
- expect(query).to_not include(tag_three)
59
- end
60
-
61
- it "Return matched records of a joined table on the parent level" do
62
- query = Tag.joins(:user).where.any_of(
63
- { users: { personal_id: 1 } },
64
- { users: { personal_id: 3 } }
65
- )
66
-
67
- expect(query).to include(tag_one, tag_three)
68
- expect(query).to_not include(tag_two)
69
- end
70
- end
71
- end
72
-
73
- describe "where.none_of/1" do
74
- it "Should return queries that match none of the outlined queries" do
75
- query = User.where.none_of({ personal_id: 1 }, { personal_id: 2 })
76
- expect(query).to include(three)
77
- expect(query).to_not include(one, two)
78
- end
79
-
80
- it "Should accept where query predicates" do
81
- personal_one = User.where(personal_id: 1)
82
- personal_two = User.where(personal_id: 2)
83
- query = User.where.none_of(personal_one, personal_two)
84
-
85
- expect(query).to include(three)
86
- expect(query).to_not include(one, two)
87
- end
88
-
89
- it "Should accept query strings" do
90
- personal_one = User.where(personal_id: 1)
91
-
92
- query = User.where.none_of(personal_one, "personal_id > 2")
93
- expect(query).to include(two)
94
- expect(query).to_not include(one, three)
95
-
96
- query = User.where.none_of(["personal_id >= ?", 2])
97
- expect(query).to include(one)
98
- expect(query).to_not include(two, three)
99
- end
100
-
101
- context "Relationship queries" do
102
- it "Finds records that are queried from two or more has_many associations" do
103
- user_one_tag = Tag.create!(user_id: one.id)
104
- user_two_tag = Tag.create!(user_id: two.id)
105
- query = Tag.where.none_of(one.hm_tags, two.hm_tags)
106
-
107
- expect(query).to include(tag_three)
108
- expect(query).to_not include(tag_one, tag_two, user_one_tag, user_two_tag)
109
- end
110
-
111
- it "Finds records that are dynamically joined" do
112
- user_one_tag = Tag.where(users: { id: one.id }).includes(:user).references(:user)
113
- user_two_tag = Tag.where(users: { id: two.id }).joins(:user)
114
- query = Tag.where.none_of(user_one_tag, user_two_tag)
115
-
116
- expect(query).to include(tag_three)
117
- expect(query).to_not include(tag_one, tag_two)
118
- end
119
-
120
- it "Return matched records of a joined table on the parent level" do
121
- query = Tag.joins(:user).where.none_of(
122
- { users: { personal_id: 1 } },
123
- { users: { personal_id: 3 } }
124
- )
125
-
126
- expect(query).to include(tag_two)
127
- expect(query).to_not include(tag_one, tag_three)
128
- end
129
- end
130
- end
131
- end
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe "Active Record Array Query Methods" do
6
- let!(:one) { User.create!(tags: [1, 2, 3], personal_id: 33) }
7
- let!(:two) { User.create!(tags: [3, 1, 5], personal_id: 88) }
8
- let!(:three) { User.create!(tags: [2, 8, 20], personal_id: 33) }
9
-
10
- describe "#overlap" do
11
- it "Should return matched records" do
12
- query = User.where.overlap(tags: [1])
13
- expect(query).to include(one, two)
14
- expect(query).to_not include(three)
15
-
16
- query = User.where.overlap(tags: [2, 3])
17
- expect(query).to include(one, two, three)
18
- end
19
- end
20
-
21
- describe "#contains" do
22
- it "returns records that contain elements in an array" do
23
- query = User.where.contains(tags: [1, 3])
24
- expect(query).to include(one, two)
25
- expect(query).to_not include(three)
26
-
27
- query = User.where.contains(tags: [8, 2])
28
- expect(query).to include(three)
29
- expect(query).to_not include(one, two)
30
- end
31
- end
32
-
33
- describe "#any" do
34
- it "should return any records that match" do
35
- query = User.where.any(tags: 3)
36
- expect(query).to include(one, two)
37
- expect(query).to_not include(three)
38
- end
39
-
40
- it "allows chaining" do
41
- query = User.where.any(tags: 3).where(personal_id: 33)
42
- expect(query).to include(one)
43
- expect(query).to_not include(two, three)
44
- end
45
- end
46
-
47
- describe "#all" do
48
- let!(:contains_all) { User.create!(tags: [1], personal_id: 1) }
49
- let!(:contains_all_two) { User.create!(tags: [1], personal_id: 2) }
50
- let!(:contains_some) { User.create!(tags: [1, 2], personal_id: 2) }
51
-
52
- it "should return any records that match" do
53
- query = User.where.all(tags: 1)
54
- expect(query).to include(contains_all, contains_all_two)
55
- expect(query).to_not include(contains_some)
56
- end
57
-
58
- it "allows chaining" do
59
- query = User.where.all(tags: 1).where(personal_id: 1)
60
- expect(query).to include(contains_all)
61
- expect(query).to_not include(contains_all_two, contains_some)
62
- end
63
- end
64
- end
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe "Active Record Either Methods" do
6
- let!(:one) { User.create! }
7
- let!(:two) { User.create! }
8
- let!(:three) { User.create! }
9
- let!(:profile_l) { ProfileL.create!(user_id: one.id, likes: 100) }
10
- let!(:profile_r) { ProfileR.create!(user_id: two.id, dislikes: 50) }
11
-
12
- describe ".either_join/2" do
13
- it "Should only only return records that belong to profile L or profile R" do
14
- query = User.either_join(:profile_l, :profile_r)
15
- expect(query).to include(one, two)
16
- expect(query).to_not include(three)
17
- end
18
-
19
- context "Alias .either_joins/2" do
20
- it "Should only only return records that belong to profile L or profile R" do
21
- query = User.either_joins(:profile_l, :profile_r)
22
- expect(query).to include(one, two)
23
- expect(query).to_not include(three)
24
- end
25
- end
26
-
27
- context "Through association .either_joins/2" do
28
- let!(:four) { User.create! }
29
- let!(:group) { Group.create!(users: [four]) }
30
-
31
- it "Should only only return records that belong to profile L or has group" do
32
- query = User.either_joins(:profile_l, :groups)
33
- expect(query).to include(one, four)
34
- expect(query).to_not include(three, two)
35
- end
36
- end
37
- end
38
-
39
- describe ".either_order/2" do
40
- it "Should not exclude anyone who does not have a relationship" do
41
- query = User.either_order(:asc, profile_l: :likes, profile_r: :dislikes)
42
- expect(query.count).to eq(3)
43
- expect(query[0]).to eq(two)
44
- expect(query[1]).to eq(one)
45
- expect(query[2]).to eq(three)
46
- end
47
-
48
- it "Should order users based on their likes and dislikes in ascended order" do
49
- query = User.either_order(:asc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
50
- expect(query.count).to eq(2)
51
- expect(query.first).to eq(two)
52
- expect(query.last).to eq(one)
53
- end
54
-
55
- it "Should order users based on their likes and dislikes in descending order" do
56
- query = User.either_order(:desc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
57
- expect(query.first).to eq(one)
58
- expect(query.last).to eq(two)
59
- end
60
-
61
- context "Alias .either_order/2" do
62
- it "Should order users based on their likes and dislikes in ascended order" do
63
- query = User.either_orders(:asc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
64
- expect(query.count).to eq(2)
65
- expect(query.first).to eq(two)
66
- expect(query.last).to eq(one)
67
- end
68
- end
69
- end
70
- end