records_with_records 0.1.1 → 0.2.0

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: d95f6bc84336cf13b99aaa7708d169df2200877fe25b8982f2444fcb493a9cf1
4
- data.tar.gz: f7c38d0f50a645185fd738ab3bd96204fc7d79fee7f25c2004f27c66c1d9e460
3
+ metadata.gz: 76ac2aa88e2257ff4fdfc74fe6a4dc5a51f5d51c59c178cafe1deec245faff6d
4
+ data.tar.gz: 2332feb2a12cb5188d6bd8f0e34cdd945d1aeef982ae713c5b1646e3ed539dd4
5
5
  SHA512:
6
- metadata.gz: ba78d4452bcc62d4a67e7aaa058d56949b83aab68391664090608174fb5bb70fba08fe9b7facf752d266929f128b8139f232a2c03cd074ef6b61f6359258bc2e
7
- data.tar.gz: 9f45042332ef18b2418a80113678f82a4dbc71b8d69c848bf647287be390e7ab4f20781354c2eac0172123822a258c13f38ddec6bef408bf048c294dd7edfbe6
6
+ metadata.gz: '0867c27d46b2275d0130155249825a2ad8b99dbe92495942d1bc01a5e7c82eb4864dd58af68d8403cb6916eaa33a3b77a87f9c5bdf17356c16e3988b22afb16e'
7
+ data.tar.gz: a63ba8b9cbffac1e97afd0bae3c4f5810cb7023993d64f23b6c47f4c2825da5deb7a194819779d5c78cfdc4bb0b8c80f444cf9eeafcf339e66f02bbabac38b66
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- records_with_records (0.1.1)
4
+ records_with_records (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # RecordsWithRecords
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/records_with_records`. To experiment with that code, run `bin/console` for an interactive prompt.
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
 
@@ -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(assoc, scope = nil)
19
- where(exist(find_reflection(assoc), scope))
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(assoc, scope = nil)
24
- where.not(exist(find_reflection(assoc), scope))
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(&@reflection_scope) if @reflection_scope
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
- scp = @reflection.klass.all
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RecordsWithRecords
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  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.1.1
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-04-30 00:00:00.000000000 Z
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