active_record_extended 2.0.3 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +140 -77
  3. data/lib/active_record_extended/arel/nodes.rb +1 -1
  4. data/lib/active_record_extended/arel/{sql_literal.rb → sql_literal_patch.rb} +2 -2
  5. data/lib/active_record_extended/arel/visitors/postgresql_decorator.rb +16 -12
  6. data/lib/active_record_extended/arel.rb +1 -1
  7. data/lib/active_record_extended/patch/array_handler_patch.rb +22 -0
  8. data/lib/active_record_extended/patch/relation_patch.rb +82 -0
  9. data/lib/active_record_extended/patch/where_clause_patch.rb +13 -0
  10. data/lib/active_record_extended/query_methods/any_of.rb +7 -31
  11. data/lib/active_record_extended/query_methods/either.rb +5 -7
  12. data/lib/active_record_extended/query_methods/{select.rb → foster_select.rb} +4 -4
  13. data/lib/active_record_extended/query_methods/json.rb +2 -2
  14. data/lib/active_record_extended/query_methods/unionize.rb +3 -3
  15. data/lib/active_record_extended/query_methods/where_chain.rb +96 -90
  16. data/lib/active_record_extended/query_methods/window.rb +3 -3
  17. data/lib/active_record_extended/query_methods/with_cte.rb +70 -9
  18. data/lib/active_record_extended/utilities/order_by.rb +1 -1
  19. data/lib/active_record_extended/utilities/support.rb +1 -1
  20. data/lib/active_record_extended/version.rb +1 -1
  21. data/lib/active_record_extended.rb +55 -4
  22. metadata +34 -83
  23. data/lib/active_record_extended/active_record/relation_patch.rb +0 -50
  24. data/lib/active_record_extended/active_record.rb +0 -25
  25. data/lib/active_record_extended/patch/5_1/where_clause.rb +0 -11
  26. data/lib/active_record_extended/patch/5_2/where_clause.rb +0 -11
  27. data/lib/active_record_extended/predicate_builder/array_handler_decorator.rb +0 -20
  28. data/spec/active_record_extended_spec.rb +0 -7
  29. data/spec/query_methods/any_of_spec.rb +0 -131
  30. data/spec/query_methods/array_query_spec.rb +0 -64
  31. data/spec/query_methods/either_spec.rb +0 -70
  32. data/spec/query_methods/hash_query_spec.rb +0 -45
  33. data/spec/query_methods/inet_query_spec.rb +0 -112
  34. data/spec/query_methods/json_spec.rb +0 -157
  35. data/spec/query_methods/select_spec.rb +0 -115
  36. data/spec/query_methods/unionize_spec.rb +0 -165
  37. data/spec/query_methods/window_spec.rb +0 -51
  38. data/spec/query_methods/with_cte_spec.rb +0 -50
  39. data/spec/spec_helper.rb +0 -28
  40. data/spec/sql_inspections/any_of_sql_spec.rb +0 -41
  41. data/spec/sql_inspections/arel/aggregate_function_name_spec.rb +0 -41
  42. data/spec/sql_inspections/arel/array_spec.rb +0 -63
  43. data/spec/sql_inspections/arel/inet_spec.rb +0 -66
  44. data/spec/sql_inspections/contains_sql_queries_spec.rb +0 -47
  45. data/spec/sql_inspections/either_sql_spec.rb +0 -71
  46. data/spec/sql_inspections/json_sql_spec.rb +0 -82
  47. data/spec/sql_inspections/unionize_sql_spec.rb +0 -124
  48. data/spec/sql_inspections/window_sql_spec.rb +0 -98
  49. data/spec/sql_inspections/with_cte_sql_spec.rb +0 -95
  50. data/spec/support/database_cleaner.rb +0 -15
  51. data/spec/support/models.rb +0 -80
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_extended
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 3.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Protacio-Karaszi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-06-25 00:00:00.000000000 Z
13
+ date: 2023-02-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -18,34 +18,20 @@ 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
- version: '6.2'
24
+ version: 7.2.0
25
25
  type: :runtime
26
26
  prerelease: false
27
27
  version_requirements: !ruby/object:Gem::Requirement
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
- version: '6.2'
35
- - !ruby/object:Gem::Dependency
36
- name: ar_outer_joins
37
- requirement: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: '0.2'
42
- type: :runtime
43
- prerelease: false
44
- version_requirements: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - "~>"
47
- - !ruby/object:Gem::Version
48
- version: '0.2'
34
+ version: 7.2.0
49
35
  - !ruby/object:Gem::Dependency
50
36
  name: pg
51
37
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +52,7 @@ dependencies:
66
52
  requirements:
67
53
  - - ">="
68
54
  - !ruby/object:Gem::Version
69
- version: '1.16'
55
+ version: '2.2'
70
56
  - - "<"
71
57
  - !ruby/object:Gem::Version
72
58
  version: '3.0'
@@ -76,7 +62,7 @@ dependencies:
76
62
  requirements:
77
63
  - - ">="
78
64
  - !ruby/object:Gem::Version
79
- version: '1.16'
65
+ version: '2.2'
80
66
  - - "<"
81
67
  - !ruby/object:Gem::Version
82
68
  version: '3.0'
@@ -86,14 +72,14 @@ dependencies:
86
72
  requirements:
87
73
  - - "~>"
88
74
  - !ruby/object:Gem::Version
89
- version: '1.6'
75
+ version: '2.0'
90
76
  type: :development
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
80
  - - "~>"
95
81
  - !ruby/object:Gem::Version
96
- version: '1.6'
82
+ version: '2.0'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: rake
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +108,20 @@ dependencies:
122
108
  - - "~>"
123
109
  - !ruby/object:Gem::Version
124
110
  version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-sqlimit
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.0.5
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.0.5
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: simplecov
127
127
  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,34 +168,11 @@ 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: {}
174
+ metadata:
175
+ rubygems_mfa_required: 'true'
201
176
  post_install_message:
202
177
  rdoc_options: []
203
178
  require_paths:
@@ -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.7'
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.0.6
191
+ rubygems_version: 3.4.1
217
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
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe "Active Record Hash Related Query Methods" do
6
- let!(:one) { User.create!(data: { nickname: "george" }, jsonb_data: { payment: "zip" }) }
7
- let!(:two) { User.create!(data: { nickname: "dan" }, jsonb_data: { payment: "zipper" }) }
8
- let!(:three) { User.create!(data: { nickname: "georgey" }) }
9
-
10
- describe "#contains" do
11
- context "HStore Column Type" do
12
- it "returns records that contain hash elements in joined tables" do
13
- tag_one = Tag.create!(user_id: one.id)
14
- tag_two = Tag.create!(user_id: two.id)
15
-
16
- query = Tag.joins(:user).where.contains(users: { data: { nickname: "george" } })
17
- expect(query).to include(tag_one)
18
- expect(query).to_not include(tag_two)
19
- end
20
-
21
- it "returns records that contain hash value" do
22
- query = User.where.contains(data: { nickname: "george" })
23
- expect(query).to include(one)
24
- expect(query).to_not include(two, three)
25
- end
26
- end
27
-
28
- context "JSONB Column Type" do
29
- it "returns records that contains a json hashed value" do
30
- query = User.where.contains(jsonb_data: { payment: "zip" })
31
- expect(query).to include(one)
32
- expect(query).to_not include(two, three)
33
- end
34
-
35
- it "returns records that contain jsonb elements in joined tables" do
36
- tag_one = Tag.create!(user_id: one.id)
37
- tag_two = Tag.create!(user_id: two.id)
38
-
39
- query = Tag.joins(:user).where.contains(users: { jsonb_data: { payment: "zip" } })
40
- expect(query).to include(tag_one)
41
- expect(query).to_not include(tag_two)
42
- end
43
- end
44
- end
45
- end