1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/fixtures/books.yml
Yoshiyuki Hirano f4fbdb1b4e Allow AR::Enum definitions with boolean values
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.
2019-12-24 07:05:42 -10:00

34 lines
587 B
YAML

awdr:
author_id: 1
id: 1
name: "Agile Web Development with Rails"
format: "paperback"
status: :published
read_status: :read
language: :english
author_visibility: :visible
illustrator_visibility: :visible
font_size: :medium
difficulty: :medium
boolean_status: :enabled
rfr:
author_id: 1
id: 2
name: "Ruby for Rails"
format: "ebook"
status: "proposed"
read_status: "reading"
ddd:
author_id: 1
id: 3
name: "Domain-Driven Design"
format: "hardcover"
status: 2
read_status: "forgotten"
tlg:
author_id: 1
id: 4
name: "Thoughtleadering"