Skip to content

Commit

Permalink
fix: enumerize with string store accessor keys
Browse files Browse the repository at this point in the history
Ensures Enumerize is agnostic to whether the key to a store accessor is a
string or symbol
  • Loading branch information
RickCSong authored and 4ndypanda committed May 21, 2024
1 parent 0be5ae0 commit 10d19da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/enumerize/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def reload(options = nil)
begin
# Checks first if the enumerized attribute is in ActiveRecord::Store
store_attr, _ = reloaded.class.stored_attributes.detect do |_store_attr, keys|
keys.include?(attr.name)
keys.map(&:to_sym).include?(attr.name.to_sym)
end

if store_attr.present?
Expand Down
12 changes: 11 additions & 1 deletion test/activerecord_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ class User < ActiveRecord::Base
extend Enumerize
include RoleEnum

store :settings, accessors: [:language]
store :settings, accessors: [:language, 'country_code']

enumerize :sex, :in => [:male, :female], scope: :shallow
enumerize :language, :in => [:en, :jp]
enumerize :country_code, :in => [:us, :ca]

serialize :interests, type: Array
enumerize :interests, :in => [:music, :sports, :dancing, :programming], :multiple => true
Expand Down Expand Up @@ -211,6 +212,15 @@ class ActiveRecordTest < Minitest::Spec
expect(user.origin).must_be_nil
end

it 'saves stored attribute value for store accessor with string key' do
User.delete_all
user = User.new
user.country_code = :us
user.save!
user.reload
expect(user.country_code).must_equal 'us'
end

it 'has default value' do
expect(User.new.role).must_equal 'user'
expect(User.new.attributes['role']).must_equal 'user'
Expand Down

0 comments on commit 10d19da

Please sign in to comment.