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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c54482529b0c9ca4c656af951d450a415701f1f5be9bb03bb1779f28b3e1a61d
4
- data.tar.gz: 79feea073eb9ac0669e5a7b1d28218ae6b800f59270ca023c38b0f7dc4705b60
3
+ metadata.gz: 3e86b5917258828aad1000b878f4cf5f263c2f00d4c9bada348159f75410d864
4
+ data.tar.gz: 5c97b8d8f4a81c72036ef4d4b627772965ac19cdbe0a4c4217aaf91f8abbaaa1
5
5
  SHA512:
6
- metadata.gz: b6909bccd2dff996432fa2117b07746f4c5cc12c6905f31b5b1eae16fbbc57bca9a8fde2843fe0de705cbd786e138f5db072131420eea90a0693da87b1dae024
7
- data.tar.gz: 7502f7eb5291001f8e51bc52184426002c0d075042380fa2e27d01316b32b64ee399a2c1a93cce40a8d0a9dbbe006e7de389a35ace07df7f311b824664b42134
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
- ActiveRecord::Migration::CommandRecorder.include AR::Enum::CommandRecorder
19
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include AR::Enum::Adapter
20
- ActiveRecord::SchemaDumper.prepend AR::Enum::SchemaDumper
21
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {name: "character varying"}
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 created on this database") if list.any?
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(",")
@@ -2,6 +2,6 @@
2
2
 
3
3
  module AR
4
4
  module Enum
5
- VERSION = "0.2.1"
5
+ VERSION = "0.2.2"
6
6
  end
7
7
  end
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.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira