baby_squeel 1.3.1 → 1.4.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a31dea90feb497d7b2cc1f01a18a9ea8a6c7ab3819f003a660b44a2ce409f4b6
4
- data.tar.gz: ae9a5da834fe1e917085c4d370543dd89de9ef5b44cc83c384c9268516c933ff
3
+ metadata.gz: 2df3535278f1724c6db1656ab9c5bbe0e9097e80ee6f2c1fdaeb3491bc838ae9
4
+ data.tar.gz: bddd464023f9f53f0ff32cc1fbaa88928f5736b3e6541418b8c36f26b01b8d34
5
5
  SHA512:
6
- metadata.gz: 5883394797bdeb50a88fef7b3b5032c7cfcf7a3dd80c7de197aa763aadf2b019e5aa0e5a8d66967268cacc5b52e3dfc3aa0de02eb235939492c3ee41a50be078
7
- data.tar.gz: ce1de58a03cb4a08e789e9c6ab9feb12333f45c477d3780b4eecb9d7acf9bb18c920fd632b5d74b059af95827cb5463bc82ee2fe8eee634ac47777a7015f1493
6
+ metadata.gz: 44f8ecb2269017a1e141f0f90d5c9a7eee7d5b877500897aba0a60646f38de0cdae4b992f505d022740c03e73d6293990d9f509d5b3188b5be8051819e168069
7
+ data.tar.gz: a63f7b474504ef598508879d8ad702396f194c663cc46ececdb9c8dd31aabb10752323332bc26b3cec5cfb2df93e5328e11d2ce1983efe23793c618ccce9294d
data/README.md CHANGED
@@ -1,10 +1,7 @@
1
- # BabySqueel
1
+ # BabySqueel 🐷
2
2
 
