method
_enum
v7.1.3.2 -
Show latest stable
-
0 notes -
Class: Enum
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
_enum(name, values, prefix: nil, suffix: nil, scopes: true, instance_methods: true, validate: false, **options)
private
Hide source
# File activerecord/lib/active_record/enum.rb, line 234 def _enum(name, values, prefix: nil, suffix: nil, scopes: true, instance_methods: true, validate: false, **options) assert_valid_enum_definition_values(values) # statuses = { } enum_values = ActiveSupport::HashWithIndifferentAccess.new name = name.to_s # def self.statuses() statuses end detect_enum_conflict!(name, name.pluralize, true) singleton_class.define_method(name.pluralize) { enum_values } defined_enums[name] = enum_values detect_enum_conflict!(name, name) detect_enum_conflict!(name, "#{name}=") attribute(name, **options) do |subtype| if subtype == ActiveModel::Type.default_value raise "Undeclared attribute type for enum '#{name}'. Enums must be" " backed by a database column or declared with an explicit type" " via `attribute`." end subtype = subtype.subtype if EnumType === subtype EnumType.new(name, enum_values, subtype, raise_on_invalid_values: !validate) end value_method_names = [] _enum_methods_module.module_eval do prefix = if prefix prefix == true ? "#{name}_" : "#{prefix}_" end suffix = if suffix suffix == true ? "_#{name}" : "_#{suffix}" end pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index pairs.each do |label, value| enum_values[label] = value label = label.to_s value_method_name = "#{prefix}#{label}#{suffix}" value_method_names << value_method_name define_enum_methods(name, value_method_name, value, scopes, instance_methods) method_friendly_label = label.gsub(/[\W&&[:ascii:]]+/, "_") value_method_alias = "#{prefix}#{method_friendly_label}#{suffix}" if value_method_alias != value_method_name && !value_method_names.include?(value_method_alias) value_method_names << value_method_alias define_enum_methods(name, value_method_alias, value, scopes, instance_methods) end end end detect_negative_enum_conditions!(value_method_names) if scopes if validate validate = {} unless Hash === validate validates_inclusion_of name, in: enum_values.keys, **validate end enum_values.freeze end