At the moment, ActiveModel::AttributeAssignment
raises an UnknownAttributeError
if an unknown attribute is provided when using assign_attributes
. This provides safety, but is not really flexible in case the class wants to handle unknown attributes somehow.
A use case for that would be when using Active Model to handle attribute assignments in View Components. Unknown attributes could be treated as HTML attributes to be set on the component’s main element.
To handle this at the moment, you could use the list provided by attribute_names
to split the attributes provided to the assign_attributes
and call it with only the know ones. This feels a bit cumbersome as that method is already looping through each attribute, providing a timely place to handle each unknown assignment.
I’m proposing to refactor the raising of the UnknownAttributeError
into a (protected?) assign_unknown_attribute
method that sub-classes can override if they wish to do something special with unknown attribute.
This concern, built for the use case described earlier, shows both the use of that new method, as well as, the change that would be required in ActiveModel::AttributeAssignment
.
Would this be something relevant to contribute to the project?