Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow an object to #becomes a non-Enumerize model. #449

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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