enum_errors_away 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3109aa3ed90342b1a9efd7021566b58b7d575f160886c5f05485a376e26a1f05
4
+ data.tar.gz: 1b47116a202a1adc29105447e351a05e165789cf64d29681ca77471acdd25e25
5
+ SHA512:
6
+ metadata.gz: 6a6329676b1ccda218d1cb2b119eaa8417d53e3017b9dc11575ac24c342894a5f339dbe2da479ba909639df9ed06d470ae7c85a524c030dcf7bdd6522b5f80e5
7
+ data.tar.gz: 30aec5078925bb1eff6fb06e85869aa68d9bd1a8904991067b4e08d8d6dc2350343bd7fc4a0c804e9cd21e01452728061af1566f6469cbfdfcb600bf821822d1
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2024-01-01
4
+
5
+ ### Added
6
+ - Initial release
7
+ - Automatic suppression of "Undeclared attribute type for enum" errors
8
+ - Rails railtie for seamless integration
9
+ - Configuration option to enable/disable the gem
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 enum_errors_away
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,261 @@
1
+ # EnumErrorsAway
2
+
3
+ [![CI](https://github.com/sampokuokkanen/enum_errors_away/actions/workflows/ci.yml/badge.svg)](https://github.com/sampokuokkanen/enum_errors_away/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/enum_errors_away.svg)](https://badge.fury.io/rb/enum_errors_away)
5
+
6
+ **Fix Rails 7.2+ enum migration failures** - EnumErrorsAway eliminates "Undeclared attribute type for enum" errors that break migrations when enum columns are added in separate migrations.
7
+
8
+ ## The Problem
9
+
10
+ Rails 7.2+ requires enum attributes to be declared with a type when they don't have a corresponding database column. This causes migration failures in a common scenario:
11
+
12
+ ```ruby
13
+ # Your model defines multiple enums
14
+ class Organization < ApplicationRecord
15
+ enum :status, { active: 0, inactive: 1 }
16
+ enum :billing_plan, { free: 0, paid: 1 }
17
+ end
18
+
19
+ # db/migrate/20240101_add_status_to_organizations.rb
20
+ add_column :organizations, :status, :integer
21
+
22
+ # db/migrate/20240201_add_billing_plan_to_organizations.rb
23
+ add_column :organizations, :billing_plan, :integer
24
+ ```
25
+
26
+ **When running migrations from scratch**, the first migration fails:
27
+
28
+ ```
29
+ ArgumentError: Undeclared attribute type for enum 'billing_plan' in Organization.
30
+ Enums must be backed by a database column or declared with an explicit type via `attribute`.
31
+ ```
32
+
33
+ This happens because:
34
+ 1. The first migration runs and loads the Organization model
35
+ 2. The model defines both `status` AND `billing_plan` enums
36
+ 3. But the `billing_plan` column doesn't exist yet (it's added in the second migration)
37
+ 4. Rails 7.2+ throws an error
38
+
39
+ This is especially problematic when:
40
+ - **Running migrations from scratch** (new development environments, CI/CD, test databases)
41
+ - **Adding new enums over time** in separate migrations
42
+ - **Migrating legacy applications** to Rails 7.2+
43
+ - **Working with large teams** where different developers add enums in different migrations
44
+
45
+ ## The Solution
46
+
47
+ EnumErrorsAway automatically handles this for you. Just add the gem and your enums work again - no code changes required!
48
+
49
+ The gem:
50
+ - ✅ **Automatically declares missing enum attributes** as integers
51
+ - ✅ **Preserves legitimate enum errors** (method collisions, invalid values, etc.)
52
+ - ✅ **Zero configuration required** - works out of the box
53
+ - ✅ **Type-safe** - includes RBS type definitions for Steep/TypeProf
54
+ - ✅ **Fully tested** - comprehensive test suite
55
+ - ✅ **Rails 7.2+ compatible**
56
+
57
+ ## Installation
58
+
59
+ Add this line to your application's Gemfile:
60
+
61
+ ```ruby
62
+ gem 'enum_errors_away'
63
+ ```
64
+
65
+ And then execute:
66
+
67
+ ```bash
68
+ $ bundle install
69
+ ```
70
+
71
+ Or install it yourself as:
72
+
73
+ ```bash
74
+ $ gem install enum_errors_away
75
+ ```
76
+
77
+ ## Usage
78
+
79
+ Once installed, the gem automatically works for all ActiveRecord models. No configuration needed!
80
+
81
+ ### Before (Rails 7.2+ without this gem):
82
+ ```ruby
83
+ class Organization < ApplicationRecord
84
+ enum :status, { active: 0, inactive: 1 } # Added in first migration
85
+ enum :billing_plan, { free: 0, paid: 1 } # Added in second migration
86
+ end
87
+
88
+ # Running the first migration fails because billing_plan column doesn't exist yet!
89
+ # You'd need to manually add: attribute :billing_plan, :integer
90
+ ```
91
+
92
+ ### After (with this gem):
93
+ ```ruby
94
+ class Organization < ApplicationRecord
95
+ enum :status, { active: 0, inactive: 1 }
96
+ enum :billing_plan, { free: 0, paid: 1 }
97
+ end
98
+
99
+ # Migrations run successfully - attributes are automatically declared as integers
100
+ # No manual attribute declarations needed!
101
+ ```
102
+
103
+ ## Features
104
+
105
+ ### All Enum Syntaxes Supported
106
+
107
+ ```ruby
108
+ class User < ApplicationRecord
109
+ # Hash syntax
110
+ enum :status, { active: 0, inactive: 1 }
111
+
112
+ # Array syntax
113
+ enum :role, [:admin, :user, :guest]
114
+
115
+ # With options
116
+ enum :access_level, { basic: 0, premium: 1 }, prefix: true
117
+
118
+ # With Rails 7.2+ options
119
+ enum :visibility, { public: 0, private: 1 }, scopes: false
120
+ end
121
+ ```
122
+
123
+ ### Legitimate Errors Still Raised
124
+
125
+ The gem **only suppresses** the "Undeclared attribute type" error. Other enum errors are preserved:
126
+
127
+ ```ruby
128
+ class Organization < ApplicationRecord
129
+ enum :status, { available: 0, unavailable: 1 }
130
+
131
+ # This will correctly raise an error about method collision
132
+ enum :availability, { available: 0, unavailable: 1 }
133
+ # ArgumentError: already defined by another enum
134
+ end
135
+ ```
136
+
137
+ ### Type Safety with RBS
138
+
139
+ The gem includes RBS type definitions for Steep and TypeProf:
140
+
141
+ ```ruby
142
+ # Your types are automatically available
143
+ module EnumErrorsAway
144
+ def self.enabled?: () -> bool
145
+ def self.configure: () { (singleton(EnumErrorsAway)) -> void } -> void
146
+ end
147
+ ```
148
+
149
+ ## Configuration
150
+
151
+ ### Disabling the Gem
152
+
153
+ You can disable the gem globally or conditionally:
154
+
155
+ ```ruby
156
+ # config/initializers/enum_errors_away.rb
157
+ EnumErrorsAway.configure do |config|
158
+ config.enabled = false
159
+ end
160
+
161
+ # Or conditionally
162
+ EnumErrorsAway.enabled = Rails.env.production?
163
+ ```
164
+
165
+ ### Checking Status
166
+
167
+ ```ruby
168
+ EnumErrorsAway.enabled? # => true
169
+ ```
170
+
171
+ ## Real-World Example
172
+
173
+ Here's the typical scenario this gem solves:
174
+
175
+ ```ruby
176
+ # app/models/organization.rb
177
+ class Organization < ApplicationRecord
178
+ enum :status, { active: 0, inactive: 1 } # Added May 2024
179
+ enum :billing_plan, { free: 0, paid: 1 } # Added June 2024
180
+ enum :notification_preference, { email: 0, sms: 1, both: 2 } # Added July 2024
181
+ end
182
+
183
+ # db/migrate/20240501_add_status_to_organizations.rb
184
+ class AddStatusToOrganizations < ActiveRecord::Migration[7.2]
185
+ def change
186
+ add_column :organizations, :status, :integer, default: 0
187
+ end
188
+ end
189
+
190
+ # db/migrate/20240601_add_billing_plan_to_organizations.rb
191
+ class AddBillingPlanToOrganizations < ActiveRecord::Migration[7.2]
192
+ def change
193
+ add_column :organizations, :billing_plan, :integer, default: 0
194
+ end
195
+ end
196
+
197
+ # db/migrate/20240701_add_notification_preference_to_organizations.rb
198
+ class AddNotificationPreferenceToOrganizations < ActiveRecord::Migration[7.2]
199
+ def change
200
+ add_column :organizations, :notification_preference, :integer, default: 0
201
+ end
202
+ end
203
+ ```
204
+
205
+ **Without this gem**: When a new developer runs `rails db:migrate` from scratch, the first migration fails because the model references enums that don't have columns yet.
206
+
207
+ **With this gem**: All migrations run successfully. The gem automatically declares the missing enum attributes, and everything works seamlessly.
208
+
209
+ ## How It Works
210
+
211
+ 1. **Hooks into ActiveRecord** - Uses a Railtie to integrate with Rails initialization
212
+ 2. **Extends enum method** - Wraps the standard `enum` method via `ActiveSupport::Concern`
213
+ 3. **Pre-declares attributes** - Before defining an enum, checks if the attribute exists in the database; if not, declares it as `:integer`
214
+ 4. **Handles migration timing** - Gracefully handles database connection errors during migrations
215
+ 5. **Preserves error handling** - Only catches "Undeclared attribute type" errors; all other enum errors pass through
216
+ 6. **Zero runtime performance impact** - Only runs during class definition, not during model usage
217
+
218
+ ## Compatibility
219
+
220
+ - **Ruby**: 3.1+
221
+ - **Rails**: 7.2+
222
+
223
+ ## Development
224
+
225
+ ```bash
226
+ # Install dependencies
227
+ bundle install
228
+
229
+ # Run tests
230
+ bundle exec rake test
231
+
232
+ # Run type checks
233
+ bundle exec steep check
234
+ ```
235
+
236
+ ## Testing
237
+
238
+ The gem includes a comprehensive test suite covering:
239
+ - Enums without database columns
240
+ - Enums with database columns
241
+ - Multiple enum syntaxes (hash, array, with options)
242
+ - Error handling (ensures legitimate errors aren't suppressed)
243
+ - Configuration and enable/disable functionality
244
+ - Integration with real Rails models
245
+
246
+ Run tests with:
247
+ ```bash
248
+ bundle exec rake test
249
+ ```
250
+
251
+ ## Contributing
252
+
253
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sampokuokkanen/enum_errors_away.
254
+
255
+ ## License
256
+
257
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
258
+
259
+ ## Credits
260
+
261
+ Created to solve the Rails 7.2+ enum declaration requirement while maintaining backward compatibility with existing codebases.
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnumErrorsAway
4
+ module ActiveRecordExtension # rubocop:todo Style/Documentation
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do # rubocop:todo Metrics/BlockLength
8
+ # rubocop:todo Metrics/PerceivedComplexity
9
+ # rubocop:todo Metrics/MethodLength
10
+ # rubocop:todo Metrics/AbcSize
11
+ def enum(name, values = nil, **options) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
12
+ return super(name, values, **options) unless EnumErrorsAway.enabled?
13
+
14
+ # Extract enum definitions based on Rails 8 signature: enum(name, values = nil, **options)
15
+ definitions = if values.nil? && options.any?
16
+ # enum(:status, active: 0, archived: 1) syntax
17
+ { name => options.except(:prefix, :suffix, :scopes, :validate, :default, :_prefix, :_suffix,
18
+ :_scopes, :_validate, :_default) }
19
+ elsif values.is_a?(Hash)
20
+ # enum(:status, { active: 0, archived: 1 }) syntax
21
+ { name => values }
22
+ elsif values.is_a?(Array)
23
+ # enum(:status, [:active, :archived]) syntax
24
+ { name => values }
25
+ else
26
+ # Fallback - shouldn't happen but just in case
27
+ { name => values }
28
+ end
29
+
30
+ # Pre-declare attributes for enum names that don't exist as columns
31
+ definitions.each do |enum_name, enum_values|
32
+ next if enum_name.nil? || enum_name.to_s.empty?
33
+ next if enum_values.nil?
34
+
35
+ begin
36
+ enum_name_str = enum_name.to_s
37
+ # @type var enum_name: Symbol | String
38
+ attribute enum_name, :integer if !attribute_types.key?(enum_name_str) && !columns_hash.key?(enum_name_str)
39
+ rescue ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotEstablished
40
+ # Ignore database errors during schema introspection
41
+ end
42
+ end
43
+
44
+ # Call the original enum method
45
+ begin
46
+ super(name, values, **options)
47
+ rescue ArgumentError => e
48
+ raise e unless e.message.include?('Undeclared attribute type for enum')
49
+
50
+ # Fallback: declare missing attributes and retry
51
+ definitions.each do |enum_name, enum_values|
52
+ next if enum_name.nil? || enum_name.to_s.empty?
53
+ next if enum_values.nil?
54
+
55
+ enum_name_str = enum_name.to_s
56
+ # @type var enum_name: Symbol | String
57
+ attribute enum_name, :integer unless attribute_types.key?(enum_name_str)
58
+ end
59
+ super(name, values, **options)
60
+ end
61
+ end
62
+ # rubocop:enable Metrics/AbcSize
63
+ # rubocop:enable Metrics/MethodLength
64
+ # rubocop:enable Metrics/PerceivedComplexity
65
+ end
66
+ end
67
+ end
68
+
69
+ ActiveSupport.on_load(:active_record) do
70
+ ActiveRecord::Base.include(EnumErrorsAway::ActiveRecordExtension)
71
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module EnumErrorsAway
6
+ class Railtie < Rails::Railtie # rubocop:todo Style/Documentation
7
+ initializer 'enum_errors_away.suppress_enum_errors', before: 'active_record.set_configs' do
8
+ ActiveSupport.on_load(:active_record) do
9
+ require 'enum_errors_away/active_record_extension'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnumErrorsAway
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'enum_errors_away/version'
4
+ require 'enum_errors_away/railtie' if defined?(Rails)
5
+
6
+ module EnumErrorsAway # rubocop:todo Style/Documentation
7
+ class << self
8
+ attr_accessor :enabled
9
+
10
+ def enabled?
11
+ @enabled != false
12
+ end
13
+
14
+ def configure
15
+ yield self
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnumErrorsAway
4
+ VERSION: String
5
+
6
+ @enabled: bool
7
+
8
+ def self.enabled: () -> bool
9
+ def self.enabled?: () -> bool
10
+ def self.enabled=: (bool value) -> bool
11
+ def self.configure: () { (singleton(EnumErrorsAway)) -> void } -> void
12
+
13
+ module ActiveRecordExtension : ActiveSupport::Concern
14
+ def self.class_methods: () { () -> void } -> void
15
+
16
+ def enum: (Symbol | String name, ?Array[Symbol] | Hash[Symbol, Integer] | nil values, **untyped options) -> void
17
+ def attribute: (Symbol | String name, Symbol type, **untyped options) -> void
18
+ def attribute_types: () -> Hash[String, untyped]
19
+ def columns_hash: () -> Hash[String, untyped]
20
+ end
21
+
22
+ class Railtie < Rails::Railtie
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum_errors_away
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sampo Kuokkanen
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.2'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rails
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.2'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '7.2'
40
+ - !ruby/object:Gem::Dependency
41
+ name: bundler
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: minitest
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: minitest-reporters
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.5'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.5'
82
+ - !ruby/object:Gem::Dependency
83
+ name: rake
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '13.0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '13.0'
96
+ - !ruby/object:Gem::Dependency
97
+ name: sqlite3
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '2.1'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '2.1'
110
+ description: Eliminates "Undeclared attribute type for enum" errors that occur when
111
+ running migrations with multiple enum columns added in separate migrations. Automatically
112
+ declares missing enum attributes as integers, preventing migration failures.
113
+ email:
114
+ - sampo.kuokkanen@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - CHANGELOG.md
120
+ - LICENSE.txt
121
+ - README.md
122
+ - lib/enum_errors_away.rb
123
+ - lib/enum_errors_away/active_record_extension.rb
124
+ - lib/enum_errors_away/railtie.rb
125
+ - lib/enum_errors_away/version.rb
126
+ - sig/enum_errors_away.rbs
127
+ homepage: https://github.com/sampokuokkanen/enum_errors_away
128
+ licenses:
129
+ - MIT
130
+ metadata:
131
+ homepage_uri: https://github.com/sampokuokkanen/enum_errors_away
132
+ source_code_uri: https://github.com/sampokuokkanen/enum_errors_away
133
+ changelog_uri: https://github.com/sampokuokkanen/enum_errors_away/blob/main/CHANGELOG.md
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: 3.1.0
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubygems_version: 3.6.9
149
+ specification_version: 4
150
+ summary: Fix Rails 7.2+ enum migration failures
151
+ test_files: []