taylors_enum 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +36 -36
- data/lib/taylors_enum/active_record/active_record.rb +2 -0
- data/lib/taylors_enum/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ccd0e1d92794d23e9e4d1a046dd58ff4be1425516ca6e9dc0b9054abc2c8501
|
4
|
+
data.tar.gz: 603dcf86a1b9e8ae383927a3f0fc466ee06eeda9d288b92037cb755a4e7c4d7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb6fd40400339f55597c6bee8d64524e7fefb8f4ab853445aa6e92a4bcbd5c4825f4dfd1b368561b6504753a77ea698453346a533b6466abf3271dae653ef813
|
7
|
+
data.tar.gz: 70b9a9dfbb726e94dc6426fb8757e22922d174880870bb017124c0a67b924f23c76ac7d21fc4af4171e092f73d2991d44ae86f25250660984a272f06df99ddc4
|
data/README.md
CHANGED
@@ -2,48 +2,48 @@
|
|
2
2
|
|
3
3
|
taylors_enum is a gem that builds on top of ActiveRecord's built in [enums](https://api.rubyonrails.org/v5.2.4.4/classes/ActiveRecord/Enum.html#method-i-enum). Specifically, it will:
|
4
4
|
|
5
|
-
|
5
|
+
##### - Define additional methods to make it clearer which values exist in Rails-land, and which in the database
|
6
6
|
In Rails:
|
7
|
-
```
|
7
|
+
```ruby
|
8
8
|
class Album < ActiveRecord::Base
|
9
9
|
self.table_name = 'albums'
|
10
10
|
enum name: %w[debut fearless speak_now red nineteen_eighty_nine reputation lover folklore evermore]
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
$ Album.names
|
14
14
|
=> {"debut"=>0, "fearless"=>1, "speak_now"=>2, "red"=>3, "nineteen_eighty_nine"=>4, "reputation"=>5, "lover"=>6, "folklore"=>7, "evermore"=>8}
|
15
15
|
```
|
16
16
|
|
17
17
|
With taylors_enum:
|
18
|
-
```
|
18
|
+
```ruby
|
19
19
|
class AlbumBase < Album
|
20
20
|
taylors_enum name: %w[debut fearless speak_now red nineteen_eighty_nine reputation lover folklore evermore]
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
$ AlbumBase.name_rails_values
|
24
24
|
=> ["debut", "fearless", "speak_now", "red", "nineteen_eighty_nine", "reputation", "lover", "folklore", "evermore"]
|
25
|
-
|
25
|
+
$ AlbumBase.name_database_values
|
26
26
|
=> ["debut", "fearless", "speak_now", "red", "nineteen_eighty_nine", "reputation", "lover", "folklore", "evermore"]
|
27
27
|
|
28
28
|
```
|
29
29
|
|
30
30
|
|
31
|
-
|
32
|
-
```
|
31
|
+
##### - Define constants for each value provided
|
32
|
+
```ruby
|
33
33
|
class AlbumBase < Album
|
34
34
|
taylors_enum name: %w[debut fearless speak_now red nineteen_eighty_nine reputation lover folklore evermore]
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
$ AlbumBase::FOLKLORE
|
38
38
|
=> "folklore"
|
39
|
-
|
39
|
+
$ AlbumBase::EVERMORE
|
40
40
|
=> "evermore"
|
41
41
|
# etc.
|
42
42
|
|
43
43
|
```
|
44
44
|
|
45
|
-
|
46
|
-
```
|
45
|
+
##### - Enables support for all the enum goodness when enumerating classes for Single Table Inheritance
|
46
|
+
```ruby
|
47
47
|
module SingleTableInheritanceAlbums
|
48
48
|
class SingleTableInheritanceAlbum < ActiveRecord::Base
|
49
49
|
self.table_name = 'single_table_inheritance_albums'
|
@@ -72,56 +72,56 @@ module SingleTableInheritanceAlbums
|
|
72
72
|
class Evermore < SingleTableInheritanceAlbum; end
|
73
73
|
end
|
74
74
|
|
75
|
-
|
75
|
+
$ album = ::SingleTableInheritanceAlbums::Debut.create
|
76
76
|
=> #<SingleTableInheritanceAlbums::Debut:0x000000010581a708 id: 2, type: "SingleTableInheritanceAlbums::Debut", created_at: 2022-06-04 14:34:24.951932 UTC, updated_at: 2022-06-04 14:34:24.951932 UTC>
|
77
|
-
|
77
|
+
$ album.type
|
78
78
|
=> "SingleTableInheritanceAlbums::Debut"
|
79
|
-
|
79
|
+
$ album.debut?
|
80
80
|
=> true
|
81
|
-
|
81
|
+
$ album.fearless!
|
82
82
|
=> true
|
83
|
-
|
83
|
+
$ album.debut?
|
84
84
|
=> false
|
85
|
-
|
85
|
+
$ album.fearless?
|
86
86
|
=> true
|
87
|
-
|
87
|
+
$ album.type
|
88
88
|
=> "SingleTableInheritanceAlbums::Fearless"
|
89
|
-
|
89
|
+
$ ::SingleTableInheritanceAlbums::SingleTableInheritanceAlbum.fearless
|
90
90
|
=> [#<SingleTableInheritanceAlbums::Fearless:0x00000001059821e0 id: 1, type: "SingleTableInheritanceAlbums::Fearless", created_at: 2022-06-04 14:33:55.178418 UTC, updated_at: 2022-06-04 14:33:55.178418 UTC>,
|
91
91
|
#<SingleTableInheritanceAlbums::Fearless:0x0000000105981a10 id: 2, type: "SingleTableInheritanceAlbums::Fearless", created_at: 2022-06-04 14:34:24.951932 UTC, updated_at: 2022-06-04 14:34:50.616316 UTC>]
|
92
|
-
|
92
|
+
$ ::SingleTableInheritanceAlbums::SingleTableInheritanceAlbum.fearless.to_sql
|
93
93
|
=> "SELECT \"single_table_inheritance_albums\".* FROM \"single_table_inheritance_albums\" WHERE \"single_table_inheritance_albums\".\"type\" = 'SingleTableInheritanceAlbums::Fearless'"
|
94
94
|
```
|
95
95
|
|
96
|
-
|
96
|
+
##### - Enables support for all the enum goodness when enumerating classes for Polymorphic Associations
|
97
97
|
Note: this excludes the `<attribute>!` method to update a value, as, as I write this, I can't conceive of a scenario in which you'd want to update just the type, and not also the ID, of an associated object. That doesn't mean you won't ever want to! And if you do, you can still do so manually via the `ActiveRecord#update` [method](https://guides.rubyonrails.org/active_record_basics.html#update).
|
98
98
|
|
99
|
-
```
|
99
|
+
```ruby
|
100
100
|
class Award < ActiveRecord::Base
|
101
101
|
self.table_name = 'awards'
|
102
102
|
belongs_to :awardable, polymorphic: true
|
103
103
|
taylors_enum awardable_type: %w[Album Song], polymorphic: true
|
104
104
|
end
|
105
105
|
|
106
|
-
|
106
|
+
$ song = Song.create!
|
107
107
|
=> #<Song:0x0000000105beddd0 id: 1, name: nil, created_at: 2022-06-04 14:39:38.837973 UTC, updated_at: 2022-06-04 14:39:38.837973 UTC>
|
108
|
-
|
108
|
+
$ album = AlbumBase.create!(name: :folklore)
|
109
109
|
=> #<AlbumBase:0x0000000105c36c10 id: 1, name: "folklore", created_at: 2022-06-04 14:39:43.001592 UTC, updated_at: 2022-06-04 14:39:43.001592 UTC>
|
110
|
-
|
110
|
+
$ song_award = Award.create!(awardable: song)
|
111
111
|
=> #<Award:0x0000000105c77030 id: 1, awardable_type: "Song", awardable_id: "1", created_at: 2022-06-04 14:39:46.714235 UTC, updated_at: 2022-06-04 14:39:46.714235 UTC>
|
112
|
-
|
112
|
+
$ album_award = Award.create!(awardable: album)
|
113
113
|
=> #<Award:0x0000000105cb4ac0 id: 2, awardable_type: "Album", awardable_id: "1", created_at: 2022-06-04 14:39:49.444071 UTC, updated_at: 2022-06-04 14:39:49.444071 UTC>
|
114
|
-
|
114
|
+
$ song_award.song?
|
115
115
|
=> true
|
116
|
-
|
116
|
+
$ song_award.album?
|
117
117
|
=> false
|
118
|
-
|
118
|
+
$ album_award.song?
|
119
119
|
=> false
|
120
|
-
|
120
|
+
$ album_award.album?
|
121
121
|
=> true
|
122
|
-
|
122
|
+
$ Award.song
|
123
123
|
=> [#<Award:0x0000000105dbfca8 id: 1, awardable_type: "Song", awardable_id: "1", created_at: 2022-06-04 14:39:46.714235 UTC, updated_at: 2022-06-04 14:39:46.714235 UTC>]
|
124
|
-
|
124
|
+
$ Award.song.to_sql
|
125
125
|
=> "SELECT \"awards\".* FROM \"awards\" WHERE \"awards\".\"awardable_type\" = 'Song'"
|
126
126
|
```
|
127
127
|
|
@@ -158,14 +158,14 @@ taylors_enum also takes a series of options, provided as a hash following the sp
|
|
158
158
|
- `integer`: defaults to `false`. When using taylors_enum with an integer column rather than a string column, pass this as `true` to ensure constants, scopes, and helper methods are defined appropriately. Should this be something that can be inferred, rather than needing to be passed explicitly? Absolutely. However, at the time of writing, I can't figure out how to get access to this information at the point that this code is called (in the `ActiveSupport::LazyLoadHooks.on_load` [callback](https://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html)), so this fudge allows integer columns to function as expected for now. Note: this cannot be passed as `true` if `single_table_inheritance` or `polymorphic` are both specified as `true`; this will raise an OptionsConflictError when loading the application.
|
159
159
|
|
160
160
|
If you want to see the base Rails value that will be used to generate the `value?` and `value!` methods, that `VALUE` constants, and the `value` scopes, you can load up a Rails console with `rails c`, and run `MyModel.check_rails_value_for(database_value)`. For example:
|
161
|
-
```
|
161
|
+
```ruby
|
162
162
|
class AlbumBase < Album
|
163
163
|
taylors_enum name: %w[debut fearless speak_now red nineteen_eighty_nine reputation lover folklore evermore]
|
164
164
|
end
|
165
165
|
|
166
|
-
|
166
|
+
$ AlbumBase.check_rails_value_for('folklore')
|
167
167
|
=> 'folklore'
|
168
|
-
|
168
|
+
$ AlbumBase.check_rails_value_for('folklore', column: :name, prefix: true)
|
169
169
|
=> 'name_folklore'
|
170
170
|
```
|
171
171
|
|
data/lib/taylors_enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: taylors_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cassie Johnstone
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|