records_with_records 0.1.1 → 0.2.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +44 -4
- data/lib/records_with_records.rb +4 -4
- data/lib/records_with_records/association.rb +4 -3
- data/lib/records_with_records/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76ac2aa88e2257ff4fdfc74fe6a4dc5a51f5d51c59c178cafe1deec245faff6d
|
4
|
+
data.tar.gz: 2332feb2a12cb5188d6bd8f0e34cdd945d1aeef982ae713c5b1646e3ed539dd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0867c27d46b2275d0130155249825a2ad8b99dbe92495942d1bc01a5e7c82eb4864dd58af68d8403cb6916eaa33a3b77a87f9c5bdf17356c16e3988b22afb16e'
|
7
|
+
data.tar.gz: a63ba8b9cbffac1e97afd0bae3c4f5810cb7023993d64f23b6c47f4c2825da5deb7a194819779d5c78cfdc4bb0b8c80f444cf9eeafcf339e66f02bbabac38b66
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# RecordsWithRecords
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This gem aims to simplify exists queries
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -21,8 +19,50 @@ Or install it yourself as:
|
|
21
19
|
$ gem install records_with_records
|
22
20
|
|
23
21
|
## Usage
|
22
|
+
Assume you have a relation like this
|
23
|
+
```ruby
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
has_many :received_messages, class_name: 'Message', foreign_key: :receiver_id
|
26
|
+
end
|
27
|
+
```
|
28
|
+
### Exists
|
29
|
+
To get all Users with associated Messages you would write
|
30
|
+
```ruby
|
31
|
+
User.where(Message.where('receiver_id = users.id').arel.exists)
|
32
|
+
=> SELECT "users".* FROM "users" WHERE EXISTS (SELECT "messages".* FROM "messages" WHERE (receiver_id = users.id))
|
33
|
+
```
|
34
|
+
Instead you can now write
|
35
|
+
```ruby
|
36
|
+
User.where_exists(:received_messages)
|
37
|
+
=> SELECT "users".* FROM "users" WHERE EXISTS (SELECT "messages".* FROM "messages" WHERE ("messages"."receiver_id" = "users"."id"))
|
38
|
+
```
|
39
|
+
### Not exists
|
40
|
+
Querying records without associated records is also possible
|
41
|
+
```ruby
|
42
|
+
User.where_not_exists(:received_messages)
|
43
|
+
=> SELECT "users".* FROM "users" WHERE NOT (EXISTS (SELECT "messages".* FROM "messages" WHERE ("messages"."receiver_id" = "users"."id")))
|
44
|
+
```
|
45
|
+
|
46
|
+
### Additional scope
|
47
|
+
You can pass a scope as second argument
|
48
|
+
```ruby
|
49
|
+
User.where_not_exists(:received_messages, -> { where(received_at: 5.hours.ago..) })
|
50
|
+
=> SELECT "users".* FROM "users" WHERE NOT (EXISTS (SELECT "messages".* FROM "messages" WHERE "messages"."received_at" >= $1 AND ("messages"."receiver_id" = "users"."id"))) [["received_at", "2021-05-01 04:16:42.325804"]]
|
51
|
+
```
|
52
|
+
|
53
|
+
### Association scope
|
54
|
+
Scopes defined on the association are also applied
|
55
|
+
```ruby
|
56
|
+
class User < ActiveRecord::Base
|
57
|
+
has_many :pending_messages, -> { where(messages: {received_at: nil}) }, class_name: 'Message', foreign_key: :receiver_id
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
## Caveats
|
64
|
+
Querying on the existance of `belongs_to` associations is currently not possible.
|
24
65
|
|
25
|
-
TODO: Write usage instructions here
|
26
66
|
|
27
67
|
## Development
|
28
68
|
|
data/lib/records_with_records.rb
CHANGED
@@ -15,13 +15,13 @@ module RecordsWithRecords
|
|
15
15
|
module ClassMethods
|
16
16
|
|
17
17
|
# Returns records of included model where +assoc+ exists
|
18
|
-
def where_exists(
|
19
|
-
where(exist(find_reflection(
|
18
|
+
def where_exists(association, scope = nil)
|
19
|
+
where(exist(find_reflection(association), scope))
|
20
20
|
end
|
21
21
|
|
22
22
|
# Returns records of included model where +assoc+ exists not
|
23
|
-
def where_not_exists(
|
24
|
-
where.not(exist(find_reflection(
|
23
|
+
def where_not_exists(association, scope = nil)
|
24
|
+
where.not(exist(find_reflection(association), scope))
|
25
25
|
end
|
26
26
|
|
27
27
|
def respond_to_missing?(name, *args)
|
@@ -20,14 +20,13 @@ class Association
|
|
20
20
|
|
21
21
|
def apply_scopes(scope)
|
22
22
|
scope = scope&.instance_exec(&@assoc_scope) if @assoc_scope
|
23
|
-
scope = scope&.instance_exec(&@
|
23
|
+
scope = scope&.instance_exec(&@reflection.scope) if @reflection.scope
|
24
24
|
scope
|
25
25
|
end
|
26
26
|
|
27
27
|
class HasMany < Association
|
28
28
|
def scope
|
29
|
-
|
30
|
-
apply_scopes(scp)
|
29
|
+
apply_scopes(@reflection.klass.all)
|
31
30
|
end
|
32
31
|
|
33
32
|
def foreign_key
|
@@ -56,4 +55,6 @@ class Association
|
|
56
55
|
@reflection.source_reflection
|
57
56
|
end
|
58
57
|
end
|
58
|
+
|
59
|
+
# TODO: Add belongs_to association
|
59
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: records_with_records
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dario Litz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An extension for Active Record for querying records with or without associated
|
14
14
|
records
|