discriminable 2.2.3 → 2.2.4

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: 77f7e596256a4d211a4437b0a52d9e0f40d03a038e48c5ce6332083f7f910da9
4
- data.tar.gz: cdaf98658eea3b2faf0bd6193c38fd83d010141c08d7c8cff643f331b51c0986
3
+ metadata.gz: 91332d7c33b8d092f2071dd5d4a59b12786058d8347ba844a8426354133f4e2c
4
+ data.tar.gz: b743b5f2db1ea1690a5bad69b6b046a58dd14e76c64e47de03dd18a8b57b8003
5
5
  SHA512:
6
- metadata.gz: eabc2a7dd9bbbce30010f777ffc782e9f875f4c9b5c67daa2d762c224a4493b6f48d74a52b43696de29f0ef2de57128c52e2e83612362e78f896645594e5ef95
7
- data.tar.gz: da4b9c0a03acca87af217b97d996077d8d60e79fe43bf1dc0baaacde5c465b3911ce0259422382c0c9ec0a3aaeea70adaf738aa62d36f1401c0763d3f6f4fa44
6
+ metadata.gz: f0a230f1257e8a53e914c8ac39b879143a897ca0d909441403494afa2e0ba2a48ea529b58efb85b8946c97416dc407ac7bf5cda60a12482cec0aceff22cc6169
7
+ data.tar.gz: abcc79722fafc181c21f1e27220c4ce46de790e90499de8dd97c94f568b1143d094d6498c93d5975836ec3781fba5e38d8ba51e8b8f002ba1e8dfb4d1d20bb25
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ### [2.2.4](https://github.com/gregorw/discriminable/compare/v2.2.3...v2.2.4) (2022-04-25)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * changelog url ([3054a1a](https://github.com/gregorw/discriminable/commit/3054a1a0137d8d3cccb55d7f2f06cb392053dab0))
9
+ * support querying when multiple values are provided ([f75190b](https://github.com/gregorw/discriminable/commit/f75190bcf33dcbddf507626bc77aa4709a594f4d))
10
+
3
11
  ### [2.2.3](https://github.com/gregorw/discriminable/compare/v2.2.2...v2.2.3) (2022-04-20)
4
12
 
5
13
 
data/README.md CHANGED
@@ -7,24 +7,17 @@
7
7
  [![Maintainability](https://api.codeclimate.com/v1/badges/94041c5f946b64040368/maintainability)](https://codeclimate.com/github/gregorw/discriminable/maintainability)
8
8
  [![Test Coverage](https://api.codeclimate.com/v1/badges/94041c5f946b64040368/test_coverage)](https://codeclimate.com/github/gregorw/discriminable/test_coverage)
9
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).
10
+ This is a Ruby gem that implements single-table inheritance (STI) for ActiveRecord models using string, integer and boolean column types.
17
11
 
12
+ In other words, use any (existing) model attribute to discriminate between different classes in your class hierarchy. This makes storing class names in a `type` column redundant.
18
13
 
19
14
  ## Installation
20
15
 
21
- Install the gem and add to the application's Gemfile by executing:
22
-
23
- $ bundle add discriminable
16
+ bundle add discriminable
24
17
 
25
- If bundler is not being used to manage dependencies, install the gem by executing:
18
+ or
26
19
 
27
- $ gem install discriminable
20
+ gem install discriminable
28
21
 
29
22
  ## Usage
30
23
 
@@ -32,28 +25,29 @@ If bundler is not being used to manage dependencies, install the gem by executin
32
25
  class Order < ActiveRecord::Base
33
26
  include Discriminable
34
27
 
35
- enum state: { open: 0, completed: 1 }
36
- discriminable state: { open: "Cart" }
28
+ discriminable_by :state
37
29
  end
38
30
 
39
31
  class Cart < Order
32
+ discriminable_as :open
40
33
  end
41
34
 
42
35
  Cart.create
43
- => #<Cart id: 1, state: "open">
36
+ # => #<Cart id: 1, state: "open">
44
37
  Order.all
45
- => #<ActiveRecord::Relation [#<Cart id: 1, state: "open">]>
38
+ # => #<ActiveRecord::Relation [#<Cart id: 1, state: "open">]>
46
39
  ```
47
40
 
48
- ### Alternative syntax
41
+ ## Features
49
42
 
50
- Instead of defining subclass names within the parent you may prefer to be open for extension but closed for modification (open-closed principle) using the alternative syntax.
43
+ ### Compatible with enums
51
44
 
52
45
  ```ruby
53
46
  class Order < ActiveRecord::Base
54
47
  include Discriminable
55
48
 
56
- enum state: { open: 0, completed: 1 }
49
+ enum state: { open: 0, processing: 1, invoiced: 2 }
50
+
57
51
  discriminable_by :state
58
52
  end
59
53
 
@@ -61,12 +55,42 @@ class Cart < Order
61
55
  discriminable_as :open
62
56
  end
63
57
 
64
- Cart.create
65
- => #<Cart id: 1, state: "open">
66
- Order.all
67
- => #<ActiveRecord::Relation [#<Cart id: 1, state: "open">]>
58
+ class Invoice < Order
59
+ discriminable_as :invoiced
60
+ end
68
61
  ```
69
62
 
63
+ ### Aliased attributes
64
+
65
+ In case you are working with a legacy database and cannot change the column name easily it’s easy to reference an aliased attribute in the `discriminable_by` definition.
66
+
67
+ ```ruby
68
+ class Property < ActiveRecord::Base
69
+ include Discriminable
70
+
71
+ alias_attribute :kind, :kind_with_legacy_postfix
72
+
73
+ # Aliased attributes are supported when specifying the discriminable attribute
74
+ discriminable_by :kind
75
+ end
76
+
77
+ class NumberProperty < Property
78
+ discriminable_as 1
79
+ end
80
+ ```
81
+
82
+ ### Multiple values
83
+
84
+ Sometimes, in a real project, you may want to map a number of values to a single class. This is possible by specifying:
85
+
86
+ ```ruby
87
+ class OptionProperty < Property
88
+ # The first mention becomes the default value
89
+ discriminable_as 2, 3, 4
90
+ end
91
+ ```
92
+
93
+ Note that when creating new records with e.g. `OptionProperty.create` a _default_ value needs to be set in the database for this discriminable class. The Discriminable gem uses the _first_ value in the list as the default.
70
94
 
71
95
  ## Contributing
72
96
 
@@ -79,3 +103,14 @@ The gem is available as open source under the terms of the [MIT License](https:/
79
103
  ## Code of Conduct
80
104
 
81
105
  Everyone interacting in the Discriminable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/gregorw/discriminable/blob/main/CODE_OF_CONDUCT.md).
106
+
107
+ ## Related work
108
+
109
+ The idea for this Gem was influenced by [“Bye Bye STI, Hello Discriminable Model”](https://www.salsify.com/blog/engineering/bye-bye-sti-hello-discriminable-model) by Randy Burkes. This Gem has started out with [his code](https://gist.github.com/rlburkes/798e186acb2f93e787a5).
110
+
111
+ See also:
112
+
113
+ - Rails [single table inheritance](https://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html) and [DelegatedType](https://api.rubyonrails.org/classes/ActiveRecord/DelegatedType.html)
114
+ - Java [JPA discrimanator](https://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/jpa_overview_mapping_discrim.html)
115
+ - Python [model inheritance](https://docs.djangoproject.com/en/4.0/topics/db/models/#model-inheritance-1)
116
+ - [Discriminator](https://github.com/gdpelican/discriminator) gem.
data/TODO.md CHANGED
@@ -1,10 +1,12 @@
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
- - [ ] Bug: multiple values: Child.all query
4
+ - [x] Bug: multiple values: Child.all query (double-check)
5
+ - [ ] Rails 5 support (see rails-5 branch)
6
+ - [ ] rubocop-minitest
5
7
  - [ ] more tests / examples
6
- - [ ] Rails 5 support
8
+ - [ ] Documentation
7
9
  - [ ] test permitted attributes
8
- - [ ] `self.abstract_class = true`
9
- - [ ] scoping…
10
+ - [ ] scoping… should work OOTB
11
+ - [ ] ~~`self.abstract_class = true` ➔ This results in separate tables~~
10
12
  - [ ] ~~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.3"
4
+ VERSION = "2.2.4"
5
5
  end
data/lib/discriminable.rb CHANGED
@@ -72,6 +72,17 @@ module Discriminable
72
72
  discriminable_inverse_map[name]
73
73
  end
74
74
 
75
+ def sti_names
76
+ ([self] + descendants).flat_map(&:discriminable_values)
77
+ end
78
+
79
+ def type_condition(table = arel_table)
80
+ return super unless discriminable_values.present?
81
+
82
+ sti_column = table[inheritance_column]
83
+ predicate_builder.build(sti_column, sti_names)
84
+ end
85
+
75
86
  def sti_class_for(value)
76
87
  return self unless (type_name = discriminable_map[value])
77
88
 
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.3
4
+ version: 2.2.4
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-20 00:00:00.000000000 Z
11
+ date: 2022-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -137,7 +137,7 @@ licenses:
137
137
  metadata:
138
138
  homepage_uri: https://github.com/gregorw/discriminable
139
139
  source_code_uri: https://github.com/gregorw/discriminable
140
- changelog_uri: https://github.com/gregorw/discriminable/CHANGELOG.md
140
+ changelog_uri: https://github.com/gregorw/discriminable/blob/main/CHANGELOG.md
141
141
  rubygems_mfa_required: 'false'
142
142
  post_install_message:
143
143
  rdoc_options: []