discriminable 2.2.2 → 2.2.3

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: 4a89c1ad10593a4aa548c3792343092ef153d6a9a5abd0a65ac253005dcb1b69
4
- data.tar.gz: 67db220cf69fa783068f8eb9b2d4852f4a44066926da4703ac41233ff62f88d7
3
+ metadata.gz: 77f7e596256a4d211a4437b0a52d9e0f40d03a038e48c5ce6332083f7f910da9
4
+ data.tar.gz: cdaf98658eea3b2faf0bd6193c38fd83d010141c08d7c8cff643f331b51c0986
5
5
  SHA512:
6
- metadata.gz: dbdd8c37b95997ccf9daf4556a79d7248e9d20165ecf8f51022961ed3ae884cb3a16316ff7e8aa7669da53af38e6fba5f9e1a61fcbe13b370c536cdc5bd5fbb1
7
- data.tar.gz: 94af960d7e56c3b913faf4d2c5ef721d0400c8e3235884b03cc77707f4015372c12cd108370de41016ffabb7e202217eab9cb14657d566b161b82567b307479f
6
+ metadata.gz: eabc2a7dd9bbbce30010f777ffc782e9f875f4c9b5c67daa2d762c224a4493b6f48d74a52b43696de29f0ef2de57128c52e2e83612362e78f896645594e5ef95
7
+ data.tar.gz: da4b9c0a03acca87af217b97d996077d8d60e79fe43bf1dc0baaacde5c465b3911ce0259422382c0c9ec0a3aaeea70adaf738aa62d36f1401c0763d3f6f4fa44
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ### [2.2.3](https://github.com/gregorw/discriminable/compare/v2.2.2...v2.2.3) (2022-04-20)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * Allow to use attribute aliases ([448e4e8](https://github.com/gregorw/discriminable/commit/448e4e81925543f1ee4949898f0ac6628eb2d3fd))
9
+
3
10
  ### [2.2.2](https://github.com/gregorw/discriminable/compare/v2.2.1...v2.2.2) (2022-04-17)
4
11
 
5
12
 
data/TODO.md CHANGED
@@ -1,6 +1,10 @@
1
1
  - [x] multiple values per subclass
2
2
  - [x] default to first value when using hash syntax
3
3
  - [x] open-closed principle
4
- - [ ] use `type` column with enum, int, string, etc.
4
+ - [ ] Bug: multiple values: Child.all query
5
+ - [ ] more tests / examples
6
+ - [ ] Rails 5 support
5
7
  - [ ] test permitted attributes
6
8
  - [ ] `self.abstract_class = true`
9
+ - [ ] scoping…
10
+ - [ ] ~~use `type` column with enum, int, string, etc.~~
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Discriminable
4
- VERSION = "2.2.2"
4
+ VERSION = "2.2.3"
5
5
  end
data/lib/discriminable.rb CHANGED
@@ -36,28 +36,28 @@ module Discriminable
36
36
 
37
37
  attribute, map = options.first
38
38
 
39
+ # E.g. { value: "ClassName" }
39
40
  self.discriminable_map = map.with_indifferent_access
40
41
 
41
42
  # Use first key as default discriminator
42
43
  # { a: "C", b: "C" }.invert => { "C" => :b }
43
44
  # { a: "C", b: "C" }.to_a.reverse.to_h.invert => { "C" => :a }
45
+ # E.g. { "ClassName" => :value }
44
46
  self.discriminable_inverse_map = map.to_a.reverse.to_h.invert
45
- self.inheritance_column = attribute.to_s
47
+
48
+ attribute = attribute.to_s
49
+ self.inheritance_column = attribute_aliases[attribute] || attribute
46
50
  end
47
51
 
48
- # rubocop:disable Metrics/AbcSize
49
52
  def discriminable_by(attribute)
50
53
  raise "Subclasses should not override .discriminable_by" unless base_class?
51
54
 
52
- self.inheritance_column = attribute.to_s
53
- self.discriminable_map ||= Hash.new do |map, value|
54
- map[value] = discriminable_descendants(value)&.name
55
- end
56
- self.discriminable_inverse_map ||= Hash.new do |map, value|
57
- map[value] = value.constantize.discriminable_values&.first
58
- end
55
+ self.discriminable_map ||= discriminable_map_memoized
56
+ self.discriminable_inverse_map ||= discriminable_inverse_map_memoized
57
+
58
+ attribute = attribute.to_s
59
+ self.inheritance_column = attribute_aliases[attribute] || attribute
59
60
  end
60
- # rubocop:enable Metrics/AbcSize
61
61
 
62
62
  def discriminable_as(*values)
63
63
  raise "Only subclasses should specify .discriminable_as" if base_class?
@@ -67,6 +67,7 @@ module Discriminable
67
67
  end
68
68
  end
69
69
 
70
+ # This is the value of the discriminable attribute
70
71
  def sti_name
71
72
  discriminable_inverse_map[name]
72
73
  end
@@ -84,13 +85,27 @@ module Discriminable
84
85
  attrs = attrs.to_h if attrs.respond_to?(:permitted?)
85
86
  return unless attrs.is_a?(Hash)
86
87
 
87
- value = attrs.with_indifferent_access[inheritance_column]
88
- value = base_class.type_for_attribute(inheritance_column).cast(value)
88
+ value = discriminable_value(attrs)
89
89
  sti_class_for(value)
90
90
  end
91
91
 
92
- def discriminable_descendants(value)
93
- descendants.detect { |d| d.discriminable_values.include? value }
92
+ def discriminable_map_memoized
93
+ Hash.new do |map, value|
94
+ map[value] = descendants.detect { |d| d.discriminable_values.include? value }&.name
95
+ end
96
+ end
97
+
98
+ def discriminable_inverse_map_memoized
99
+ Hash.new do |map, value|
100
+ map[value] = value.constantize.discriminable_values&.first
101
+ end
102
+ end
103
+
104
+ def discriminable_value(attrs)
105
+ attrs = attrs.with_indifferent_access
106
+ value = attrs[inheritance_column]
107
+ value ||= attrs[attribute_aliases.invert[inheritance_column]]
108
+ base_class.type_for_attribute(inheritance_column).cast(value)
94
109
  end
95
110
  end
96
111
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discriminable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregor Wassmann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-17 00:00:00.000000000 Z
11
+ date: 2022-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord