From 10d19da866ea29d4bfc1b02e4820b25d17a73d33 Mon Sep 17 00:00:00 2001 From: Rick Song Date: Mon, 27 Jun 2022 12:52:44 -0700 Subject: [PATCH] fix: enumerize with string store accessor keys Ensures Enumerize is agnostic to whether the key to a store accessor is a string or symbol --- lib/enumerize/activerecord.rb | 2 +- test/activerecord_test.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/enumerize/activerecord.rb b/lib/enumerize/activerecord.rb index f6dec5f..0a26ee3 100644 --- a/lib/enumerize/activerecord.rb +++ b/lib/enumerize/activerecord.rb @@ -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? diff --git a/test/activerecord_test.rb b/test/activerecord_test.rb index aa51e13..cebf787 100644 --- a/test/activerecord_test.rb +++ b/test/activerecord_test.rb @@ -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 @@ -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'