discriminable 2.1.0 → 2.1.1
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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +33 -7
- data/TODO.md +5 -2
- data/lib/discriminable/version.rb +1 -1
- data/lib/discriminable.rb +9 -4
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b312ea73ac2afbbe72341feffdf97fce4edeee0e918d54ae33ea4b0c822b08cb
|
4
|
+
data.tar.gz: 7f56a7e7fe5b6668f37d1dc8c08730f5fd5bf6a06668b8402a5194b23cb9ba88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53c746dbd6c1db4286132420909652551a0471043f29e8a6a1dae37b74905253ae6bdd9f78904a941474e6cdee24cf90f33d85d2e220bdaf841f162c11a54383
|
7
|
+
data.tar.gz: daa33d8edf5ebc3b336388f66f22b826a7fab98f022c27c80dfa5f5068e8d3b537cccf7f9e12f59fe22d5a4ab742aece66f907e33cfd5924864d972cea1201c9
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
### [2.1.1](https://github.com/gregorw/discriminable/compare/v2.1.0...v2.1.1) (2022-04-15)
|
4
|
+
|
5
|
+
|
6
|
+
### Bug Fixes
|
7
|
+
|
8
|
+
* Subclass is not properly initialized on new objects ([c24706c](https://github.com/gregorw/discriminable/commit/c24706c9e8cd4c80ea20016e341f010a5a3c792b))
|
9
|
+
* Use correct, i.e. first value, when using multiple states for same subclass ([3a05c0d](https://github.com/gregorw/discriminable/commit/3a05c0d3a76080a39f40b1d57ce2862316693f97))
|
10
|
+
|
3
11
|
## [2.1.0](https://github.com/gregorw/discriminable/compare/v2.0.0...v2.1.0) (2022-04-01)
|
4
12
|
|
5
13
|
|
data/README.md
CHANGED
@@ -1,10 +1,20 @@
|
|
1
|
-
|
1
|
+
|
2
2
|
|
3
3
|
# Discriminable
|
4
4
|
|
5
|
-
|
5
|
+
[](https://rubygems.org/gems/discriminable)
|
6
|
+
[](https://github.com/gregorw/discriminable/actions/workflows/main.yml)
|
7
|
+
[](https://codeclimate.com/github/gregorw/discriminable/maintainability)
|
8
|
+
[](https://codeclimate.com/github/gregorw/discriminable/test_coverage)
|
9
|
+
|
10
|
+
Single table inheritance (STI) for Ruby on Rails models (ActiveRecord) using enum, boolean, string and integer column types.
|
11
|
+
|
12
|
+
In other words, use any _existing_ model attribute for STI instead of storing class names in a `type` column.
|
13
|
+
|
14
|
+
**Related work**
|
15
|
+
|
16
|
+
The idea was originally described in [“Bye Bye STI, Hello Discriminable Model”](https://www.salsify.com/blog/engineering/bye-bye-sti-hello-discriminable-model) by Randy Burkes and this Gem has started out with [his code](https://gist.github.com/rlburkes/798e186acb2f93e787a5).
|
6
17
|
|
7
|
-
TODO: Delete this and the text above, and describe your gem
|
8
18
|
|
9
19
|
## Installation
|
10
20
|
|
@@ -18,13 +28,29 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
18
28
|
|
19
29
|
## Usage
|
20
30
|
|
21
|
-
|
31
|
+
```ruby
|
32
|
+
ActiveRecord::Schema.define do
|
33
|
+
create_table :orders do |t|
|
34
|
+
t.integer :state, limit: 1, default: 0
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Order < ActiveRecord::Base
|
39
|
+
include Discriminable
|
40
|
+
|
41
|
+
enum state: { open: 0, completed: 1 }
|
42
|
+
discriminable state: { open: "Cart" }
|
43
|
+
end
|
22
44
|
|
23
|
-
|
45
|
+
class Cart < Order
|
46
|
+
end
|
24
47
|
|
25
|
-
|
48
|
+
Cart.create
|
49
|
+
=> #<Cart id: 1, state: "open">
|
50
|
+
Order.all
|
51
|
+
=> #<ActiveRecord::Relation [#<Cart id: 1, state: "open">]>
|
52
|
+
```
|
26
53
|
|
27
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
54
|
|
29
55
|
## Contributing
|
30
56
|
|
data/TODO.md
CHANGED
data/lib/discriminable.rb
CHANGED
@@ -25,7 +25,7 @@ module Discriminable
|
|
25
25
|
|
26
26
|
included do
|
27
27
|
class_attribute :discriminable_map, instance_writer: false
|
28
|
-
class_attribute :
|
28
|
+
class_attribute :discriminable_inverse_map, instance_writer: false
|
29
29
|
end
|
30
30
|
|
31
31
|
# Specify the column to use for discrimination.
|
@@ -36,12 +36,16 @@ module Discriminable
|
|
36
36
|
discriminator, map = options.first
|
37
37
|
|
38
38
|
self.discriminable_map = map.with_indifferent_access
|
39
|
-
|
39
|
+
|
40
|
+
# Use first key as default discriminator
|
41
|
+
# { a: "C", b: "C" }.invert => { "C" => :b }
|
42
|
+
# { a: "C", b: "C" }.to_a.reverse.to_h.invert => { "C" => :a }
|
43
|
+
self.discriminable_inverse_map = map.to_a.reverse.to_h.invert
|
40
44
|
self.inheritance_column = discriminator.to_s
|
41
45
|
end
|
42
46
|
|
43
47
|
def sti_name
|
44
|
-
|
48
|
+
discriminable_inverse_map[name]
|
45
49
|
end
|
46
50
|
|
47
51
|
def sti_class_for(value)
|
@@ -57,7 +61,8 @@ module Discriminable
|
|
57
61
|
attrs = attrs.to_h if attrs.respond_to?(:permitted?)
|
58
62
|
return unless attrs.is_a?(Hash)
|
59
63
|
|
60
|
-
value =
|
64
|
+
value = attrs.with_indifferent_access[inheritance_column]
|
65
|
+
value = base_class.type_for_attribute(inheritance_column).cast(value)
|
61
66
|
sti_class_for(value)
|
62
67
|
end
|
63
68
|
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.1.
|
4
|
+
version: 2.1.1
|
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-
|
11
|
+
date: 2022-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.26'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.17.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.17.0
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: sqlite3
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|