Skip to content

Commit

Permalink
Allow an object to #becomes a non-Enumerize model. (#449)
Browse files Browse the repository at this point in the history
Co-authored-by: Lorin Thwaits <[email protected]>
  • Loading branch information
nashby and lorint authored Mar 30, 2024
1 parent 0be5ae0 commit 928152a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/enumerize/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def write_attribute(attr_name, value, *options)
# Support multiple enumerized attributes
def becomes(klass)
became = super
return became unless became.respond_to?(:enumerized_attributes)

klass.enumerized_attributes.each do |attr|
# Rescue when column associated to the enum does not exist.
begin
Expand Down
19 changes: 19 additions & 0 deletions test/activerecord_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,25 @@ def self.name
expect(user.interests).must_equal uniq_user.interests
end

it 'allows an object to #becomes a non-Enumerize model' do
class NonEnumerizeUser < ActiveRecord::Base
self.table_name = "users"
end

class EnumerizeUser < NonEnumerizeUser
extend Enumerize

enumerize :role, in: [:admin, :ghost]
end
EnumerizeUser.delete_all

user = EnumerizeUser.create(role: :admin)
expect(user.role).must_equal 'admin'

non_enumerize_user = user.becomes(NonEnumerizeUser)
expect(non_enumerize_user).must_be :valid?
end

it "doesn't update record" do
Document.delete_all

Expand Down

0 comments on commit 928152a

Please sign in to comment.