mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor enum
to extract the defining part for each definitions
This commit is contained in:
parent
da418dc250
commit
9c70d24e11
1 changed files with 19 additions and 20 deletions
|
@ -103,7 +103,6 @@ module ActiveRecord
|
|||
#
|
||||
# conversation.comments_inactive!
|
||||
# conversation.comments_active? # => false
|
||||
|
||||
module Enum
|
||||
def self.extended(base) # :nodoc:
|
||||
base.class_attribute(:defined_enums, instance_writer: false, default: {})
|
||||
|
@ -160,14 +159,20 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def enum(definitions)
|
||||
enum_prefix = definitions.delete(:_prefix)
|
||||
enum_suffix = definitions.delete(:_suffix)
|
||||
enum_scopes = definitions.delete(:_scopes)
|
||||
prefix = definitions.delete(:_prefix)
|
||||
suffix = definitions.delete(:_suffix)
|
||||
scopes = definitions.delete(:_scopes) != false
|
||||
|
||||
default = {}
|
||||
default[:default] = definitions.delete(:_default) if definitions.key?(:_default)
|
||||
|
||||
definitions.each do |name, values|
|
||||
_enum(name, values, prefix: prefix, suffix: suffix, scopes: scopes, **default)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def _enum(name, values, prefix: nil, suffix: nil, scopes: true, **options)
|
||||
assert_valid_enum_definition_values(values)
|
||||
# statuses = { }
|
||||
enum_values = ActiveSupport::HashWithIndifferentAccess.new
|
||||
|
@ -183,23 +188,19 @@ module ActiveRecord
|
|||
|
||||
attr = attribute_alias?(name) ? attribute_alias(name) : name
|
||||
|
||||
attribute(attr, **default) do |subtype|
|
||||
attribute(attr, **options) do |subtype|
|
||||
subtype = subtype.subtype if EnumType === subtype
|
||||
EnumType.new(attr, enum_values, subtype)
|
||||
end
|
||||
|
||||
value_method_names = []
|
||||
_enum_methods_module.module_eval do
|
||||
prefix = if enum_prefix == true
|
||||
"#{name}_"
|
||||
elsif enum_prefix
|
||||
"#{enum_prefix}_"
|
||||
prefix = if prefix
|
||||
prefix == true ? "#{name}_" : "#{prefix}_"
|
||||
end
|
||||
|
||||
suffix = if enum_suffix == true
|
||||
"_#{name}"
|
||||
elsif enum_suffix
|
||||
"_#{enum_suffix}"
|
||||
suffix = if suffix
|
||||
suffix == true ? "_#{name}" : "_#{suffix}"
|
||||
end
|
||||
|
||||
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
||||
|
@ -209,23 +210,21 @@ module ActiveRecord
|
|||
|
||||
value_method_name = "#{prefix}#{label}#{suffix}"
|
||||
value_method_names << value_method_name
|
||||
define_enum_methods(name, value_method_name, value, enum_scopes)
|
||||
define_enum_methods(name, value_method_name, value, scopes)
|
||||
|
||||
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, enum_scopes)
|
||||
define_enum_methods(name, value_method_alias, value, scopes)
|
||||
end
|
||||
end
|
||||
end
|
||||
detect_negative_enum_conditions!(value_method_names) if enum_scopes != false
|
||||
detect_negative_enum_conditions!(value_method_names) if scopes
|
||||
enum_values.freeze
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
class EnumMethods < Module # :nodoc:
|
||||
def initialize(klass)
|
||||
@klass = klass
|
||||
|
@ -234,7 +233,7 @@ module ActiveRecord
|
|||
private
|
||||
attr_reader :klass
|
||||
|
||||
def define_enum_methods(name, value_method_name, value, enum_scopes)
|
||||
def define_enum_methods(name, value_method_name, value, scopes)
|
||||
# def active?() status_for_database == 0 end
|
||||
klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
|
||||
define_method("#{value_method_name}?") { public_send(:"#{name}_for_database") == value }
|
||||
|
@ -245,7 +244,7 @@ module ActiveRecord
|
|||
|
||||
# scope :active, -> { where(status: 0) }
|
||||
# scope :not_active, -> { where.not(status: 0) }
|
||||
if enum_scopes != false
|
||||
if scopes
|
||||
klass.send(:detect_enum_conflict!, name, value_method_name, true)
|
||||
klass.scope value_method_name, -> { where(name => value) }
|
||||
|
||||
|
|
Loading…
Reference in a new issue