pg_activerecord_enum 0.0.9 → 0.1.0
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 +32 -7
- data/lib/pg_activerecord_enum/schema.rb +19 -0
- data/lib/pg_activerecord_enum/version.rb +1 -1
- data/lib/pg_activerecord_enum.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fe9547627600e36141697fe97434cb7c0f3c7cb
|
4
|
+
data.tar.gz: d88758d4d17ec31e2aa20e13b169981379fd5025
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f77ab292a4137eb0f3f74e685f40266fa3ca048c16bdfdab8cdc10e157ae8ce0ec4ace9596b688296f273928ca346a50ed4d01aa8df4e881c8a0d30ff276137
|
7
|
+
data.tar.gz: 41d06432b1edcb027536bef19bf93525ffa84608395b332f60382c221aafa01138714b607b15c149ba35487ddacf37a72bd0762f34ff4bf5f7389f372e237987
|
data/README.md
CHANGED
@@ -36,7 +36,7 @@ Migration:
|
|
36
36
|
class CreateFruits < ActiveRecord::Migration[5.1]
|
37
37
|
def change
|
38
38
|
create_table :fruits do |t|
|
39
|
-
t.enum :fruit_type, values: %i[banana orange grape], allow_blank: true
|
39
|
+
t.enum :fruit_type, values: %i[banana orange grape], allow_blank: true, null: false, default: 'banana', index: true
|
40
40
|
t.timestamps
|
41
41
|
end
|
42
42
|
end
|
@@ -53,7 +53,7 @@ class CreateFruits < ActiveRecord::Migration[5.1]
|
|
53
53
|
end
|
54
54
|
```
|
55
55
|
|
56
|
-
**allow_blank** adds empty value to enum.
|
56
|
+
**allow_blank** adds empty value to enum, other options are default options for column.
|
57
57
|
|
58
58
|
**Note:** migration rollback will remove enums only if other tables do not depend on them.
|
59
59
|
|
@@ -68,8 +68,8 @@ end
|
|
68
68
|
|
69
69
|
```ruby
|
70
70
|
Fruit.banana.count # 0
|
71
|
-
banana = Fruit.
|
72
|
-
banana.banana? # true
|
71
|
+
banana = Fruit.create
|
72
|
+
banana.banana? # true because default
|
73
73
|
banana.orange! #
|
74
74
|
banana.orange? # true
|
75
75
|
Fruit.orange.count # 1
|
@@ -82,10 +82,23 @@ In PostgreSQL DB:
|
|
82
82
|
# select * from fruits;
|
83
83
|
id | fruit_type | created_at | updated_at | color
|
84
84
|
----+------------+----------------------------+----------------------------+--------
|
85
|
-
|
85
|
+
3 | orange | 2018-02-28 20:45:13.724395 | 2018-02-28 20:45:13.731656 | orange
|
86
86
|
(1 row)
|
87
87
|
|
88
|
-
|
88
|
+
# \d fruits
|
89
|
+
Table "public.fruits"
|
90
|
+
Column | Type | Modifiers
|
91
|
+
------------+-----------------------------+-----------------------------------------------------
|
92
|
+
id | bigint | not null default nextval('fruits_id_seq'::regclass)
|
93
|
+
fruit_type | fruit_type | not null default 'banana'::fruit_type
|
94
|
+
created_at | timestamp without time zone | not null
|
95
|
+
updated_at | timestamp without time zone | not null
|
96
|
+
color | color |
|
97
|
+
Indexes:
|
98
|
+
"fruits_pkey" PRIMARY KEY, btree (id)
|
99
|
+
"index_fruits_on_fruit_type" btree (fruit_type)
|
100
|
+
|
101
|
+
# \dT+ fruit_type;
|
89
102
|
List of data types
|
90
103
|
Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
|
91
104
|
--------+------------+---------------+------+----------+----------+-------------------+-------------
|
@@ -95,17 +108,29 @@ In PostgreSQL DB:
|
|
95
108
|
| | | | | | |
|
96
109
|
(1 row)
|
97
110
|
|
98
|
-
|
111
|
+
# \dT+ color;
|
99
112
|
List of data types
|
100
113
|
Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
|
101
114
|
--------+-------+---------------+------+----------+----------+-------------------+-------------
|
102
115
|
public | color | color | 4 | red +| postgres | |
|
103
116
|
| | | | green +| | |
|
104
117
|
| | | | yellow +| | |
|
118
|
+
| | | | orange +| | |
|
105
119
|
| | | | | | |
|
106
120
|
(1 row)
|
107
121
|
```
|
108
122
|
|
123
|
+
In `schema.rb`:
|
124
|
+
```ruby
|
125
|
+
create_table "fruits", force: :cascade do |t|
|
126
|
+
t.enum "fruit_type", default: "banana", null: false, values: ["banana", "orange", "grape", ""]
|
127
|
+
t.datetime "created_at", null: false
|
128
|
+
t.datetime "updated_at", null: false
|
129
|
+
t.enum "color", values: ["red", "green", "yellow", "orange", ""]
|
130
|
+
t.index ["fruit_type"], name: "index_fruits_on_fruit_type"
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
109
134
|
## Development
|
110
135
|
|
111
136
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -4,6 +4,8 @@ module PgActiveRecordEnum
|
|
4
4
|
ActiveRecord::ConnectionAdapters::Table.send :include, TableDefinition
|
5
5
|
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TableDefinition
|
6
6
|
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements
|
7
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :prepend, AdapterEnumSupport
|
8
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :prepend, ColumnDumper
|
7
9
|
ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
|
8
10
|
end
|
9
11
|
|
@@ -53,6 +55,23 @@ module PgActiveRecordEnum
|
|
53
55
|
end
|
54
56
|
end
|
55
57
|
|
58
|
+
module AdapterEnumSupport
|
59
|
+
def native_database_types
|
60
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES.merge(enum: {name: :enum})
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
module ColumnDumper
|
65
|
+
def prepare_column_options(column)
|
66
|
+
spec = super
|
67
|
+
spec[:array] = "true" if column.array?
|
68
|
+
if column.sql_type_metadata.type == :enum
|
69
|
+
spec[:values] = PgActiveRecordEnum.values_for(column.name)
|
70
|
+
end
|
71
|
+
spec
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
56
75
|
module CommandRecorder
|
57
76
|
def add_enum(*args)
|
58
77
|
record(:add_enum, args)
|
data/lib/pg_activerecord_enum.rb
CHANGED