activerecord-pg_enum 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +8 -0
- data/README.md +15 -7
- data/gemfiles/5.0.gemfile.lock +1 -1
- data/gemfiles/5.1.gemfile.lock +2 -2
- data/gemfiles/5.2.gemfile.lock +2 -2
- data/gemfiles/edge.gemfile.lock +2 -2
- data/lib/active_record/pg_enum/5.0/prepare_column_options.rb +18 -0
- data/lib/active_record/pg_enum/{4.2 → 5.0}/schema_dumper.rb +5 -12
- data/lib/active_record/pg_enum/5.1/prepare_column_options.rb +1 -0
- data/lib/active_record/pg_enum/5.1/schema_dumper.rb +1 -0
- data/lib/active_record/pg_enum/5.2/prepare_column_options.rb +10 -0
- data/lib/active_record/pg_enum/5.2/schema_dumper.rb +10 -0
- data/lib/active_record/pg_enum/6.alpha/prepare_column_options.rb +1 -0
- data/lib/active_record/pg_enum/6.alpha/schema_dumper.rb +1 -0
- data/lib/active_record/pg_enum/command_recorder.rb +5 -0
- data/lib/active_record/pg_enum/helper.rb +23 -29
- data/lib/active_record/pg_enum/postgresql_adapter.rb +6 -0
- data/lib/active_record/pg_enum/schema_statements.rb +5 -0
- data/lib/active_record/pg_enum/table_definition.rb +21 -0
- data/lib/active_record/pg_enum/version.rb +1 -1
- data/lib/active_record/pg_enum.rb +30 -21
- metadata +12 -7
- data/lib/active_record/pg_enum/4.2/column_dumper.rb +0 -13
- data/lib/active_record/pg_enum/schema_dumper.rb +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fff1e0a83e5de4507a70ca673c8dbe261715810cda9936430a4c73673b74c623
|
4
|
+
data.tar.gz: 4a02ec6d42efb7fdec11f5cd5ceeee2200b47d6e544202d6eef6fa7947bef69d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61cdf54e236bb77fc2196779ff2c8300ef504a23a5d714a63ab9423d3d02bc3fef7f31c598dda827db950652f9eb680ce01e7c54ab0bb7b2d63b6fcd364a2844
|
7
|
+
data.tar.gz: f546fa5f64257640d9bc4889bea00fd23e52033928a7f7623e571be48f16ce8358904caffac975994404b73115bb0214e124e4f8f36be88b3beef55085d06caa
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [0.4.0] - 2019-06-19
|
10
|
+
### Added
|
11
|
+
- `enum` method on `TableDefinition`
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
- Refactored init hook to be much more flexible
|
15
|
+
- Removed `ActiveRecord::PGEnum::Helper` in favor of `ActiveRecord::PGEnum()` module builder
|
16
|
+
|
9
17
|
## [0.3.0] - 2019-03-03
|
10
18
|
- Support for 5.0 and 5.1
|
11
19
|
- Change travis config to test against oldest supported version of ruby
|
data/README.md
CHANGED
@@ -58,22 +58,30 @@ class AddSMSToContactMethodType < ActiveRecord::Migration[5.2]
|
|
58
58
|
end
|
59
59
|
```
|
60
60
|
|
61
|
-
|
61
|
+
Adding an enum column to a table
|
62
62
|
|
63
63
|
```ruby
|
64
|
-
class
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
class AddStatusToOrder < ActiveRecord::Migration[5.2]
|
65
|
+
def change
|
66
|
+
change_table :orders do |t|
|
67
|
+
t.enum :status, as: "status_type"
|
68
|
+
end
|
69
|
+
end
|
68
70
|
end
|
69
71
|
```
|
70
72
|
|
71
|
-
|
73
|
+
### Module Builder
|
72
74
|
|
73
75
|
```ruby
|
74
76
|
class ContactInfo < ActiveRecord::Base
|
75
|
-
include ActiveRecord::PGEnum
|
77
|
+
include ActiveRecord::PGEnum(contact_method: %w[Email SMS Phone])
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
The generated module calls the official `enum` method converting array syntax into strings. The above example is equivalent to:
|
76
82
|
|
83
|
+
```ruby
|
84
|
+
class ContactInfo < ActiveRecord::Base
|
77
85
|
enum contact_method: { Email: "Email", SMS: "SMS", Phone: "Phone" }
|
78
86
|
end
|
79
87
|
```
|
data/gemfiles/5.0.gemfile.lock
CHANGED
data/gemfiles/5.1.gemfile.lock
CHANGED
data/gemfiles/5.2.gemfile.lock
CHANGED
data/gemfiles/edge.gemfile.lock
CHANGED
@@ -66,7 +66,7 @@ GIT
|
|
66
66
|
PATH
|
67
67
|
remote: ..
|
68
68
|
specs:
|
69
|
-
activerecord-pg_enum (0.
|
69
|
+
activerecord-pg_enum (0.4.0)
|
70
70
|
activerecord (>= 5.0.0)
|
71
71
|
activesupport
|
72
72
|
pg
|
@@ -157,4 +157,4 @@ DEPENDENCIES
|
|
157
157
|
rspec (~> 3.0)
|
158
158
|
|
159
159
|
BUNDLED WITH
|
160
|
-
1.
|
160
|
+
1.17.3
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module PGEnum
|
3
|
+
def self.install_column_options
|
4
|
+
require "active_record/connection_adapters/postgresql/schema_dumper"
|
5
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper.prepend PrepareColumnOptions
|
6
|
+
end
|
7
|
+
|
8
|
+
module PrepareColumnOptions
|
9
|
+
def prepare_column_options(column)
|
10
|
+
super.tap do |spec|
|
11
|
+
if column.type == :enum
|
12
|
+
spec[:as] = column.sql_type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module PGEnum
|
3
|
+
def self.install_schema_dumper
|
4
|
+
require "active_record/schema_dumper"
|
5
|
+
ActiveRecord::SchemaDumper.prepend SchemaDumper
|
6
|
+
end
|
7
|
+
|
3
8
|
module SchemaDumper
|
4
9
|
private
|
5
10
|
|
@@ -19,18 +24,6 @@ module ActiveRecord
|
|
19
24
|
|
20
25
|
stream.puts
|
21
26
|
end
|
22
|
-
|
23
|
-
# Takes a column specification in Object form and serializes it into a String.
|
24
|
-
def format_colspec(colspec)
|
25
|
-
case colspec
|
26
|
-
when String
|
27
|
-
colspec
|
28
|
-
when Array
|
29
|
-
colspec.map { |value| format_colspec(value) }.select(&:present?).join(", ")
|
30
|
-
else
|
31
|
-
super
|
32
|
-
end
|
33
|
-
end
|
34
27
|
end
|
35
28
|
end
|
36
29
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "active_record/pg_enum/5.0/prepare_column_options"
|
@@ -0,0 +1 @@
|
|
1
|
+
require "active_record/pg_enum/5.0/schema_dumper"
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "active_record/pg_enum/5.0/prepare_column_options"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module PGEnum
|
5
|
+
def self.install_column_options
|
6
|
+
require "active_record/connection_adapters/postgresql/schema_dumper"
|
7
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend PrepareColumnOptions
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "active_record/pg_enum/5.0/schema_dumper"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module PGEnum
|
5
|
+
def self.install_schema_dumper
|
6
|
+
require "active_record/connection_adapters/postgresql/schema_dumper"
|
7
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend SchemaDumper
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "active_record/pg_enum/5.2/prepare_column_options"
|
@@ -0,0 +1 @@
|
|
1
|
+
require "active_record/pg_enum/5.2/schema_dumper"
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module PGEnum
|
3
|
+
def self.install_command_recorder
|
4
|
+
require "active_record/migration/command_recorder"
|
5
|
+
ActiveRecord::Migration::CommandRecorder.include CommandRecorder
|
6
|
+
end
|
7
|
+
|
3
8
|
# ActiveRecord::Migration::CommandRecorder is a class used by reversible migrations.
|
4
9
|
# It captures the forward migration commands and translates them into their inverse
|
5
10
|
# by way of some simple metaprogramming.
|
@@ -1,36 +1,30 @@
|
|
1
1
|
module ActiveRecord
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
# Declare an enum attribute where the values map to strings enforced by PostgreSQL's
|
3
|
+
# enumerated types.
|
4
|
+
#
|
5
|
+
# class Conversation < ActiveRecord::Base
|
6
|
+
# include ActiveRecord::PGEnum(status: %i[active archived])
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# This is merely a wrapper over traditional enum syntax so that you can define
|
10
|
+
# string-based enums with an array of values.
|
11
|
+
def self.PGEnum(**definitions)
|
12
|
+
values = definitions.values.map do |value|
|
13
|
+
if value.is_a? Array
|
14
|
+
keys = value.map(&:to_sym)
|
15
|
+
values = value.map(&:to_s)
|
7
16
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# include ActiveRecord::PGEnum::Helper
|
14
|
-
#
|
15
|
-
# pg_enum status: %i[active archived]
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# This is merely a wrapper over traditional enum syntax so that you can define
|
19
|
-
# string-based enums with an array of values.
|
20
|
-
def pg_enum(definitions)
|
21
|
-
values = definitions.values.map do |value|
|
22
|
-
if value.is_a? Array
|
23
|
-
keys = value.map(&:to_sym)
|
24
|
-
values = value.map(&:to_s)
|
17
|
+
Hash[keys.zip(values)]
|
18
|
+
else
|
19
|
+
value
|
20
|
+
end
|
21
|
+
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
value
|
29
|
-
end
|
30
|
-
end
|
23
|
+
Module.new do
|
24
|
+
define_singleton_method(:inspect) { %{ActiveRecord::PGEnum(#{definitions.keys.join(" ")})} }
|
31
25
|
|
32
|
-
|
33
|
-
|
26
|
+
define_singleton_method :included do |klass|
|
27
|
+
klass.enum Hash[definitions.keys.zip(values)]
|
34
28
|
end
|
35
29
|
end
|
36
30
|
end
|
@@ -1,5 +1,11 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module PGEnum
|
3
|
+
def self.install_postgresql_adapter
|
4
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
5
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include PostgreSQLAdapter
|
6
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES.merge!(enum: { name: "enum" })
|
7
|
+
end
|
8
|
+
|
3
9
|
module PostgreSQLAdapter
|
4
10
|
# Helper method used by the monkeypatch internals. Provides a hash of ENUM types as they exist currently.
|
5
11
|
#
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module PGEnum
|
3
|
+
def self.install_schema_statements
|
4
|
+
require "active_record/connection_adapters/postgresql/schema_statements"
|
5
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include SchemaStatements
|
6
|
+
end
|
7
|
+
|
3
8
|
module SchemaStatements
|
4
9
|
# Create a new ENUM type, with an arbitrary number of values.
|
5
10
|
#
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module PGEnum
|
3
|
+
def self.install_table_definition
|
4
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
5
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.include TableDefinition
|
6
|
+
end
|
7
|
+
|
8
|
+
module TableDefinition
|
9
|
+
# Create an enum column inside a TableDefinition
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# create_table :orders do |t|
|
14
|
+
# t.enum :status, as: "status_type"
|
15
|
+
# end
|
16
|
+
def enum(name, as:, **options)
|
17
|
+
column(name, as, options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,30 +1,39 @@
|
|
1
|
-
require "active_record"
|
2
|
-
require "active_record/connection_adapters/postgresql/schema_dumper"
|
3
|
-
require "active_record/connection_adapters/postgresql/schema_statements"
|
4
|
-
require "active_record/connection_adapters/postgresql_adapter"
|
5
1
|
require "active_support/lazy_load_hooks"
|
2
|
+
require "active_record"
|
6
3
|
|
7
4
|
ActiveSupport.on_load(:active_record) do
|
8
|
-
require "active_record/pg_enum/command_recorder"
|
9
|
-
require "active_record/pg_enum/postgresql_adapter"
|
10
|
-
require "active_record/pg_enum/schema_statements"
|
11
5
|
require "active_record/pg_enum/helper"
|
6
|
+
ActiveRecord::PGEnum.install Gem.loaded_specs["activerecord"].version
|
7
|
+
end
|
12
8
|
|
13
|
-
|
9
|
+
module ActiveRecord
|
10
|
+
module PGEnum
|
11
|
+
KNOWN_VERSIONS = %w[5.0 5.1 5.2 6.alpha].map { |v| Gem::Version.new(v) }
|
14
12
|
|
15
|
-
|
13
|
+
def self.install(version)
|
14
|
+
major_minor = version.canonical_segments[0..1].join(".")
|
15
|
+
major_minor = Gem::Version.new(major_minor)
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
17
|
+
# Don't immediately fail if we don't yet support the current version.
|
18
|
+
# There's at least a chance it could work.
|
19
|
+
if !KNOWN_VERSIONS.include?(major_minor) && major_minor > KNOWN_VERSIONS.last
|
20
|
+
major_minor = KNOWN_VERSIONS.last
|
21
|
+
warn "[PGEnum] Current ActiveRecord version unsupported! Falling back to: #{major_minor}"
|
22
|
+
end
|
23
|
+
|
24
|
+
require "active_record/pg_enum/#{major_minor}/prepare_column_options"
|
25
|
+
require "active_record/pg_enum/#{major_minor}/schema_dumper"
|
26
|
+
require "active_record/pg_enum/postgresql_adapter"
|
27
|
+
require "active_record/pg_enum/schema_statements"
|
28
|
+
require "active_record/pg_enum/command_recorder"
|
29
|
+
require "active_record/pg_enum/table_definition"
|
26
30
|
|
27
|
-
|
28
|
-
|
29
|
-
|
31
|
+
install_column_options
|
32
|
+
install_schema_dumper
|
33
|
+
install_postgresql_adapter
|
34
|
+
install_schema_statements
|
35
|
+
install_command_recorder
|
36
|
+
install_table_definition
|
37
|
+
end
|
38
|
+
end
|
30
39
|
end
|
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.4.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: 2019-
|
11
|
+
date: 2019-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
@@ -152,13 +152,19 @@ files:
|
|
152
152
|
- gemfiles/edge.gemfile
|
153
153
|
- gemfiles/edge.gemfile.lock
|
154
154
|
- lib/active_record/pg_enum.rb
|
155
|
-
- lib/active_record/pg_enum/
|
156
|
-
- lib/active_record/pg_enum/
|
155
|
+
- lib/active_record/pg_enum/5.0/prepare_column_options.rb
|
156
|
+
- lib/active_record/pg_enum/5.0/schema_dumper.rb
|
157
|
+
- lib/active_record/pg_enum/5.1/prepare_column_options.rb
|
158
|
+
- lib/active_record/pg_enum/5.1/schema_dumper.rb
|
159
|
+
- lib/active_record/pg_enum/5.2/prepare_column_options.rb
|
160
|
+
- lib/active_record/pg_enum/5.2/schema_dumper.rb
|
161
|
+
- lib/active_record/pg_enum/6.alpha/prepare_column_options.rb
|
162
|
+
- lib/active_record/pg_enum/6.alpha/schema_dumper.rb
|
157
163
|
- lib/active_record/pg_enum/command_recorder.rb
|
158
164
|
- lib/active_record/pg_enum/helper.rb
|
159
165
|
- lib/active_record/pg_enum/postgresql_adapter.rb
|
160
|
-
- lib/active_record/pg_enum/schema_dumper.rb
|
161
166
|
- lib/active_record/pg_enum/schema_statements.rb
|
167
|
+
- lib/active_record/pg_enum/table_definition.rb
|
162
168
|
- lib/active_record/pg_enum/version.rb
|
163
169
|
- lib/activerecord/pg_enum.rb
|
164
170
|
homepage: https://github.com/getflywheel/activerecord-pg_enum
|
@@ -180,8 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
186
|
- !ruby/object:Gem::Version
|
181
187
|
version: '0'
|
182
188
|
requirements: []
|
183
|
-
|
184
|
-
rubygems_version: 2.4.5
|
189
|
+
rubygems_version: 3.0.3
|
185
190
|
signing_key:
|
186
191
|
specification_version: 4
|
187
192
|
summary: Integrate PostgreSQL's enumerated types with the Rails enum feature
|
@@ -1,45 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module PGEnum
|
3
|
-
module SchemaDumper
|
4
|
-
private
|
5
|
-
|
6
|
-
def extensions(stream)
|
7
|
-
super
|
8
|
-
enums(stream)
|
9
|
-
end
|
10
|
-
|
11
|
-
def enums(stream)
|
12
|
-
return unless (enum_types = @connection.enum_types).any?
|
13
|
-
|
14
|
-
stream.puts " # These are custom enum types that must be created before they can be used in the schema definition"
|
15
|
-
|
16
|
-
enum_types.each do |name, definition|
|
17
|
-
stream.puts %Q{ create_enum "#{name}", %w[#{definition.join(" ")}]}
|
18
|
-
end
|
19
|
-
|
20
|
-
stream.puts
|
21
|
-
end
|
22
|
-
|
23
|
-
# Gathers the arguments needed for the column line inside the create_table block.
|
24
|
-
def column_spec(column)
|
25
|
-
if column.type == :enum
|
26
|
-
return ["column", [column.sql_type.inspect, prepare_column_options(column)]]
|
27
|
-
end
|
28
|
-
|
29
|
-
super
|
30
|
-
end
|
31
|
-
|
32
|
-
# Takes a column specification in Object form and serializes it into a String.
|
33
|
-
def format_colspec(colspec)
|
34
|
-
case colspec
|
35
|
-
when String
|
36
|
-
colspec
|
37
|
-
when Array
|
38
|
-
colspec.map { |value| format_colspec(value) }.select(&:present?).join(", ")
|
39
|
-
else
|
40
|
-
super
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|