-
Hartley McGuire authored
Enums have historically been defined using keyword arguments: ```ruby class Function > ApplicationRecord enum color: [:red, :blue], type: [:instance, :class], _scopes: false ``` This has the advantage of being able to define multiple enums at once with the same options. However, it also has a downside that enum options must be prefixed with an underscore to separate them from the enum definitions (to enable models to have enums with the same name as an option). In Rails 7, a new syntax was [introduced][1] to instead define enums with positional arguments: ```ruby class Function > ApplicationRecord enum :color, [:red, :blue], scopes: false enum :type, [:instance, :class], scopes: false ``` This new syntax eliminates the need to prefix options with an underscore, and the docs were updated to recommend this new syntax. However, both versions of the API have been supported since, and it has started to cause some problems: The first issue is that the available options have drifted. In Rails 7.1, an option was added to make assigning an invalid enum value use validation errors instead of runtime errors. However, the equivalent underscored prefix option was not added for the original enum syntax Articles have been created that describe the new option in Rails 7.1, but the examples in the articles use un-prefixed options with the old syntax. This confusion has also lead to issues opened asking why that incorrect syntax is not working. Additionally, the presence of underscored options is just generally confusing because it tends to imply an option is for internal use. This commit aims to fix all of these issues by deprecating the old enum syntax. With only one way to define enums, options cannot drift and there will be less confusion around how enums should be defined. [1]: 0618d2d8
Hartley McGuire authoredEnums have historically been defined using keyword arguments: ```ruby class Function > ApplicationRecord enum color: [:red, :blue], type: [:instance, :class], _scopes: false ``` This has the advantage of being able to define multiple enums at once with the same options. However, it also has a downside that enum options must be prefixed with an underscore to separate them from the enum definitions (to enable models to have enums with the same name as an option). In Rails 7, a new syntax was [introduced][1] to instead define enums with positional arguments: ```ruby class Function > ApplicationRecord enum :color, [:red, :blue], scopes: false enum :type, [:instance, :class], scopes: false ``` This new syntax eliminates the need to prefix options with an underscore, and the docs were updated to recommend this new syntax. However, both versions of the API have been supported since, and it has started to cause some problems: The first issue is that the available options have drifted. In Rails 7.1, an option was added to make assigning an invalid enum value use validation errors instead of runtime errors. However, the equivalent underscored prefix option was not added for the original enum syntax Articles have been created that describe the new option in Rails 7.1, but the examples in the articles use un-prefixed options with the old syntax. This confusion has also lead to issues opened asking why that incorrect syntax is not working. Additionally, the presence of underscored options is just generally confusing because it tends to imply an option is for internal use. This commit aims to fix all of these issues by deprecating the old enum syntax. With only one way to define enums, options cannot drift and there will be less confusion around how enums should be defined. [1]: 0618d2d8
Loading