This results in having to declare the extended properties in the translation for the base record class, which is confusing and pollutes the translations.
Solutions
My first local hack was to do this:
def self.model_name
super.tap{|n| n.instance_variable_set(:@i18n_key, name.underscore) }
end
Which is of course super hacky. An alternative would be to create a Name subclass:
class ActiveTypeName < ActiveModel::Name
def initialize(klass, extended_record_base_klass)
super(extended_record_base_klass)
@i18n_key = klass.name.underscore
end
attr_reader :i18n_key # Redefine to avoid depending on internal rails behavior
end
and then redefine model_name:
def model_name
@model_name ||= ActiveTypeName.new(self, extended_record_base_class)
end
A third alternative would be to monkey_patch i18n_key:
def model_name
@model_name ||= begin
dup_model_name = extended_record_base_class.model_name.dup
key = name.underscore
dup_model_name.define_singleton_method(:i18n_key) { key }
dup_model_name
end
end
Which creates pollution for the base class translations.
The setup
Expected Result
Actual Result
Investigation
Turns out that
ActiveType::RecordExtension::Inheritance
simply delegatesmodel_name
to the base record class:https://github.com/makandra/active_type/blob/0973a8fb300f7c2dcb8d7aa903119dd23506ccaf/lib/active_type/record_extension/inheritance.rb#L30-L32
AFAICT, this necessary because it is desired that
param_key
and friends be the same. However, this has the problem thati18n_key
is the same too:This results in having to declare the extended properties in the translation for the base record class, which is confusing and pollutes the translations.
Solutions
My first local hack was to do this:
Which is of course super hacky. An alternative would be to create a Name subclass:
and then redefine
model_name
:A third alternative would be to monkey_patch
i18n_key
: