Skip to content

Commit

Permalink
fix: preview when using *_fields DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Oct 7, 2024
1 parent a53f9a0 commit b99ece6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ def destroy
def preview
@resource.hydrate(record: @record, view: Avo::ViewInquirer.new(:show), user: _current_user, params: params)

@preview_fields = @resource.get_preview_fields

render layout: params[:turbo_frame].blank?
end

Expand Down
4 changes: 2 additions & 2 deletions app/views/avo/base/preview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<div class="text-md font-semibold uppercase text-gray-800">Previewing <%= @resource.record_title %></div>
<% end %>
<% if @resource.get_preview_fields.present? %>
<% if @preview_fields.present? %>
<div class="mt-0 divide-y border-t">
<% @resource.get_preview_fields.each_with_index do |field, index| %>
<% @preview_fields.each_with_index do |field, index| %>
<%= render field
.hydrate(
resource: @resource,
Expand Down
10 changes: 9 additions & 1 deletion lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,17 @@ def detect_fields
end

def fetch_fields
if view.preview?
[:fields, :index_fields, :show_fields, :display_fields].each do |fields_method|
send(fields_method) if respond_to?(fields_method)
end

return
end

possible_methods_for_view = VIEW_METHODS_MAPPING[view.to_sym]

# Safe navigation operator is used because the view can be "destroy" or "preview"
# Safe navigation operator is used because the view can be "destroy"
possible_methods_for_view&.each do |method_for_view|
return send(method_for_view) if respond_to?(method_for_view)
end
Expand Down

0 comments on commit b99ece6

Please sign in to comment.