ar-enum 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -3
- data/lib/ar/enum.rb +3 -0
- data/lib/ar/enum/command_recorder.rb +25 -0
- data/lib/ar/enum/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d0d98ef7326a262b83b299baaf0912de2452f3cfba75e4ce47365896fa82bdc
|
4
|
+
data.tar.gz: 825fb4e5a0e0911eaff410324abaf15a72251b5434dbfcbfb2f71b57cbf161c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a13ac7fde7ec4ddb9ec49182b16cfe23d8c0ce1f1e3c6313dda9714f5e0b5b7a57d608196fb9c47c43f59019a8132ecc8f1b8118f4b61fc514b9d277db2869e1
|
7
|
+
data.tar.gz: d40f5cb1237b57d14b5d188ef4bbfd56a590658ae93f5e640490692d7a961eea5fe5d2e18d4877b3cc133273f0dcde0ed54508327acb4930ec689608e21e8ede
|
data/README.md
CHANGED
@@ -26,6 +26,8 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
+
### Creating enums
|
30
|
+
|
29
31
|
To create a `enum` type, use the method `create_enum`.
|
30
32
|
|
31
33
|
```ruby
|
@@ -38,6 +40,8 @@ create_table :articles do |t|
|
|
38
40
|
end
|
39
41
|
```
|
40
42
|
|
43
|
+
### Dropping enums
|
44
|
+
|
41
45
|
To remove the enum, use `drop_enum`.
|
42
46
|
|
43
47
|
```ruby
|
@@ -57,6 +61,8 @@ change_column :articles, :status, :text
|
|
57
61
|
drop_enum :article_status
|
58
62
|
```
|
59
63
|
|
64
|
+
### Adding new labels
|
65
|
+
|
60
66
|
Use `add_enum_label` to add new values to the enum type. You can control where the value will be inserted by using `before: label` and `after: label`.
|
61
67
|
|
62
68
|
```ruby
|
@@ -72,16 +78,34 @@ add_enum_value :article_status, "unlisted", after: "draft"
|
|
72
78
|
add_enum_value :article_status, "unlisted", before: "published"
|
73
79
|
```
|
74
80
|
|
81
|
+
**WARNING:** PostgreSQL does not have a way of removing values from enum types.
|
82
|
+
|
83
|
+
### Renaming existing labels
|
84
|
+
|
75
85
|
Use `rename_enum_label` to rename a value.
|
76
86
|
|
77
87
|
```ruby
|
78
88
|
create_enum :article_status, %w[draft unlisted published]
|
79
89
|
|
80
|
-
# labels will be [draft,
|
81
|
-
rename_enum_label :article_status, "
|
90
|
+
# labels will be [draft, unlisted, live]
|
91
|
+
rename_enum_label :article_status, "published", "live"
|
82
92
|
```
|
83
93
|
|
84
|
-
|
94
|
+
### Reversing changes
|
95
|
+
|
96
|
+
The following commands can be reversed:
|
97
|
+
|
98
|
+
- `create_enum`
|
99
|
+
- `rename_enum_label`
|
100
|
+
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class CreateColorEnumClass < ActiveRecord::Migration
|
104
|
+
def change
|
105
|
+
create_enum :color, %w[red blue black]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
```
|
85
109
|
|
86
110
|
## Development
|
87
111
|
|
data/lib/ar/enum.rb
CHANGED
@@ -4,14 +4,17 @@ require "active_support/all"
|
|
4
4
|
require "active_record"
|
5
5
|
require "active_record/connection_adapters/postgresql_adapter"
|
6
6
|
require "active_record/schema_dumper"
|
7
|
+
require "active_record/migration/command_recorder"
|
7
8
|
require "ar/enum/version"
|
8
9
|
|
9
10
|
module AR
|
10
11
|
module Enum
|
11
12
|
require "ar/enum/adapter"
|
12
13
|
require "ar/enum/schema_dumper"
|
14
|
+
require "ar/enum/command_recorder"
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
18
|
+
ActiveRecord::Migration::CommandRecorder.include AR::Enum::CommandRecorder
|
16
19
|
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include AR::Enum::Adapter
|
17
20
|
ActiveRecord::SchemaDumper.prepend AR::Enum::SchemaDumper
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AR
|
4
|
+
module Enum
|
5
|
+
module CommandRecorder
|
6
|
+
def create_enum(name, values)
|
7
|
+
record(__method__, [name, values])
|
8
|
+
end
|
9
|
+
|
10
|
+
def rename_enum_label(name, from, to)
|
11
|
+
record(__method__, [name, from, to])
|
12
|
+
end
|
13
|
+
|
14
|
+
def invert_create_enum(args)
|
15
|
+
name, _ = args
|
16
|
+
[:drop_enum, name]
|
17
|
+
end
|
18
|
+
|
19
|
+
def invert_rename_enum_label(args)
|
20
|
+
name, to, from = args
|
21
|
+
[:rename_enum_label, [name, from, to]]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/ar/enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/ar-enum.rb
|
130
130
|
- lib/ar/enum.rb
|
131
131
|
- lib/ar/enum/adapter.rb
|
132
|
+
- lib/ar/enum/command_recorder.rb
|
132
133
|
- lib/ar/enum/schema_dumper.rb
|
133
134
|
- lib/ar/enum/version.rb
|
134
135
|
homepage: https://rubygems.org/gems/ar-enum
|