active_record_has 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_record_has/scope.rb +44 -6
- data/lib/active_record_has/version.rb +1 -1
- data/lib/active_record_has.rb +4 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8e6638722e38048167aaadfc8a1d56f0381808394023137b361a58d415d2408
|
4
|
+
data.tar.gz: a1fabbc4b3002c51a0504cdad5e60ee105f1d44c8e4ccbe5243ed9cd7280812b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 :
|
6
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
53
|
+
scope.arel.exists
|
16
54
|
end
|
17
55
|
end
|
18
56
|
end
|
data/lib/active_record_has.rb
CHANGED
@@ -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(
|
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(
|
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.
|
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-
|
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.
|
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:
|