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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6d47dda88efcbf4f311ff843e0b4e3e06ffda5f205d810e9a4ab4ad089d13ad
4
- data.tar.gz: 64d5ad043e453d28d98603d5c3153783ac153c97bcd70179ea86e962bcdd4f85
3
+ metadata.gz: b312ea73ac2afbbe72341feffdf97fce4edeee0e918d54ae33ea4b0c822b08cb
4
+ data.tar.gz: 7f56a7e7fe5b6668f37d1dc8c08730f5fd5bf6a06668b8402a5194b23cb9ba88
5
5
  SHA512:
6
- metadata.gz: 1e272415e3aef0d7c365b0189564261ac138029d3feb1e8bcc7c01468132763f2af723c8b73df8806f5f313f30b0d7bbbe070b00f3f67ddbb4103677aa6d3b3e
7
- data.tar.gz: 04c96941797c296976f88ecf00b84c51c26d9dcfd954012c3abec8ae17dec6bcd03d6958f47af2e01ba93bd206ca42f3e8c5fa0eff5019e7925e81d28f41ce24
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
- [![Ruby](https://github.com/gregorw/discriminable/actions/workflows/main.yml/badge.svg)](https://github.com/gregorw/discriminable/actions/workflows/main.yml)
1
+
2
2
 
3
3
  # Discriminable
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/discriminable`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ [![Gem Version](https://badge.fury.io/rb/discriminable.svg)](https://rubygems.org/gems/discriminable)
6
+ [![CI](https://github.com/gregorw/discriminable/actions/workflows/main.yml/badge.svg)](https://github.com/gregorw/discriminable/actions/workflows/main.yml)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/94041c5f946b64040368/maintainability)](https://codeclimate.com/github/gregorw/discriminable/maintainability)
8
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/94041c5f946b64040368/test_coverage)](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
- TODO: Write usage instructions here
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
- ## Development
45
+ class Cart < Order
46
+ end
24
47
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
@@ -1,2 +1,5 @@
1
- - [ ] `self.abstract_class = true`
2
- - [ ]
1
+ - [ ] multiple values per subclass
2
+ - [ ] default to first value when using hash syntax
3
+ - [ ] open-closed principle
4
+ - [ ] test permitted attributes
5
+ - [ ] `self.abstract_class = true`
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Discriminable
4
- VERSION = "2.1.0"
4
+ VERSION = "2.1.1"
5
5
  end
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 :discriminable_inverse, instance_writer: false
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
- self.discriminable_inverse = map.invert
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
- discriminable_inverse[name]
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 = base_class.type_for_attribute(inheritance_column).cast(attrs[inheritance_column])
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.0
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-01 00:00:00.000000000 Z
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