3
- [![Build Status](https://travis-ci.org/rzane/baby_squeel.svg?branch=master)](https://travis-ci.org/rzane/baby_squeel)
4
- [![Code Climate](https://codeclimate.com/github/rzane/baby_squeel/badges/gpa.svg)](https://codeclimate.com/github/rzane/baby_squeel)
5
- [![Coverage Status](https://coveralls.io/repos/github/rzane/baby_squeel/badge.svg?branch=master)](https://coveralls.io/github/rzane/baby_squeel?branch=master)
6
-
7
- <img align="right" src="http://static.thefrisky.com/uploads/2010/07/01/pig_in_boots_070110_m.jpg" alt="biddy piggy">
3
+ ![Build](https://github.com/rzane/baby_squeel/workflows/Build/badge.svg)
4
+ ![Version](https://img.shields.io/gem/v/baby_squeel)
8
5
 
9
6
  Have you ever used the [Squeel](https://github.com/activerecord-hackery/squeel) gem? It's a really nice way to build complex queries. However, Squeel monkeypatches Active Record internals, because it was aimed at enhancing the existing API with the aim of inclusion into Rails. However, that inclusion never happened, and it left Squeel susceptible to breakage from arbitrary changes in Active Record, eventually burning out the maintainer.
10
7
 
@@ -219,6 +216,21 @@ Post.joins(:author).where.has {
219
216
  # )
220
217
  ```
221
218
 
219
+ ##### Exists
220
+
221
+ ```ruby
222
+ Post.where.has {
223
+ exists Post.where.has { author_id == 1 }
224
+ }
225
+ # SELECT "posts".* FROM "posts"
226
+ # WHERE (
227
+ # EXISTS(
228
+ # SELECT "posts".* FROM "posts"
229
+ # WHERE "posts"."author_id" = 1
230
+ # )
231
+ # )
232
+ ```
233
+
222
234
  ##### Custom SQL Operators
223
235
 
224
236
  ```ruby
@@ -240,6 +252,26 @@ Post.joining {
240
252
  }
241
253
  ```
242
254
 
255
+ ##### Polymorphism
256
+
257
+ Given this polymorphism:
258
+
259
+ ```ruby
260
+ # app/models/picture.rb
261
+ belongs_to :imageable, polymorphic: true
262
+
263
+ # app/models/post.rb
264
+ has_many :pictures, as: :imageable
265
+ ```
266
+
267
+ The query might look like this:
268
+
269
+ ```ruby
270
+ Picture.
271
+ joining { imageable.of(Post) }.
272
+ selecting { imageable.of(Post).id }
273
+ ```
274
+
243
275
  ##### Helpers
244
276
 
245
277
  ```ruby
@@ -283,7 +315,7 @@ Post.joins(:author).where.has {
283
315
  The following methods give you access to BabySqueel's DSL:
284
316
 
285
317
  | BabySqueel | Active Record Equivalent |
286
- |---------------|--------------------------|
318
+ | ------------- | ------------------------ |
287
319
  | `selecting` | `select` |
288
320
  | `ordering` | `order` |
289
321
  | `joining` | `joins` |
data/baby_squeel.gemspec CHANGED
@@ -19,12 +19,12 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.files = Dir.glob('{lib/**/*,*.{md,txt,gemspec}}')
21
21
 
22
- spec.add_dependency 'activerecord', '>= 4.2.0'
23
- spec.add_dependency 'polyamorous', '~> 1.3'
24
- spec.add_dependency 'join_dependency', '~> 0.1.2'
22
+ spec.add_dependency 'activerecord', '~> 5.2'
23
+ spec.add_dependency 'ransack', '~> 2.3'
24
+ spec.add_dependency 'join_dependency', '~> 0.1.4'
25
25
 
26
- spec.add_development_dependency 'bundler', '~> 1.11'
27
- spec.add_development_dependency 'rake', '~> 10.0'
28
- spec.add_development_dependency 'rspec', '~> 3.4.0'
29
- spec.add_development_dependency 'sqlite3'
26
+ spec.add_development_dependency 'bundler', '~> 2'
27
+ spec.add_development_dependency 'rake', '~> 13.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.10'
29
+ spec.add_development_dependency 'sqlite3', '~> 1.3.6'
30
30
  end
data/lib/baby_squeel.rb CHANGED
@@ -1,6 +1,11 @@
1
1
  require 'active_record'
2
2
  require 'active_record/relation'
3
- require 'polyamorous'
3
+ begin
4
+ require 'polyamorous'
5
+ rescue LoadError
6
+ # Trying loading from 'ransack' as of commit c9cc20de9 (post v2.3.2)
7
+ require 'polyamorous/polyamorous'
8
+ end
4
9
  require 'baby_squeel/version'
5
10
  require 'baby_squeel/errors'
6
11
  require 'baby_squeel/active_record/base'
@@ -35,43 +35,29 @@ module BabySqueel
35
35
  # a list (in order of chaining) of associations and finding
36
36
  # the respective JoinAssociation at each level.
37
37
  def find_alias(associations)
38
- table = find_join_association(associations).table
39
- reconstruct_with_type_caster(table, associations)
38
+ # If we tell join_dependency to construct its tables, Active Record
39
+ # handles building the correct aliases and attaching them to its
40
+ # JoinDepenencies.
41
+ if ::ActiveRecord::VERSION::STRING >= '5.2.3'
42
+ join_dependency.send(:construct_tables!, join_dependency.send(:join_root))
43
+ end
44
+
45
+ join_association = find_join_association(associations)
46
+ join_association.table
40
47
  end
41
48
 
42
49
  private
43
50
 
44
51
  def find_join_association(associations)
45
- associations.inject(join_dependency.send(:join_root)) do |parent, assoc|
46
- parent.children.find do |join_association|
47
- reflections_equal?(
48
- assoc._reflection,
49
- join_association.reflection
50
- )
51
- end
52
- end
53
- end
52
+ current = join_dependency.send(:join_root)
54
53
 
55
- # Compare two reflections and see if they're the same.
56
- def reflections_equal?(a, b)
57
- comparable_reflection(a) == comparable_reflection(b)
58
- end
59
-
60
- # Get the parent of the reflection if it has one.
61
- # In AR4, #parent_reflection returns [name, reflection]
62
- # In AR5, #parent_reflection returns just a reflection
63
- def comparable_reflection(reflection)
64
- [*reflection.parent_reflection].last || reflection
65
- end
54
+ associations.each do |association|
55
+ name = association._reflection.name
56
+ current = current.children.find { |c| c.reflection.name == name }
57
+ break if current.nil?
58
+ end
66
59
 
67
- # Active Record 5's AliasTracker initializes Arel tables
68
- # with the type_caster belonging to the wrong model.
69
- #
70
- # See: https://github.com/rails/rails/pull/27994
71
- def reconstruct_with_type_caster(table, associations)
72
- return table if ::ActiveRecord::VERSION::MAJOR < 5
73
- type_caster = associations.last._scope.type_caster
74
- ::Arel::Table.new(table.name, type_caster: type_caster)
60
+ current
75
61
  end
76
62
  end
77
63
  end
@@ -1,3 +1,3 @@
1
1
  module BabySqueel
2
- VERSION = '1.3.1'.freeze
2
+ VERSION = '1.4.0.beta1'.freeze
3
3
  end
metadata CHANGED
@@ -1,113 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baby_squeel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-15 00:00:00.000000000 Z
11
+ date: 2021-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: '5.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: polyamorous
28
+ name: ransack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.3'
33
+ version: '2.3'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: '2.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: join_dependency
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.1.2
47
+ version: 0.1.4
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.1.2
54
+ version: 0.1.4
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.11'
61
+ version: '2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.11'
68
+ version: '2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '13.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '13.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 3.4.0
89
+ version: '3.10'
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: 3.4.0
96
+ version: '3.10'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: sqlite3
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: 1.3.6
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: 1.3.6
111
111
  description: An expressive query DSL for Active Record 4 and 5.
112
112
  email:
113
113
  - ray@promptworks.com
@@ -149,7 +149,7 @@ homepage: https://github.com/rzane/baby_squeel
149
149
  licenses:
150
150
  - MIT
151
151
  metadata: {}
152
- post_install_message:
152
+ post_install_message:
153
153
  rdoc_options: []
154
154
  require_paths:
155
155
  - lib
@@ -160,13 +160,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
160
  version: '0'
161
161
  required_rubygems_version: !ruby/object:Gem::Requirement
162
162
  requirements:
163
- - - ">="
163
+ - - ">"
164
164
  - !ruby/object:Gem::Version
165
- version: '0'
165
+ version: 1.3.1
166
166
  requirements: []
167
- rubyforge_project:
168
- rubygems_version: 2.7.6
169
- signing_key:
167
+ rubygems_version: 3.0.3
168
+ signing_key:
170
169
  specification_version: 4
171
170
  summary: An expressive query DSL for Active Record 4 and 5.
172
171
  test_files: []