activerecord-pg_enum 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +13 -15
  3. data/Appraisals +16 -0
  4. data/CHANGELOG.md +7 -0
  5. data/LICENSE +1 -1
  6. data/README.md +44 -14
  7. data/activerecord-pg_enum.gemspec +9 -9
  8. data/bin/console +1 -6
  9. data/bin/setup +2 -1
  10. data/gemfiles/4.1.gemfile +9 -0
  11. data/gemfiles/4.1.gemfile.lock +77 -0
  12. data/gemfiles/4.2.gemfile +9 -0
  13. data/gemfiles/4.2.gemfile.lock +75 -0
  14. data/gemfiles/5.0.gemfile.lock +1 -1
  15. data/gemfiles/5.1.gemfile.lock +1 -1
  16. data/gemfiles/5.2.gemfile.lock +1 -1
  17. data/gemfiles/6.0.gemfile +7 -0
  18. data/gemfiles/6.0.gemfile.lock +71 -0
  19. data/gemfiles/edge.gemfile.lock +1 -1
  20. data/lib/active_record/pg_enum.rb +72 -23
  21. data/lib/active_record/pg_enum/4.1/initialize_type_map.rb +41 -0
  22. data/lib/active_record/pg_enum/4.1/migration_keys.rb +14 -0
  23. data/lib/active_record/pg_enum/4.1/prepare_column_options.rb +16 -0
  24. data/lib/active_record/pg_enum/4.1/schema_dumper.rb +30 -0
  25. data/lib/active_record/pg_enum/4.1/simplified_type.rb +21 -0
  26. data/lib/active_record/pg_enum/{table_definition.rb → 4.1/table_definition.rb} +2 -2
  27. data/lib/active_record/pg_enum/4.2/migration_keys.rb +1 -0
  28. data/lib/active_record/pg_enum/4.2/prepare_column_options.rb +1 -0
  29. data/lib/active_record/pg_enum/4.2/schema_dumper.rb +1 -0
  30. data/lib/active_record/pg_enum/4.2/table_definition.rb +10 -0
  31. data/lib/active_record/pg_enum/5.0/migration_keys.rb +2 -0
  32. data/lib/active_record/pg_enum/5.0/prepare_column_options.rb +5 -7
  33. data/lib/active_record/pg_enum/5.0/schema_dumper.rb +1 -29
  34. data/lib/active_record/pg_enum/5.0/table_definition.rb +1 -0
  35. data/lib/active_record/pg_enum/5.1/schema_dumper.rb +1 -1
  36. data/lib/active_record/pg_enum/5.1/table_definition.rb +2 -0
  37. data/lib/active_record/pg_enum/5.2/prepare_column_options.rb +1 -1
  38. data/lib/active_record/pg_enum/5.2/schema_dumper.rb +2 -2
  39. data/lib/active_record/pg_enum/5.2/table_definition.rb +2 -0
  40. data/lib/active_record/pg_enum/{6.alpha → 6.0}/prepare_column_options.rb +0 -0
  41. data/lib/active_record/pg_enum/{6.alpha → 6.0}/schema_dumper.rb +0 -0
  42. data/lib/active_record/pg_enum/6.0/table_definition.rb +2 -0
  43. data/lib/active_record/pg_enum/command_recorder.rb +1 -1
  44. data/lib/active_record/pg_enum/postgresql_adapter.rb +8 -2
  45. data/lib/active_record/pg_enum/schema_statements.rb +8 -4
  46. data/lib/active_record/pg_enum/version.rb +1 -1
  47. metadata +28 -10
  48. data/Gemfile +0 -6
  49. data/lib/active_record/pg_enum/helper.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fff1e0a83e5de4507a70ca673c8dbe261715810cda9936430a4c73673b74c623
4
- data.tar.gz: 4a02ec6d42efb7fdec11f5cd5ceeee2200b47d6e544202d6eef6fa7947bef69d
3
+ metadata.gz: 6cc3af8b6c62c5db56b315c318f77f6cabf8ca8b522e6548f20f0cc780a64aa4
4
+ data.tar.gz: 2bdc0adf214333b6777006dfeb025aca3de673adbb672d7581df53ea0bf3724b
5
5
  SHA512:
6
- metadata.gz: 61cdf54e236bb77fc2196779ff2c8300ef504a23a5d714a63ab9423d3d02bc3fef7f31c598dda827db950652f9eb680ce01e7c54ab0bb7b2d63b6fcd364a2844
7
- data.tar.gz: f546fa5f64257640d9bc4889bea00fd23e52033928a7f7623e571be48f16ce8358904caffac975994404b73115bb0214e124e4f8f36be88b3beef55085d06caa
6
+ metadata.gz: 7fbbbf6ddf2d7f1eca10bc5fbc7218ffedbb87fd9798e277a0f566b0c6e18b428ed7a96824fd1e5676442a74796c9d7a55977adf2f7c17da36bf8d08a6b8981f
7
+ data.tar.gz: e6a309965758d93b2c558e8029edcdb51bf4a4e2c5af5283d5fdf645e65729596dc02dfba497b22f3bfa35e913c3dc903c4ebe7ea8e2b8535da29bf88c7ff174
data/.travis.yml CHANGED
@@ -1,27 +1,25 @@
1
1
  sudo: false
2
2
  language: ruby
3
- rvm:
4
- - 2.2.2
5
- - 2.5.0
6
- gemfile:
7
- - gemfiles/5.0.gemfile
8
- - gemfiles/5.1.gemfile
9
- - gemfiles/5.2.gemfile
10
- - gemfiles/edge.gemfile
11
3
  matrix:
12
- exclude:
13
- - rvm: 2.5.0
4
+ include:
5
+ - rvm: 2.2.4
6
+ gemfile: gemfiles/4.1.gemfile
7
+ - rvm: 2.2.10
8
+ gemfile: gemfiles/4.2.gemfile
9
+ - rvm: 2.3.8
14
10
  gemfile: gemfiles/5.0.gemfile
15
- - rvm: 2.5.0
11
+ - rvm: 2.4.4
16
12
  gemfile: gemfiles/5.1.gemfile
17
- - rvm: 2.5.0
13
+ - rvm: 2.5.1
18
14
  gemfile: gemfiles/5.2.gemfile
19
- - rvm: 2.2.2
15
+ - rvm: 2.6.0
16
+ gemfile: gemfiles/6.0.gemfile
17
+ - rvm: ruby-head
20
18
  gemfile: gemfiles/edge.gemfile
21
19
  allow_failures:
22
20
  - gemfile: gemfiles/edge.gemfile
23
- services:
24
- - postgresql
21
+ addons:
22
+ postgresql: "9.3"
25
23
  before_install: gem install bundler -v 1.17.3
26
24
  script: "bundle exec rake spec"
27
25
  env:
data/Appraisals CHANGED
@@ -1,3 +1,7 @@
1
+ appraise "6.0" do
2
+ gem "activerecord", "6.0.0.rc1"
3
+ end
4
+
1
5
  appraise "5.2" do
2
6
  gem "activerecord", "~> 5.2"
3
7
  end
@@ -11,6 +15,18 @@ appraise "5.0" do
11
15
  gem "i18n", "1.5.1"
12
16
  end
13
17
 
18
+ appraise "4.2" do
19
+ gem "activerecord", ">= 4.2.0", "< 5.0.0"
20
+ gem "i18n", "~> 0.7"
21
+ gem "pg", "~> 0.15"
22
+ end
23
+
24
+ appraise "4.1" do
25
+ gem "activerecord", ">= 4.1.0", "< 4.2.0"
26
+ gem "i18n", "~> 0.7"
27
+ gem "pg", "~> 0.15"
28
+ end
29
+
14
30
  appraise "edge" do
15
31
  gem "rails", git: "https://github.com/rails/rails.git", branch: "master"
16
32
  end
data/CHANGELOG.md CHANGED
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.0.0] - 2019-06-23
10
+ ### Added
11
+ - Support for 4.1 and 4.2
12
+
13
+ ### Changed
14
+ - Moved module builder to top-level `PGEnum()` method
15
+
9
16
  ## [0.4.0] - 2019-06-19
10
17
  ### Added
11
18
  - `enum` method on `TableDefinition`
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018 Flywheel
3
+ Copyright (c) 2019 Adam Lassek
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,14 +1,46 @@
1
- # ActiveRecord::PGEnum [![Build Status](https://travis-ci.com/getflywheel/activerecord-pg_enum.svg?branch=master)](https://travis-ci.com/getflywheel/activerecord-pg_enum)
1
+ # ActiveRecord::PGEnum [![Build Status](https://travis-ci.com/alassek/activerecord-pg_enum.svg?branch=master)](https://travis-ci.com/alassek/activerecord-pg_enum)
2
2
 
3
- This gem is a small monkeypatch to ActiveRecord so that your `schema.rb` file can support PostgreSQL's native enumerated types.
3
+ The `enum` feature in Rails has bad developer ergonomics. It uses integer types at the DB layer, which means trying to understand SQL output is a pain.
4
4
 
5
- This will allow you to use enum columns in your database without replacing `schema.rb` with `structure.sql`.
5
+ Using the easy form of the helper syntax is a minor footgun:
6
+
7
+ ```ruby
8
+ enum status: %w[new active archived]
9
+ ```
10
+
11
+ It's not obvious that the above code is order-dependent, but if you decide to add a new enum anywhere but the end, you're in trouble.
12
+
13
+ If you choose the use `varchar` fields instead, now you have to write annoying check constraints and lose the efficient storage.
14
+
15
+ ```ruby
16
+ enum status: { new: "new", active: "active", archived: "archived" }
17
+ ```
18
+
19
+ Nobody has time to write that nonsense.
20
+
21
+ ## Enumerated Types: The Best of Both Worlds
22
+
23
+ Did you know you can define your own types in PostgreSQL? You can, and this type system also supports enumeration.
24
+
25
+ ```SQL
26
+ CREATE TYPE status_type AS ENUM ('new', 'active', 'archived');
27
+ ```
28
+
29
+ Not only does this give you full type safety at the DB layer, the implementation is highly efficient. An enum value only takes up [four bytes](https://www.postgresql.org/docs/11/datatype-enum.html).
30
+
31
+ The best part is that PostgreSQL supports inserting new values at any point of the list without having to migrate your data.
32
+
33
+ ```SQL
34
+ ALTER TYPE status_type ADD VALUE 'pending' BEFORE 'active';
35
+ ```
6
36
 
7
37
  ## Version support
8
38
 
9
- I intend to support every version of Rails that supports `enum`, which was introduced in 4.1.
39
+ Every version of Rails with an `enum` macro is supported. This means 4.1 through master. Yes, this was annoying and difficult.
40
+
41
+ The monkeypatches in this library are extremely narrow and contained; the dirty hacks I had to do to make 4.1 work, for instance, have no impact on 6.0.
10
42
 
11
- Currently Rails 5.0 through 6.0 are supported.
43
+ Monkeypatching Rails internals is **scary**. So this library has a comprehensive test suite that runs against every known minor version.
12
44
 
13
45
  ## Installation
14
46
 
@@ -22,10 +54,6 @@ And then execute:
22
54
 
23
55
  $ bundle
24
56
 
25
- Or install it yourself as:
26
-
27
- $ gem install activerecord-pg_enum
28
-
29
57
  ## Usage
30
58
 
31
59
  ### Migrations
@@ -64,7 +92,7 @@ Adding an enum column to a table
64
92
  class AddStatusToOrder < ActiveRecord::Migration[5.2]
65
93
  def change
66
94
  change_table :orders do |t|
67
- t.enum :status, as: "status_type"
95
+ t.enum :status, as: "status_type", default: "new"
68
96
  end
69
97
  end
70
98
  end
@@ -74,7 +102,7 @@ end
74
102
 
75
103
  ```ruby
76
104
  class ContactInfo < ActiveRecord::Base
77
- include ActiveRecord::PGEnum(contact_method: %w[Email SMS Phone])
105
+ include PGEnum(contact_method: %w[Email SMS Phone])
78
106
  end
79
107
  ```
80
108
 
@@ -90,13 +118,15 @@ There's no technical reason why you couldn't detect enum columns at startup time
90
118
 
91
119
  ## Development
92
120
 
93
- 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.
121
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `appraise rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
122
+
123
+ Test a specific version with `appraise 6.0 rake spec`. This is usually necessary because different versions have different Ruby version support.
94
124
 
95
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
125
+ To install this gem onto your local machine, run `bundle exec rake install`.
96
126
 
97
127
  ## Contributing
98
128
 
99
- Bug reports and pull requests are welcome on GitHub at https://github.com/getflywheel/activerecord-pg_enum.
129
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alassek/activerecord-pg_enum.
100
130
 
101
131
  ## License
102
132
 
@@ -5,16 +5,16 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require "active_record/pg_enum/version"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = "activerecord-pg_enum"
9
- spec.version = ActiveRecord::PGEnum::VERSION
10
- spec.authors = ["Adam Lassek"]
11
- spec.email = ["adam@doubleprime.net"]
8
+ spec.name = "activerecord-pg_enum"
9
+ spec.version = ActiveRecord::PGEnum::VERSION
10
+ spec.authors = ["Adam Lassek"]
11
+ spec.email = ["adam@doubleprime.net"]
12
12
 
13
- spec.summary = %q{Integrate PostgreSQL's enumerated types with the Rails enum feature}
14
- spec.homepage = "https://github.com/getflywheel/activerecord-pg_enum"
15
- spec.license = "MIT"
13
+ spec.summary = %q{Integrate PostgreSQL's enumerated types with the Rails enum feature}
14
+ spec.homepage = "https://github.com/alassek/activerecord-pg_enum"
15
+ spec.license = "MIT"
16
16
 
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
18
  f.match(%r{^(test|spec|features)/})
19
19
  end
20
20
 
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.require_paths = ["lib"]
26
26
 
27
27
  spec.add_dependency "pg"
28
- spec.add_dependency "activerecord", ">= 5.0.0"
28
+ spec.add_dependency "activerecord", ">= 4.1.0"
29
29
  spec.add_dependency "activesupport"
30
30
 
31
31
  spec.add_development_dependency "appraisal"
data/bin/console CHANGED
@@ -3,12 +3,7 @@
3
3
  require "bundler/setup"
4
4
  require "activerecord/pg_enum"
5
5
 
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
6
+ ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
12
7
 
13
8
  require "pry"
14
9
  Pry.start
data/bin/setup CHANGED
@@ -3,6 +3,7 @@ set -euo pipefail
3
3
  IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
- bundle install
6
+ gem install appraisal
7
+ appraise bundle install
7
8
 
8
9
  # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", ">= 4.1.0", "< 4.2.0"
6
+ gem "i18n", "~> 0.7"
7
+ gem "pg", "~> 0.15"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ activerecord-pg_enum (0.4.0)
5
+ activerecord (>= 4.1.0)
6
+ activesupport
7
+ pg
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (4.1.16)
13
+ activesupport (= 4.1.16)
14
+ builder (~> 3.1)
15
+ activerecord (4.1.16)
16
+ activemodel (= 4.1.16)
17
+ activesupport (= 4.1.16)
18
+ arel (~> 5.0.0)
19
+ activesupport (4.1.16)
20
+ i18n (~> 0.6, >= 0.6.9)
21
+ json (~> 1.7, >= 1.7.7)
22
+ minitest (~> 5.1)
23
+ thread_safe (~> 0.1)
24
+ tzinfo (~> 1.1)
25
+ appraisal (2.2.0)
26
+ bundler
27
+ rake
28
+ thor (>= 0.14.0)
29
+ arel (5.0.1.20140414130214)
30
+ builder (3.2.3)
31
+ coderay (1.1.2)
32
+ concurrent-ruby (1.1.5)
33
+ diff-lcs (1.3)
34
+ i18n (0.9.5)
35
+ concurrent-ruby (~> 1.0)
36
+ json (1.8.6)
37
+ method_source (0.9.2)
38
+ minitest (5.11.3)
39
+ pg (0.21.0)
40
+ pry (0.12.2)
41
+ coderay (~> 1.1.0)
42
+ method_source (~> 0.9.0)
43
+ rake (10.5.0)
44
+ rspec (3.8.0)
45
+ rspec-core (~> 3.8.0)
46
+ rspec-expectations (~> 3.8.0)
47
+ rspec-mocks (~> 3.8.0)
48
+ rspec-core (3.8.1)
49
+ rspec-support (~> 3.8.0)
50
+ rspec-expectations (3.8.4)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.8.0)
53
+ rspec-mocks (3.8.1)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.8.0)
56
+ rspec-support (3.8.2)
57
+ thor (0.20.3)
58
+ thread_safe (0.3.6)
59
+ tzinfo (1.2.5)
60
+ thread_safe (~> 0.1)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ activerecord (>= 4.1.0, < 4.2.0)
67
+ activerecord-pg_enum!
68
+ appraisal
69
+ bundler (~> 1.15)
70
+ i18n (~> 0.7)
71
+ pg (~> 0.15)
72
+ pry
73
+ rake (~> 10.0)
74
+ rspec (~> 3.0)
75
+
76
+ BUNDLED WITH
77
+ 1.16.3
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", ">= 4.2.0", "< 5.0.0"
6
+ gem "i18n", "~> 0.7"
7
+ gem "pg", "~> 0.15"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ activerecord-pg_enum (0.4.0)
5
+ activerecord (>= 4.1.0)
6
+ activesupport
7
+ pg
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (4.2.11.1)
13
+ activesupport (= 4.2.11.1)
14
+ builder (~> 3.1)
15
+ activerecord (4.2.11.1)
16
+ activemodel (= 4.2.11.1)
17
+ activesupport (= 4.2.11.1)
18
+ arel (~> 6.0)
19
+ activesupport (4.2.11.1)
20
+ i18n (~> 0.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ appraisal (2.2.0)
25
+ bundler
26
+ rake
27
+ thor (>= 0.14.0)
28
+ arel (6.0.4)
29
+ builder (3.2.3)
30
+ coderay (1.1.2)
31
+ concurrent-ruby (1.1.5)
32
+ diff-lcs (1.3)
33
+ i18n (0.9.5)
34
+ concurrent-ruby (~> 1.0)
35
+ method_source (0.9.2)
36
+ minitest (5.11.3)
37
+ pg (0.21.0)
38
+ pry (0.12.2)
39
+ coderay (~> 1.1.0)
40
+ method_source (~> 0.9.0)
41
+ rake (10.5.0)
42
+ rspec (3.8.0)
43
+ rspec-core (~> 3.8.0)
44
+ rspec-expectations (~> 3.8.0)
45
+ rspec-mocks (~> 3.8.0)
46
+ rspec-core (3.8.1)
47
+ rspec-support (~> 3.8.0)
48
+ rspec-expectations (3.8.4)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.8.0)
51
+ rspec-mocks (3.8.1)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.8.0)
54
+ rspec-support (3.8.2)
55
+ thor (0.20.3)
56
+ thread_safe (0.3.6)
57
+ tzinfo (1.2.5)
58
+ thread_safe (~> 0.1)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ activerecord (>= 4.2.0, < 5.0.0)
65
+ activerecord-pg_enum!
66
+ appraisal
67
+ bundler (~> 1.15)
68
+ i18n (~> 0.7)
69
+ pg (~> 0.15)
70
+ pry
71
+ rake (~> 10.0)
72
+ rspec (~> 3.0)
73
+
74
+ BUNDLED WITH
75
+ 1.17.3
@@ -2,7 +2,7 @@ PATH
2
2
  remote: ..
3
3
  specs:
4
4
  activerecord-pg_enum (0.4.0)
5
- activerecord (>= 5.0.0)
5
+ activerecord (>= 4.1.0)
6
6
  activesupport
7
7
  pg
8
8
 
@@ -2,7 +2,7 @@ PATH
2
2
  remote: ..
3
3
  specs:
4
4
  activerecord-pg_enum (0.4.0)
5
- activerecord (>= 5.0.0)
5
+ activerecord (>= 4.1.0)
6
6
  activesupport
7
7
  pg
8
8
 
@@ -2,7 +2,7 @@ PATH
2
2
  remote: ..
3
3
  specs:
4
4
  activerecord-pg_enum (0.4.0)
5
- activerecord (>= 5.0.0)
5
+ activerecord (>= 4.1.0)
6
6
  activesupport
7
7
  pg
8
8
 
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "6.0.0.rc1"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ activerecord-pg_enum (0.4.0)
5
+ activerecord (>= 4.1.0)
6
+ activesupport
7
+ pg
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (6.0.0.rc1)
13
+ activesupport (= 6.0.0.rc1)
14
+ activerecord (6.0.0.rc1)
15
+ activemodel (= 6.0.0.rc1)
16
+ activesupport (= 6.0.0.rc1)
17
+ activesupport (6.0.0.rc1)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 0.7, < 2)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ zeitwerk (~> 2.1, >= 2.1.4)
23
+ appraisal (2.2.0)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
27
+ coderay (1.1.2)
28
+ concurrent-ruby (1.1.5)
29
+ diff-lcs (1.3)
30
+ i18n (1.6.0)
31
+ concurrent-ruby (~> 1.0)
32
+ method_source (0.9.2)
33
+ minitest (5.11.3)
34
+ pg (1.1.4)
35
+ pry (0.12.2)
36
+ coderay (~> 1.1.0)
37
+ method_source (~> 0.9.0)
38
+ rake (10.5.0)
39
+ rspec (3.8.0)
40
+ rspec-core (~> 3.8.0)
41
+ rspec-expectations (~> 3.8.0)
42
+ rspec-mocks (~> 3.8.0)
43
+ rspec-core (3.8.1)
44
+ rspec-support (~> 3.8.0)
45
+ rspec-expectations (3.8.4)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.8.0)
48
+ rspec-mocks (3.8.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.8.0)
51
+ rspec-support (3.8.2)
52
+ thor (0.20.3)
53
+ thread_safe (0.3.6)
54
+ tzinfo (1.2.5)
55
+ thread_safe (~> 0.1)
56
+ zeitwerk (2.1.6)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ activerecord (= 6.0.0.rc1)
63
+ activerecord-pg_enum!
64
+ appraisal
65
+ bundler (~> 1.15)
66
+ pry
67
+ rake (~> 10.0)
68
+ rspec (~> 3.0)
69
+
70
+ BUNDLED WITH
71
+ 1.17.3
@@ -67,7 +67,7 @@ PATH
67
67
  remote: ..
68
68
  specs:
69
69
  activerecord-pg_enum (0.4.0)
70
- activerecord (>= 5.0.0)
70
+ activerecord (>= 4.1.0)
71
71
  activesupport
72
72
  pg
73
73
 
@@ -2,38 +2,87 @@ require "active_support/lazy_load_hooks"
2
2
  require "active_record"
3
3
 
4
4
  ActiveSupport.on_load(:active_record) do
5
- require "active_record/pg_enum/helper"
6
5
  ActiveRecord::PGEnum.install Gem.loaded_specs["activerecord"].version
7
6
  end
8
7
 
9
8
  module ActiveRecord
10
9
  module PGEnum
11
- KNOWN_VERSIONS = %w[5.0 5.1 5.2 6.alpha].map { |v| Gem::Version.new(v) }
10
+ KNOWN_VERSIONS = %w[4.1 4.2 5.0 5.1 5.2 6.0].map { |v| Gem::Version.new(v) }
12
11
 
13
- def self.install(version)
14
- major_minor = version.canonical_segments[0..1].join(".")
15
- major_minor = Gem::Version.new(major_minor)
12
+ class << self
13
+ attr_reader :enabled_version
16
14
 
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}"
15
+ def install(version)
16
+ @enabled_version = approximate_version(version)
17
+
18
+ # Don't immediately fail if we don't yet support the current version.
19
+ # There's at least a chance it could work.
20
+ if !KNOWN_VERSIONS.include?(enabled_version) && enabled_version > KNOWN_VERSIONS.last
21
+ @enabled_version = KNOWN_VERSIONS.last
22
+ warn "[PGEnum] Current ActiveRecord version unsupported! Falling back to: #{enabled_version}"
23
+ end
24
+
25
+ initialize!
26
+ end
27
+
28
+ def register(patch, &block)
29
+ monkeypatches[patch] = block
30
+ end
31
+
32
+ private
33
+
34
+ def monkeypatches
35
+ @patches ||= {}
36
+ end
37
+
38
+ def initialize!
39
+ require "active_record/pg_enum/command_recorder"
40
+ require "active_record/pg_enum/postgresql_adapter"
41
+ require "active_record/pg_enum/schema_statements"
42
+
43
+ Dir[File.join(__dir__, "pg_enum", enabled_version.to_s, "*.rb")].each { |file| require file }
44
+ monkeypatches.keys.each { |patch| monkeypatches.delete(patch).call }
45
+ end
46
+
47
+ def approximate_version(version)
48
+ segments = version.respond_to?(:canonical_segments) ? version.canonical_segments : version.segments
49
+
50
+ segments.pop while segments.any? { |s| String === s }
51
+ segments.pop while segments.size > 2
52
+ segments.push(0) while segments.size < 2
53
+
54
+ Gem::Version.new segments.join(".")
22
55
  end
56
+ end
57
+ end
58
+ end
59
+
60
+ # Declare an enum attribute where the values map to strings enforced by PostgreSQL's
61
+ # enumerated types.
62
+ #
63
+ # class Conversation < ActiveRecord::Base
64
+ # include PGEnum(status: %i[active archived])
65
+ # end
66
+ #
67
+ # This is merely a wrapper over traditional enum syntax so that you can define
68
+ # string-based enums with an array of values.
69
+ def PGEnum(**definitions)
70
+ values = definitions.values.map do |value|
71
+ if value.is_a? Array
72
+ keys = value.map(&:to_sym)
73
+ values = value.map(&:to_s)
74
+
75
+ Hash[keys.zip(values)]
76
+ else
77
+ value
78
+ end
79
+ end
80
+
81
+ Module.new do
82
+ define_singleton_method(:inspect) { %{ActiveRecord::PGEnum(#{definitions.keys.join(" ")})} }
23
83
 
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"
30
-
31
- install_column_options
32
- install_schema_dumper
33
- install_postgresql_adapter
34
- install_schema_statements
35
- install_command_recorder
36
- install_table_definition
84
+ define_singleton_method :included do |klass|
85
+ klass.enum Hash[definitions.keys.zip(values)]
37
86
  end
38
87
  end
39
88
  end
@@ -0,0 +1,41 @@
1
+ require "active_record/connection_adapters/postgresql_adapter"
2
+
3
+ module ActiveRecord
4
+ module PGEnum
5
+ register :type_map do
6
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend InitializeTypeMap
7
+ end
8
+
9
+ module InitializeTypeMap
10
+ private
11
+
12
+ def initialize_type_map(type_map)
13
+ super
14
+
15
+ adapter = ConnectionAdapters::PostgreSQLAdapter
16
+
17
+ adapter::OID.register_type "enum", adapter::OID::Enum.new
18
+
19
+ execute("SELECT t.oid, t.typname, t.typtype FROM pg_type as t WHERE t.typtype = 'e'", "SCHEMA").each do |row|
20
+ adapter::OID.alias_type row["typname"], "enum"
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ module ConnectionAdapters
27
+ class PostgreSQLAdapter
28
+ module OID
29
+ class Enum < Type
30
+ def type
31
+ :enum
32
+ end
33
+
34
+ def type_cast(value)
35
+ value
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveRecord
2
+ module PGEnum
3
+ register :migration_keys do
4
+ require "active_record/connection_adapters/postgresql_adapter"
5
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend MigrationKeys
6
+ end
7
+
8
+ module MigrationKeys
9
+ def migration_keys
10
+ super + [:as]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ module ActiveRecord
2
+ module PGEnum
3
+ register :column_options do
4
+ require "active_record/connection_adapters/postgresql_adapter"
5
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend PrepareColumnOptions
6
+ end
7
+
8
+ module PrepareColumnOptions
9
+ def prepare_column_options(column, types)
10
+ spec = super
11
+ spec[:as] = column.sql_type.inspect if column.type == :enum
12
+ spec
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module ActiveRecord
2
+ module PGEnum
3
+ register :schema_dumper do
4
+ require "active_record/schema_dumper"
5
+ ActiveRecord::SchemaDumper.prepend SchemaDumper
6
+ end
7
+
8
+ module SchemaDumper
9
+ private
10
+
11
+ def extensions(stream)
12
+ super
13
+ enums(stream)
14
+ end
15
+
16
+ def enums(stream)
17
+ return unless (enum_types = @connection.enum_types).any?
18
+
19
+ stream.puts " # These are custom enum types that must be created before they can be used in the schema definition"
20
+
21
+ enum_types.each do |name, definition|
22
+ stream.puts %Q{ create_enum "#{name}", %w[#{definition.join(" ")}]}
23
+ end
24
+
25
+ stream.puts
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,21 @@
1
+ require "active_record/connection_adapters/postgresql_adapter"
2
+
3
+ module ActiveRecord
4
+ module PGEnum
5
+ register :simplified_type do
6
+ ConnectionAdapters::PostgreSQLColumn.prepend SimplifiedType
7
+ end
8
+
9
+ module SimplifiedType
10
+ private
11
+
12
+ def simplified_type(sql_type)
13
+ if ConnectionAdapters::PostgreSQLAdapter::OID::NAMES[sql_type].type == :enum
14
+ :enum
15
+ else
16
+ super
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,8 +1,8 @@
1
1
  module ActiveRecord
2
2
  module PGEnum
3
- def self.install_table_definition
3
+ register :table_definition do
4
4
  require "active_record/connection_adapters/postgresql_adapter"
5
- ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.include TableDefinition
5
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition.include TableDefinition
6
6
  end
7
7
 
8
8
  module TableDefinition
@@ -0,0 +1 @@
1
+ require "active_record/pg_enum/4.1/migration_keys"
@@ -0,0 +1 @@
1
+ require "active_record/pg_enum/4.1/prepare_column_options"
@@ -0,0 +1 @@
1
+ require "active_record/pg_enum/4.1/schema_dumper"
@@ -0,0 +1,10 @@
1
+ require "active_record/pg_enum/4.1/table_definition"
2
+
3
+ module ActiveRecord
4
+ module PGEnum
5
+ register :table_definition do
6
+ require "active_record/connection_adapters/postgresql_adapter"
7
+ ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.include TableDefinition
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ require "active_record/pg_enum/4.1/migration_keys"
2
+
@@ -1,17 +1,15 @@
1
1
  module ActiveRecord
2
2
  module PGEnum
3
- def self.install_column_options
3
+ register :column_options do
4
4
  require "active_record/connection_adapters/postgresql/schema_dumper"
5
- ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper.prepend PrepareColumnOptions
5
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend PrepareColumnOptions
6
6
  end
7
7
 
8
8
  module PrepareColumnOptions
9
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
10
+ spec = super
11
+ spec[:as] = column.sql_type.inspect if column.type == :enum
12
+ spec
15
13
  end
16
14
  end
17
15
  end
@@ -1,29 +1 @@
1
- module ActiveRecord
2
- module PGEnum
3
- def self.install_schema_dumper
4
- require "active_record/schema_dumper"
5
- ActiveRecord::SchemaDumper.prepend SchemaDumper
6
- end
7
-
8
- module SchemaDumper
9
- private
10
-
11
- def extensions(stream)
12
- super
13
- enums(stream)
14
- end
15
-
16
- def enums(stream)
17
- return unless (enum_types = @connection.enum_types).any?
18
-
19
- stream.puts " # These are custom enum types that must be created before they can be used in the schema definition"
20
-
21
- enum_types.each do |name, definition|
22
- stream.puts %Q{ create_enum "#{name}", %w[#{definition.join(" ")}]}
23
- end
24
-
25
- stream.puts
26
- end
27
- end
28
- end
29
- end
1
+ require "active_record/pg_enum/4.2/schema_dumper"
@@ -0,0 +1 @@
1
+ require "active_record/pg_enum/4.2/table_definition"
@@ -1 +1 @@
1
- require "active_record/pg_enum/5.0/schema_dumper"
1
+ require "active_record/pg_enum/4.2/schema_dumper"
@@ -0,0 +1,2 @@
1
+ require "active_record/pg_enum/4.2/table_definition"
2
+
@@ -2,7 +2,7 @@ require "active_record/pg_enum/5.0/prepare_column_options"
2
2
 
3
3
  module ActiveRecord
4
4
  module PGEnum
5
- def self.install_column_options
5
+ register :column_options do
6
6
  require "active_record/connection_adapters/postgresql/schema_dumper"
7
7
  ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend PrepareColumnOptions
8
8
  end
@@ -1,8 +1,8 @@
1
- require "active_record/pg_enum/5.0/schema_dumper"
1
+ require "active_record/pg_enum/4.2/schema_dumper"
2
2
 
3
3
  module ActiveRecord
4
4
  module PGEnum
5
- def self.install_schema_dumper
5
+ register :schema_dumper do
6
6
  require "active_record/connection_adapters/postgresql/schema_dumper"
7
7
  ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend SchemaDumper
8
8
  end
@@ -0,0 +1,2 @@
1
+ require "active_record/pg_enum/4.2/table_definition"
2
+
@@ -0,0 +1,2 @@
1
+ require "active_record/pg_enum/4.2/table_definition"
2
+
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module PGEnum
3
- def self.install_command_recorder
3
+ register :command_recorder do
4
4
  require "active_record/migration/command_recorder"
5
5
  ActiveRecord::Migration::CommandRecorder.include CommandRecorder
6
6
  end
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module PGEnum
3
- def self.install_postgresql_adapter
3
+ register :postgresql_adapter do
4
4
  require "active_record/connection_adapters/postgresql_adapter"
5
5
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include PostgreSQLAdapter
6
6
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES.merge!(enum: { name: "enum" })
@@ -13,7 +13,7 @@ module ActiveRecord
13
13
  #
14
14
  # { "foo_type" => ["foo", "bar", "baz"] }
15
15
  def enum_types
16
- res = exec_query(<<-SQL.strip_heredoc, "SCHEMA").cast_values
16
+ res = exec_query(<<-SQL.strip_heredoc, "SCHEMA")
17
17
  SELECT t.typname AS enum_name, string_agg(e.enumlabel, ' ' ORDER BY e.enumsortorder) AS enum_value
18
18
  FROM pg_type t
19
19
  JOIN pg_enum e ON t.oid = e.enumtypid
@@ -22,6 +22,12 @@ module ActiveRecord
22
22
  GROUP BY enum_name
23
23
  SQL
24
24
 
25
+ if res.respond_to?(:cast_values)
26
+ res = res.cast_values
27
+ else
28
+ res = res.rows
29
+ end
30
+
25
31
  res.inject({}) do |memo, (name, values)|
26
32
  memo[name] = values.split(" ")
27
33
  memo
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module PGEnum
3
- def self.install_schema_statements
3
+ register :schema_statements do
4
4
  require "active_record/connection_adapters/postgresql/schema_statements"
5
5
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include SchemaStatements
6
6
  end
@@ -12,12 +12,16 @@ module ActiveRecord
12
12
  #
13
13
  # create_enum("foo_type", "foo", "bar", "baz")
14
14
  def create_enum(name, values)
15
- execute "CREATE TYPE #{name} AS ENUM (#{Array(values).map { |v| "'#{v}'" }.join(", ")})"
15
+ execute("CREATE TYPE #{name} AS ENUM (#{Array(values).map { |v| "'#{v}'" }.join(", ")})").tap {
16
+ reload_type_map
17
+ }
16
18
  end
17
19
 
18
20
  # Drop an ENUM type from the database.
19
21
  def drop_enum(name, values_for_revert = nil)
20
- execute "DROP TYPE #{name}"
22
+ execute("DROP TYPE #{name}").tap {
23
+ reload_type_map
24
+ }
21
25
  end
22
26
 
23
27
  # Add a new value to an existing ENUM type.
@@ -41,7 +45,7 @@ module ActiveRecord
41
45
  cmd << " AFTER '#{after}'"
42
46
  end
43
47
 
44
- execute cmd
48
+ execute(cmd).tap { reload_type_map }
45
49
  end
46
50
  end
47
51
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module PGEnum
3
- VERSION = "0.4.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  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.0
4
+ version: 1.0.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-06-20 00:00:00.000000000 Z
11
+ date: 2019-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 5.0.0
33
+ version: 4.1.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 5.0.0
40
+ version: 4.1.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activesupport
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -135,7 +135,6 @@ files:
135
135
  - ".travis.yml"
136
136
  - Appraisals
137
137
  - CHANGELOG.md
138
- - Gemfile
139
138
  - LICENSE
140
139
  - README.md
141
140
  - Rakefile
@@ -143,31 +142,50 @@ files:
143
142
  - bin/console
144
143
  - bin/setup
145
144
  - gemfiles/.bundle/config
145
+ - gemfiles/4.1.gemfile
146
+ - gemfiles/4.1.gemfile.lock
147
+ - gemfiles/4.2.gemfile
148
+ - gemfiles/4.2.gemfile.lock
146
149
  - gemfiles/5.0.gemfile
147
150
  - gemfiles/5.0.gemfile.lock
148
151
  - gemfiles/5.1.gemfile
149
152
  - gemfiles/5.1.gemfile.lock
150
153
  - gemfiles/5.2.gemfile
151
154
  - gemfiles/5.2.gemfile.lock
155
+ - gemfiles/6.0.gemfile
156
+ - gemfiles/6.0.gemfile.lock
152
157
  - gemfiles/edge.gemfile
153
158
  - gemfiles/edge.gemfile.lock
154
159
  - lib/active_record/pg_enum.rb
160
+ - lib/active_record/pg_enum/4.1/initialize_type_map.rb
161
+ - lib/active_record/pg_enum/4.1/migration_keys.rb
162
+ - lib/active_record/pg_enum/4.1/prepare_column_options.rb
163
+ - lib/active_record/pg_enum/4.1/schema_dumper.rb
164
+ - lib/active_record/pg_enum/4.1/simplified_type.rb
165
+ - lib/active_record/pg_enum/4.1/table_definition.rb
166
+ - lib/active_record/pg_enum/4.2/migration_keys.rb
167
+ - lib/active_record/pg_enum/4.2/prepare_column_options.rb
168
+ - lib/active_record/pg_enum/4.2/schema_dumper.rb
169
+ - lib/active_record/pg_enum/4.2/table_definition.rb
170
+ - lib/active_record/pg_enum/5.0/migration_keys.rb
155
171
  - lib/active_record/pg_enum/5.0/prepare_column_options.rb
156
172
  - lib/active_record/pg_enum/5.0/schema_dumper.rb
173
+ - lib/active_record/pg_enum/5.0/table_definition.rb
157
174
  - lib/active_record/pg_enum/5.1/prepare_column_options.rb
158
175
  - lib/active_record/pg_enum/5.1/schema_dumper.rb
176
+ - lib/active_record/pg_enum/5.1/table_definition.rb
159
177
  - lib/active_record/pg_enum/5.2/prepare_column_options.rb
160
178
  - 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
179
+ - lib/active_record/pg_enum/5.2/table_definition.rb
180
+ - lib/active_record/pg_enum/6.0/prepare_column_options.rb
181
+ - lib/active_record/pg_enum/6.0/schema_dumper.rb
182
+ - lib/active_record/pg_enum/6.0/table_definition.rb
163
183
  - lib/active_record/pg_enum/command_recorder.rb
164
- - lib/active_record/pg_enum/helper.rb
165
184
  - lib/active_record/pg_enum/postgresql_adapter.rb
166
185
  - lib/active_record/pg_enum/schema_statements.rb
167
- - lib/active_record/pg_enum/table_definition.rb
168
186
  - lib/active_record/pg_enum/version.rb
169
187
  - lib/activerecord/pg_enum.rb
170
- homepage: https://github.com/getflywheel/activerecord-pg_enum
188
+ homepage: https://github.com/alassek/activerecord-pg_enum
171
189
  licenses:
172
190
  - MIT
173
191
  metadata: {}
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in activerecord-pg_enum.gemspec
6
- gemspec
@@ -1,31 +0,0 @@
1
- module ActiveRecord
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)
16
-
17
- Hash[keys.zip(values)]
18
- else
19
- value
20
- end
21
- end
22
-
23
- Module.new do
24
- define_singleton_method(:inspect) { %{ActiveRecord::PGEnum(#{definitions.keys.join(" ")})} }
25
-
26
- define_singleton_method :included do |klass|
27
- klass.enum Hash[definitions.keys.zip(values)]
28
- end
29
- end
30
- end
31
- end