Skip to content

Commit 857129c

Browse files
authored
Merge pull request #305 from brainspec/fix/issue-304
Fallback to a raw passed value if AR type can't find value in the attribute.
2 parents 4d6a66d + 6c9d868 commit 857129c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### bug fix
66

77
* Fix RailsAdmin integration when enumerated field used on edit form and enumerated value wasn't set. (by [@nashby](https://github.com/nashby))
8+
* Fallback to a raw passed value instead of nil if AR type can't find value in the attribute. (by [@nashby](https://github.com/nashby))
89

910
## 2.1.2 (May 18, 2017)
1011

@@ -43,11 +44,11 @@
4344
* Drop support for Rails 4.0 and 4.1. Support only Rails 4.2 and newer. (by [@lest](https://github.com/lest))
4445
* Support Rails 5.0. (by [@nashby](https://github.com/nashby) and [@lest](https://github.com/lest))
4546
* Allow to pass enumerize values to `ActiveRecord#update_all` (by [@DmitryTsepelev](https://github.com/DmitryTsepelev) and [@ianwhite](https://github.com/ianwhite))
46-
47+
4748
```ruby
4849
User.update_all(status: :blocked)
4950
```
50-
51+
5152
### bug fix
5253

5354
* Rescue MissingAttributeError on attribute writing. (by [@embs](https://github.com/embs))

lib/enumerize/activerecord.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def initialize(attr, subtype)
7676

7777
def serialize(value)
7878
v = @attr.find_value(value)
79-
v && v.value
79+
(v && v.value) || value
8080
end
8181

8282
alias type_cast_for_database serialize

test/activerecord_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,12 @@ class InterestsRequiredUser < User
522522
user = YAML.load(User.create(status: :blocked).to_yaml)
523523
user.status.must_equal 'blocked'
524524
end
525+
526+
# https://github.com/brainspec/enumerize/issues/304
527+
it "fallbacks to a raw passed value if AR type can't find value in the attribute" do
528+
table = User.arel_table
529+
sql = User.where(table[:account_type].matches '%foo%').to_sql
530+
531+
sql.must_include 'LIKE \'%foo%\''
532+
end
525533
end

0 commit comments

Comments
 (0)