Given an ActiveRecord class Animal which has a subclass Cat:
class Animal < ApplicationRecord
end
class Cat < Animal
include Enumerize
end
When a form for Animal is built in this kind of way so that it will post back to AnimalsController (instead of CatsController):
<%= form_for(@cat.becomes(Animal)) do |f| %>
...
<% end %>
Then when the overridden version of #becomes from Enumerize is called, this error surfaces:
NoMethodError - undefined method `enumerized_attributes' for Animal:Class:
In order to remedy this issue, it is possible to add a line to reset klass when necessary before calling #enumerized_attributes:
# lib/enumerize/activerecord.rb:
...
def becomes(klass)
became = super
klass = self.class unless klass.respond_to?(:enumerized_attributes) # <-- Add this line
klass.enumerized_attributes.each do |attr|
...
Given an ActiveRecord class
Animal
which has a subclassCat
:When a form for
Animal
is built in this kind of way so that it will post back to AnimalsController (instead of CatsController):Then when the overridden version of
#becomes
from Enumerize is called, this error surfaces:In order to remedy this issue, it is possible to add a line to reset
klass
when necessary before calling #enumerized_attributes:Will open a PR to address this issue.