diff --git a/app/views/common_templates/_search_results_template.html.erb b/app/views/common_templates/_search_results_template.html.erb index deb9f250cf..e227c59355 100644 --- a/app/views/common_templates/_search_results_template.html.erb +++ b/app/views/common_templates/_search_results_template.html.erb @@ -387,9 +387,18 @@ <% if full_name.to_s.start_with?('dynamic_model__') && option_type_config_name.to_s == default_option_type_name.to_s %> _fpa.state.caption_before.<%=name.underscore%> = _fpa.state.caption_before.<%=name_with_option_type.underscore%>; _fpa.state.labels_<%=name.underscore%> = _fpa.state.labels_<%=name_with_option_type.underscore%>; - _fpa.state.template_config.<%=resource_name%> = _fpa.state.template_config.<%=resource_name%> || _fpa.state.template_config.<%=name.underscore%>; - _fpa.state.template_config.<%=name.underscore.pluralize%> = _fpa.state.template_config.<%=name.underscore.pluralize%> || _fpa.state.template_config.<%=name.underscore%>; - _fpa.state.template_config.<%=name.underscore.sub('dynamic_model__', '')%> = _fpa.state.template_config.<%=name.underscore.sub('dynamic_model__', '')%> || _fpa.state.template_config.<%=name.underscore%> + <%# Alias the dynamic model's template config under its alternative resource + names (plural resource_name, pluralized name, and short name). The current + version's template object is aliased as before, but we must ALSO alias the + specific def_version key. Without this, when the base object already exists + from the initial page load (the current version), an older version's + template (loaded on demand via the template_config endpoint) is never linked + onto these alternative names, so versioned embedded items fail to render in + show mode (issue #1238). %> + <% [resource_name, name.underscore.pluralize, name.underscore.sub('dynamic_model__', '')].uniq.each do |alias_name| %> + _fpa.state.template_config.<%=alias_name%> = _fpa.state.template_config.<%=alias_name%> || _fpa.state.template_config.<%=name.underscore%>; + _fpa.state.template_config.<%=alias_name%>.v<%=def_version%> = _fpa.state.template_config.<%=alias_name%>.v<%=def_version%> || _fpa.state.template_config.<%=name.underscore%>.v<%=def_version%>; + <% end %> <% end %> <% end %> diff --git a/app/views/dynamic_models/_search_results_template.html.erb b/app/views/dynamic_models/_search_results_template.html.erb index 4c2524985e..a07f38da1a 100644 --- a/app/views/dynamic_models/_search_results_template.html.erb +++ b/app/views/dynamic_models/_search_results_template.html.erb @@ -2,4 +2,15 @@ next unless m.implementation_class_defined?(::DynamicModel, fail_without_exception: true) %> <%= render partial: 'dynamic_models/search_results_template_item', locals: { def_record: m } %> + <%# Also emit JS-only template_config entries for older history versions so that embedded + items created before a definition version bump can still resolve their vN key (issue #1238). + The common _search_results_template already skips Handlebars HTML-template rendering + when def_version is non-nil, so this only produces the lightweight JS config block. %> + <% m.all_versions.each do |historical_version| + # Skip the latest history entry: it corresponds to the current live record, whose + # config is already emitted above with def_version=nil. + next if historical_version.def_version == m.all_versions.first&.def_version + %> + <%= render partial: 'dynamic_models/search_results_template_item', locals: { def_record: historical_version } %> + <% end %> <% end %> diff --git a/spec/requests/activity_log/embedded_versioned_template_config_spec.rb b/spec/requests/activity_log/embedded_versioned_template_config_spec.rb new file mode 100644 index 0000000000..797a920fcb --- /dev/null +++ b/spec/requests/activity_log/embedded_versioned_template_config_spec.rb @@ -0,0 +1,278 @@ +# frozen_string_literal: true + +# Request specs for issue #1238 — activity log embedded dynamic models fail to +# render in show (read-only) mode when the embedded dynamic model uses +# "definition versioning: version at record creation". +# +# User-facing symptom: after saving an activity log embedded form, the +# read-only (show) form does not appear, and the browser console logs: +# fpa_state_item: could not find template_config dynamic_model__... v123 +# Editing the activity correctly shows the edit form, but on save/cancel the +# read-only form is missing. +# +# Root cause exercised here: the activity log `template_config` endpoint builds +# the Handlebars template-config