ar-enum 0.2.1 → 0.2.2
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 +4 -4
- data/README.md +12 -0
- data/lib/ar/enum.rb +49 -4
- data/lib/ar/enum/schema_dumper.rb +1 -1
- data/lib/ar/enum/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e86b5917258828aad1000b878f4cf5f263c2f00d4c9bada348159f75410d864
|
4
|
+
data.tar.gz: 5c97b8d8f4a81c72036ef4d4b627772965ac19cdbe0a4c4217aaf91f8abbaaa1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 584a2323fad2de86e7e30979a037a7ae935f3d12d4590b6484ffc5733b266d458962a513b265af863dcb0f152815169687dd85bfce79df5c7fe7524c234c9658
|
7
|
+
data.tar.gz: 6f713d4790f5f802775062448626e94e61698d4ba98d735ea3cd929a6202acb750f9210bced6a73b371c960225e15fc8072d95123e2bfafc04b9336b442486ca
|
data/README.md
CHANGED
@@ -40,6 +40,18 @@ create_table :articles do |t|
|
|
40
40
|
end
|
41
41
|
```
|
42
42
|
|
43
|
+
You can even use the type shortcut if you want; i.e. you can use `t.article_status` instead of `t.column`.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# The type is created independently from the table.
|
47
|
+
create_enum :article_status, %w[draft published]
|
48
|
+
|
49
|
+
create_table :articles do |t|
|
50
|
+
# Use the type `article_status` when defining your column.
|
51
|
+
t.article_status :status, null: false, default: "draft"
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
43
55
|
### Dropping enums
|
44
56
|
|
45
57
|
To remove the enum, use `drop_enum`.
|
data/lib/ar/enum.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require "active_support/all"
|
4
4
|
require "active_record"
|
5
5
|
require "active_record/connection_adapters/postgresql_adapter"
|
6
|
+
require "active_record/connection_adapters/postgresql/schema_statements"
|
7
|
+
require "active_record/connection_adapters/postgresql/schema_definitions"
|
6
8
|
require "active_record/schema_dumper"
|
7
9
|
require "active_record/migration/command_recorder"
|
8
10
|
require "ar/enum/version"
|
@@ -15,7 +17,50 @@ module AR
|
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
20
|
+
ActiveSupport.on_load(:active_record) do
|
21
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
22
|
+
|
23
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(
|
24
|
+
Module.new do
|
25
|
+
def create_table_definition(*args)
|
26
|
+
ActiveRecord::Base.connection.enum_types.each do |enum|
|
27
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[enum["name"].to_sym] = {name: enum["name"]}
|
28
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnMethods.class_eval do
|
29
|
+
define_method(enum["name"]) do |*names, **options|
|
30
|
+
names.each {|name| column(name, enum["name"].to_sym, options) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
super
|
36
|
+
end
|
37
|
+
end
|
38
|
+
)
|
39
|
+
|
40
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements.class_eval do
|
41
|
+
original_fetch_type_metadata = instance_method(:fetch_type_metadata)
|
42
|
+
|
43
|
+
define_method(:fetch_type_metadata) do |*args|
|
44
|
+
type_metadata = original_fetch_type_metadata.bind(self).call(*args)
|
45
|
+
|
46
|
+
if type_metadata.type == :enum
|
47
|
+
type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(
|
48
|
+
sql_type: type_metadata.sql_type,
|
49
|
+
type: type_metadata.sql_type.to_sym,
|
50
|
+
limit: type_metadata.limit,
|
51
|
+
precision: type_metadata.precision,
|
52
|
+
scale: type_metadata.scale
|
53
|
+
)
|
54
|
+
type_metadata = ActiveRecord::ConnectionAdapters::PostgreSQLTypeMetadata.new(type_metadata)
|
55
|
+
|
56
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[type_metadata.type] = {name: "character varying"}
|
57
|
+
end
|
58
|
+
|
59
|
+
type_metadata
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
ActiveRecord::Migration::CommandRecorder.include AR::Enum::CommandRecorder
|
64
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include AR::Enum::Adapter
|
65
|
+
ActiveRecord::SchemaDumper.prepend AR::Enum::SchemaDumper
|
66
|
+
end
|
@@ -11,7 +11,7 @@ module AR
|
|
11
11
|
def enum_types(stream)
|
12
12
|
list = @connection.enum_types.to_a
|
13
13
|
|
14
|
-
stream.puts(" # These are enum types
|
14
|
+
stream.puts(" # These are enum types available on this database") if list.any?
|
15
15
|
|
16
16
|
list.each do |row|
|
17
17
|
labels = row["labels"].split(",")
|
data/lib/ar/enum/version.rb
CHANGED