mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #28969 from kamipo/refactor_enum_to_use_value_instead_of_label
Refactor enum to use `value` instead of `label` in the scope
This commit is contained in:
commit
7eb66b1617
4 changed files with 16 additions and 14 deletions
|
@ -154,11 +154,12 @@ module ActiveRecord
|
|||
definitions.each do |name, values|
|
||||
# statuses = { }
|
||||
enum_values = ActiveSupport::HashWithIndifferentAccess.new
|
||||
name = name.to_sym
|
||||
name = name.to_s
|
||||
|
||||
# def self.statuses() statuses end
|
||||
detect_enum_conflict!(name, name.to_s.pluralize, true)
|
||||
klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values }
|
||||
detect_enum_conflict!(name, name.pluralize, true)
|
||||
singleton_class.send(:define_method, name.pluralize) { enum_values }
|
||||
defined_enums[name] = enum_values
|
||||
|
||||
detect_enum_conflict!(name, name)
|
||||
detect_enum_conflict!(name, "#{name}=")
|
||||
|
@ -170,7 +171,7 @@ module ActiveRecord
|
|||
|
||||
_enum_methods_module.module_eval do
|
||||
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
||||
pairs.each do |value, i|
|
||||
pairs.each do |label, value|
|
||||
if enum_prefix == true
|
||||
prefix = "#{name}_"
|
||||
elsif enum_prefix
|
||||
|
@ -182,23 +183,23 @@ module ActiveRecord
|
|||
suffix = "_#{enum_suffix}"
|
||||
end
|
||||
|
||||
value_method_name = "#{prefix}#{value}#{suffix}"
|
||||
enum_values[value] = i
|
||||
value_method_name = "#{prefix}#{label}#{suffix}"
|
||||
enum_values[label] = value
|
||||
label = label.to_s
|
||||
|
||||
# def active?() status == 0 end
|
||||
# def active?() status == "active" end
|
||||
klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
|
||||
define_method("#{value_method_name}?") { self[attr] == value.to_s }
|
||||
define_method("#{value_method_name}?") { self[attr] == label }
|
||||
|
||||
# def active!() update! status: :active end
|
||||
# def active!() update!(status: 0) end
|
||||
klass.send(:detect_enum_conflict!, name, "#{value_method_name}!")
|
||||
define_method("#{value_method_name}!") { update!(attr => value) }
|
||||
|
||||
# scope :active, -> { where status: 0 }
|
||||
# scope :active, -> { where(status: 0) }
|
||||
klass.send(:detect_enum_conflict!, name, value_method_name, true)
|
||||
klass.scope value_method_name, -> { where(attr => value) }
|
||||
end
|
||||
end
|
||||
defined_enums[name.to_s] = enum_values
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ class EnumTest < ActiveRecord::TestCase
|
|||
fixtures :books, :authors
|
||||
|
||||
setup do
|
||||
@author = authors(:david)
|
||||
@book = books(:awdr)
|
||||
end
|
||||
|
||||
|
@ -39,6 +38,8 @@ class EnumTest < ActiveRecord::TestCase
|
|||
assert_equal @book, Book.author_visibility_visible.first
|
||||
assert_equal @book, Book.illustrator_visibility_visible.first
|
||||
assert_equal @book, Book.medium_to_read.first
|
||||
assert_equal books(:ddd), Book.forgotten.first
|
||||
assert_equal books(:rfr), authors(:david).unpublished_books.first
|
||||
end
|
||||
|
||||
test "find via where with values" do
|
||||
|
@ -57,7 +58,6 @@ class EnumTest < ActiveRecord::TestCase
|
|||
assert_not_equal @book, Book.where(status: :written).first
|
||||
assert_equal @book, Book.where(status: [:published]).first
|
||||
assert_not_equal @book, Book.where(status: [:written]).first
|
||||
assert_not @author.unpublished_books.include?(@book)
|
||||
assert_not_equal @book, Book.where.not(status: :published).first
|
||||
assert_equal @book, Book.where.not(status: :written).first
|
||||
end
|
||||
|
|
1
activerecord/test/fixtures/books.yml
vendored
1
activerecord/test/fixtures/books.yml
vendored
|
@ -25,6 +25,7 @@ ddd:
|
|||
name: "Domain-Driven Design"
|
||||
format: "hardcover"
|
||||
status: 2
|
||||
read_status: "forgotten"
|
||||
|
||||
tlg:
|
||||
author_id: 1
|
||||
|
|
|
@ -8,7 +8,7 @@ class Book < ActiveRecord::Base
|
|||
has_many :subscribers, through: :subscriptions
|
||||
|
||||
enum status: [:proposed, :written, :published]
|
||||
enum read_status: { unread: 0, reading: 2, read: 3 }
|
||||
enum read_status: { unread: 0, reading: 2, read: 3, forgotten: nil }
|
||||
enum nullable_status: [:single, :married]
|
||||
enum language: [:english, :spanish, :french], _prefix: :in
|
||||
enum author_visibility: [:visible, :invisible], _prefix: true
|
||||
|
|
Loading…
Reference in a new issue