activerecord-pg_enum 0.1.1 → 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/CHANGELOG.md +4 -0
- data/README.md +57 -1
- data/lib/active_record/pg_enum/schema_dumper.rb +1 -1
- data/lib/active_record/pg_enum/schema_statements.rb +3 -3
- data/lib/active_record/pg_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: cd614eca633279bd5a19660137152b8461356b8431fea368a3c3a947c64715f7
|
4
|
+
data.tar.gz: ce1bba6131f601029c42402e2561869530f4a2ff2bc0d38a56b84c3ba9226f03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '095f6470ee10ea1c3499b8452cf5e0d9e8d8e5cbc937e91969014574c6558eec1bc30a85306017a98a5e6ae2ddc631381ca91af5d4eb8a4c8991d50dc4575bf2'
|
7
|
+
data.tar.gz: ad1ecac7919ea5e73a0653bc8700403b002a5ca518ef305d713c2a22f1ab8bd13ae17f54652bd0c07cdc72cf425ae5bcdaeb9668443627f4891887557dea02f5
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [0.2.0] - 2018-08-18
|
10
|
+
### Changed
|
11
|
+
- Change API of `create_enum` to take two arguments instead, the name and an array of values
|
12
|
+
|
9
13
|
## [0.1.1] - 2018-08-14
|
10
14
|
- Bump Ruby requirement to 2.2.2 (for Rails 5.2) until earlier framework versions are supported.
|
11
15
|
- `enum_types` are listed in alphabetical order
|
data/README.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
This gem is a small monkeypatch to ActiveRecord so that your `schema.rb` file can support PostgreSQL's native enumerated types.
|
4
4
|
|
5
|
+
This will allow you to use enum columns in your database without replacing `schema.rb` with `structure.sql`.
|
6
|
+
|
7
|
+
## Version support
|
8
|
+
|
9
|
+
Currently only Rails 5.2 is supported. Support for previous versions going back to 4.1 are planned.
|
10
|
+
|
5
11
|
## Installation
|
6
12
|
|
7
13
|
Add this line to your application's Gemfile:
|
@@ -20,7 +26,57 @@ Or install it yourself as:
|
|
20
26
|
|
21
27
|
## Usage
|
22
28
|
|
23
|
-
|
29
|
+
### Migrations
|
30
|
+
|
31
|
+
Defining a new ENUM
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class AddContactMethodType < ActiveRecord::Migration[5.2]
|
35
|
+
def up
|
36
|
+
create_enum "contact_method_type", %w[Email Phone]
|
37
|
+
end
|
38
|
+
|
39
|
+
def down
|
40
|
+
drop_enum "contact_method_type"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Adding a value to an existing ENUM
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class AddSMSToContactMethodType < ActiveRecord::Migration[5.2]
|
49
|
+
def up
|
50
|
+
add_enum_value "contact_method_type", "SMS", before: "Phone"
|
51
|
+
end
|
52
|
+
|
53
|
+
def down
|
54
|
+
raise ActiveRecord::IrreversibleMigration
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Helper Methods
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class ContactInfo < ActiveRecord::Base
|
63
|
+
include ActiveRecord::PGEnum::Helper
|
64
|
+
|
65
|
+
pg_enum contact_method: %w[Email SMS Phone]
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
`pg_enum` is a wrapper around the official `enum` method that converts array syntax into strings. The above example is equivalent to:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class ContactInfo < ActiveRecord::Base
|
73
|
+
include ActiveRecord::PGEnum::Helper
|
74
|
+
|
75
|
+
enum contact_method: { Email: "Email", SMS: "SMS", Phone: "Phone" }
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
There's no technical reason why you couldn't detect enum columns at startup time and automatically do this wireup, but I feel that the benefit of self-documenting outweighs the convenience.
|
24
80
|
|
25
81
|
## Development
|
26
82
|
|
@@ -14,7 +14,7 @@ module ActiveRecord
|
|
14
14
|
stream.puts " # These are custom enum types that must be created before they can be used in the schema definition"
|
15
15
|
|
16
16
|
enum_types.each do |name, definition|
|
17
|
-
stream.puts %Q{ create_enum "#{name}",
|
17
|
+
stream.puts %Q{ create_enum "#{name}", %w[#{definition.join(" ")}]"}
|
18
18
|
end
|
19
19
|
|
20
20
|
stream.puts
|
@@ -6,12 +6,12 @@ module ActiveRecord
|
|
6
6
|
# Example:
|
7
7
|
#
|
8
8
|
# create_enum("foo_type", "foo", "bar", "baz")
|
9
|
-
def create_enum(name,
|
10
|
-
execute "CREATE TYPE #{name} AS ENUM (#{values.map { |v| "'#{v}'" }.join(", ")})"
|
9
|
+
def create_enum(name, values)
|
10
|
+
execute "CREATE TYPE #{name} AS ENUM (#{Array(values).map { |v| "'#{v}'" }.join(", ")})"
|
11
11
|
end
|
12
12
|
|
13
13
|
# Drop an ENUM type from the database.
|
14
|
-
def drop_enum(name,
|
14
|
+
def drop_enum(name, values_for_revert = nil)
|
15
15
|
execute "DROP TYPE #{name}"
|
16
16
|
end
|
17
17
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-pg_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
|
- Adam Lassek
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|