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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86899f2fd76d82fd51e37a540b7548211d07d167
4
- data.tar.gz: 469fb6907906238115839004d2f5c667ddf127dc
3
+ metadata.gz: 8fe9547627600e36141697fe97434cb7c0f3c7cb
4
+ data.tar.gz: d88758d4d17ec31e2aa20e13b169981379fd5025
5
5
  SHA512:
6
- metadata.gz: cf1b2dfb1b84d758e4a54db7ead10e447e3443364c682e6d46e13fc311b6a96de0c120a1c394a835630aaed883dcd2226a42406cfeb54cbbd1329d3477d69c20
7
- data.tar.gz: 7572c4c9b29ecb5e86b5172927a9d616e433e869ec130b441e868b4b6409fba97147f3cdee913810d804774b0ed8d1a68b90d675672e5bd11f2b9571911abf86
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.banana.create
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
- 1 | grape | 2018-02-19 04:07:20.548005 | 2018-02-19 04:07:20.554754 | yellow
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
- =# \dT+ fruit_type;
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
- =# \dT+ color;
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)
@@ -1,3 +1,3 @@
1
1
  module PgActiveRecordEnum
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -75,5 +75,6 @@ end
75
75
 
76
76
  require 'active_support/all'
77
77
  require 'active_record'
78
+ require 'active_record/connection_adapters/postgresql_adapter'
78
79
  ActiveRecord::Base.send :extend, PgActiveRecordEnum
79
80
  PgActiveRecordEnum::Schema.include_migrations
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_activerecord_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - inpego