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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 873946315cf0ac4297ef8a4b16db871e009886c757f380006a4b980d67fbf59b
4
- data.tar.gz: a05ed25fd35de7eb5b3418899f781105da5e8624b200da1689588f50cb00007b
3
+ metadata.gz: 6e06209d1f222b99ba36ec6619390cec6465e41000e4dc9100a0de23e47e962b
4
+ data.tar.gz: 7ed179db041bff9e317e0eb4e51b7e6e8877c0a2ef782cbd1e464c5fe640dec3
5
5
  SHA512:
6
- metadata.gz: fe922e968ee53fce622f4fe149582167b6b16336c09d49befb57476ab5d79ee5a8acf2811390ee06d87dff2b676180a220a7579ffb672e47e1d509a0b44bf65a
7
- data.tar.gz: c3954c2d58bdd3ef006556c0fb84ce613b3249784cad10a829e3eb1ce46cd5cad594ff7ef85070ba33bf025cae5c3457151680bf03ff9ced9fe0e1fb38765c91
6
+ metadata.gz: 2a2b55f78f24d9e1e1cf50f3d2260dff3e62dc70ce2ba6594835192e0189eee3b222d17a2b55e6f0ff5351a2927befcd35f0f3e660c45ec6679d5a186a3fb18a
7
+ data.tar.gz: 73a819a21306bb1ed00645063af41eea0851687c944942a8aa7a9f8b2f209980ca9bb99b007e778f197c1c40126d0585f199f9a9d5742a57937cd3fb6fd1220b
@@ -30,4 +30,4 @@ before_script:
30
30
  - bundle exec rake db:create db:migrate db:seed RAILS_ENV=test
31
31
  - cd ../..
32
32
  script:
33
- - bundle exec rspec
33
+ - DEBUG=1 bundle exec rspec
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.2.0
4
+
5
+ * Add source_map option to resolve polymorphic type.
6
+
3
7
  ## 1.1.1
4
8
 
5
9
  * Fix query for associations with primary key option.
@@ -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.klass.to_s,
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
- polymorphic_klasses.map { |klass| Node.new(klass, @name, self) }
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
- private
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
- def polymorphic_klasses
77
- types = @klass.unscoped.group(reflection.foreign_type).pluck(reflection.foreign_type).compact
78
- types.map { |type| type.safe_constantize }.compact
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
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordNestedScope
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  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.1.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-09-28 00:00:00.000000000 Z
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.1.2
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