activerecord_nested_scope 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/CHANGELOG.md +4 -0
- data/lib/activerecord_nested_scope/builder.rb +1 -1
- data/lib/activerecord_nested_scope/node.rb +37 -7
- data/lib/activerecord_nested_scope/version.rb +1 -1
- 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: 6e06209d1f222b99ba36ec6619390cec6465e41000e4dc9100a0de23e47e962b
|
4
|
+
data.tar.gz: 7ed179db041bff9e317e0eb4e51b7e6e8877c0a2ef782cbd1e464c5fe640dec3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a2b55f78f24d9e1e1cf50f3d2260dff3e62dc70ce2ba6594835192e0189eee3b222d17a2b55e6f0ff5351a2927befcd35f0f3e660c45ec6679d5a186a3fb18a
|
7
|
+
data.tar.gz: 73a819a21306bb1ed00645063af41eea0851687c944942a8aa7a9f8b2f209980ca9bb99b007e778f197c1c40126d0585f199f9a9d5742a57937cd3fb6fd1220b
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -48,7 +48,7 @@ module ActiveRecordNestedScope
|
|
48
48
|
rels = node.children.map do |child|
|
49
49
|
relation = child_relation(child, select: child.klass.primary_key)
|
50
50
|
node.klass.where(
|
51
|
-
node.reflection.foreign_type => child.
|
51
|
+
node.reflection.foreign_type => child.source_type,
|
52
52
|
node.reflection.foreign_key => relation
|
53
53
|
)
|
54
54
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module ActiveRecordNestedScope
|
2
2
|
class Node
|
3
|
-
attr_accessor :klass, :name, :parent
|
3
|
+
attr_accessor :klass, :name, :parent, :source_type
|
4
4
|
|
5
|
-
def initialize(klass, name, parent = nil)
|
5
|
+
def initialize(klass, name, parent = nil, source_type = nil)
|
6
6
|
@klass = klass
|
7
7
|
@name = name
|
8
8
|
@parent = parent
|
9
|
+
@source_type = source_type
|
9
10
|
end
|
10
11
|
|
11
12
|
def has_options?
|
@@ -46,7 +47,8 @@ module ActiveRecordNestedScope
|
|
46
47
|
if leaf?
|
47
48
|
[]
|
48
49
|
elsif polymorphic_belongs_to?
|
49
|
-
|
50
|
+
types = PolymorphicType.new(self, reflection).resolve
|
51
|
+
types.map { |klass, source_type| Node.new(klass, @name, self, source_type) }
|
50
52
|
else
|
51
53
|
[Node.new(reflection.klass, @name, self)]
|
52
54
|
end
|
@@ -71,11 +73,39 @@ module ActiveRecordNestedScope
|
|
71
73
|
@klass.all.instance_eval(&@parent.reflection.scope) if has_scope?
|
72
74
|
end
|
73
75
|
|
74
|
-
|
76
|
+
class PolymorphicType
|
77
|
+
def initialize(node, reflection)
|
78
|
+
@node = node
|
79
|
+
@reflection = reflection
|
80
|
+
end
|
81
|
+
|
82
|
+
def resolve
|
83
|
+
types = @node.klass.unscoped.group(@reflection.foreign_type).pluck(@reflection.foreign_type).compact
|
84
|
+
types.map do |type|
|
85
|
+
klass = resolve_source_type(type)&.safe_constantize
|
86
|
+
[klass, type] if klass
|
87
|
+
end.compact
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
75
91
|
|
76
|
-
|
77
|
-
|
78
|
-
|
92
|
+
def resolve_source_type(type)
|
93
|
+
if (map = @node.options(:source_map))
|
94
|
+
resolve_from_map(type, map)
|
95
|
+
else
|
96
|
+
type
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def resolve_from_map(type, map)
|
101
|
+
if map.is_a?(Proc)
|
102
|
+
map.call(type)
|
103
|
+
elsif map.is_a?(Hash)
|
104
|
+
map[type]
|
105
|
+
else
|
106
|
+
raise ArgumentError.new("unsupported argument type for source_map option: #{map.class}.")
|
107
|
+
end
|
108
|
+
end
|
79
109
|
end
|
80
110
|
end
|
81
111
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord_nested_scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
167
|
- !ruby/object:Gem::Version
|
168
168
|
version: '0'
|
169
169
|
requirements: []
|
170
|
-
rubygems_version: 3.
|
170
|
+
rubygems_version: 3.0.3
|
171
171
|
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: An ActiveRecord extension to build nested scopes
|