Last active
April 23, 2021 22:05
-
-
Save kerolloz/097c3d8ea6ee5e65c414f78b2a422284 to your computer and use it in GitHub Desktop.
ActiveRecord::Enum article on Medium
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
def assert_valid_value(value) | |
unless value.blank? || mapping.has_key?(value) || mapping.has_value?(value) | |
raise ArgumentError, "'#{value}' is not a valid #{name}" | |
end | |
end | |
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
class BlogPost < ApplicationRecord | |
enum state: %i[draft published] # [:draft, :published] | |
validates :state, inclusion: { in: states.keys } | |
end | |
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
class BlogPost < ApplicationRecord | |
include ValidatableEnum | |
enum state: %i[draft published] | |
validates :state, inclusion: { in: states.keys, | |
message: "state must be one of #{states.keys}" } | |
validatable_enum :state | |
end | |
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
class BlogPost < ApplicationRecord | |
include ValidatableEnum | |
enum state: %i[draft published] | |
validates :state, inclusion: { in: states.keys } | |
validatable_enum :state | |
end | |
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
module ValidatableEnum | |
extend ActiveSupport::Concern | |
class_methods do | |
def validatable_enum(*enums_to_fix) | |
enums_to_fix.each do |element| | |
decorate_attribute_type(element) do |subtype| | |
ValidatableEnumType.new(element, defined_enums.fetch(element.to_s), subtype) | |
end | |
end | |
end | |
end | |
end | |
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
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment