mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
f4fbdb1b4e
If `AR::Enum` is used for boolean field, it would be not expected behavior for us. fixes #38075 Problem: In case of using boolean for enum, we can set with string (hash key) to instance, but we cannot set with actual value (hash value). ```ruby class Post < ActiveRecord::Base enum status: { enabled: true, disabled: false } end post.status = 'enabled' post.status # 'enabled' post.status = true post.status # 'enabled' post.status = 'disabled' post.status # 'disabled' post.status = false post.status # nil (This is not expected behavior) ``` After looking into `AR::Enum::EnumType#cast`, I found that `blank?` method converts from false value to nil (it seems it may not intentional behavior). In this patch, I improved that if it defines enum with boolean, it returns reasonable behavior.
33 lines
1 KiB
Ruby
33 lines
1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Book < ActiveRecord::Base
|
|
belongs_to :author
|
|
|
|
has_many :citations, foreign_key: "book1_id"
|
|
has_many :references, -> { distinct }, through: :citations, source: :reference_of
|
|
|
|
has_many :subscriptions
|
|
has_many :subscribers, through: :subscriptions
|
|
|
|
enum status: [:proposed, :written, :published]
|
|
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
|
|
enum illustrator_visibility: [:visible, :invisible], _prefix: true
|
|
enum font_size: [:small, :medium, :large], _prefix: :with, _suffix: true
|
|
enum difficulty: [:easy, :medium, :hard], _suffix: :to_read
|
|
enum cover: { hard: "hard", soft: "soft" }
|
|
enum boolean_status: { enabled: true, disabled: false }
|
|
|
|
def published!
|
|
super
|
|
"do publish work..."
|
|
end
|
|
end
|
|
|
|
class PublishedBook < ActiveRecord::Base
|
|
self.table_name = "books"
|
|
|
|
validates_uniqueness_of :isbn
|
|
end
|