active_record_has 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e050075e8902614ca9297fa6db501848187c2bbdc422e2194a32a3d7953faa4
4
- data.tar.gz: 31ad22d4014c8a943309e722c3c8ead9218c4faab679781306a919feee47ba58
3
+ metadata.gz: a8e6638722e38048167aaadfc8a1d56f0381808394023137b361a58d415d2408
4
+ data.tar.gz: a1fabbc4b3002c51a0504cdad5e60ee105f1d44c8e4ccbe5243ed9cd7280812b
5
5
  SHA512:
6
- metadata.gz: d41e8f0e5d4c2a82cb417fe0ba0d559fc807a91d3fa653e060217313100da40742df38a1ac4e011fcd02e9ce91336dd047b74d505856f02dfcfcf37c544cfffb
7
- data.tar.gz: 24330af5173b92b37e511167dafef226aeac1a4374d1b317b67ccd42d169da62e872333431a3a4f8f7f77e3a9ca115487ad737dbde21b0ae2a6f79682a58dea8
6
+ metadata.gz: 73ae0efb26f616ef5f69e067e30720fafca90c3c2dde0bf5b3f2c84b625d02eca4095be04dd42f17c9f55a0f8c516ae769c1693dbf18197f2b84ecb4c0ae70a5
7
+ data.tar.gz: cd17d4d95812570ba2c14ccc76d176c1fee784c5cce8eb1bf252864331c9e2ea55c965bd718c1e58ae982f69d2bcf40d7f7e9189eec2e656c613125cb2779b07
@@ -2,17 +2,55 @@
2
2
 
3
3
  module ActiveRecordHas
4
4
  class Scope
5
- attr_reader :foreign_scope
6
- def initialize(reflection, &block)
5
+ attr_reader :model, :association, :relation, :reflection, :options, :block
6
+
7
+ def initialize(model, association, relation, **options, &block)
8
+ @model = model
9
+ @association = association
10
+ @block = block
11
+ @options = options
12
+ @reflection ||=
13
+ if association.is_a? Symbol
14
+ @relation = relation if relation.is_a? ActiveRecord::Relation
15
+ @reflection = reflections[association.to_s]
16
+ elsif association.is_a? ActiveRecord::Relation
17
+ @relation = association
18
+ model_name = association.model_name
19
+ @reflection = reflections[model_name.plural] || reflections[model_name.singular]
20
+ else
21
+ nil
22
+ end
23
+ end
24
+
25
+ delegate :reflections, to: :model
26
+
27
+ def reflection
28
+ @reflection ||=
29
+ if association.is_a? Symbol
30
+ @reflection = reflections[association.to_s]
31
+ elsif association.is_a? ActiveRecord::Relation
32
+ model_name = association.model_name
33
+ @reflection = reflections[model_name.plural] || reflections[model_name.singular]
34
+ else
35
+ nil
36
+ end
37
+ end
38
+
39
+ def scope
7
40
  raise Error unless reflection
8
41
 
9
- foreign_scope = reflection.foreign_scope
10
- foreign_scope = foreign_scope.instance_eval(&block) if block
11
- @foreign_scope = foreign_scope
42
+ @scope ||=
43
+ begin
44
+ result = reflection.foreign_scope
45
+ result = result.instance_eval(&block) if block
46
+ result = result.merge(relation) if relation
47
+ result = result.where(**options) if options.present?
48
+ result
49
+ end
12
50
  end
13
51
 
14
52
  def exists
15
- foreign_scope.arel.exists
53
+ scope.arel.exists
16
54
  end
17
55
  end
18
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordHas
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -13,11 +13,11 @@ ActiveRecord::Reflection::ThroughReflection.include ActiveRecordHas::ThroughMeth
13
13
  module ActiveRecordHas
14
14
  class Error < StandardError; end
15
15
 
16
- def has(association, &block)
17
- where(Scope.new(reflections[association.to_s], &block)&.exists)
16
+ def has(association, relation=nil, **options, &block)
17
+ where(Scope.new(self, association, relation, **options, &block).exists)
18
18
  end
19
19
 
20
- def has_not(association, &block)
21
- where.not(Scope.new(reflections[association.to_s], &block)&.exists)
20
+ def has_not(association, relation=nil, **options, &block)
21
+ where.not(Scope.new(self, association, relation, **options, &block).exists)
22
22
  end
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_has
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Ferney
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-15 00:00:00.000000000 Z
11
+ date: 2024-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -48,7 +48,7 @@ licenses: []
48
48
  metadata:
49
49
  homepage_uri: https://github.com/capnregex/active_record_has
50
50
  source_code_uri: https://github.com/capnregex/active_record_has
51
- changelog_uri: https://github.com/capnregex/active_record_has/releases/tag/v0.1.1
51
+ changelog_uri: https://github.com/capnregex/active_record_has/releases/tag/v0.1.2
52
52
  post_install_message:
53
53
  rdoc_options: []
54
54
  require_paths: