Created
February 28, 2023 09:39
-
-
Save CyJimmy264/6c008bab4dbe9af84253dc0822411038 to your computer and use it in GitHub Desktop.
ActiveRecord::Enum validation in Rails API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# ActiveRecord::Enum validation in Rails API | |
# https://medium.com/nerd-for-tech/using-activerecord-enum-in-rails-35edc2e9070f | |
module EnumValidatable | |
extend ActiveSupport::Concern | |
class_methods do | |
def validatable_enum(*enums_to_fix) | |
enums_to_fix.each do |element| | |
attribute(element) do |subtype| | |
subtype = subtype.subtype if subtype.is_a?(ActiveRecord::Enum::EnumType) | |
ValidatableEnumType.new(element, defined_enums.fetch(element.to_s), subtype) | |
end | |
end | |
end | |
end | |
class ValidatableEnumType < ActiveRecord::Enum::EnumType | |
# override assert_valid_value() to supress <ArgumentError> | |
# return a value and depend on our own validation | |
def assert_valid_value(value) | |
value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment