avo
avo copied to clipboard
Add the ability to change the view type of a field
From here.
Maybe you must use a computed field on an Edit view. You can fake it like in that comment or you could pass in a computed field with the view set to :show. This will render that field as a show field in the edit view.
Example:

Rough PoC:
<% if main_panel.present? %>
<% c.with_body do %>
<div class="divide-y">
<% main_panel.items.each_with_index do |field, index| %>
<% v = field.id == :user_label ? :show : view %>
<%= render field
.hydrate(resource: @resource, model: @resource.model, user: @resource.user, view: view_for(field, forced_view: v))
.component_for_view(view_for(field, forced_view: v))
.new(field: field, resource: @resource, index: index, form: form, compact: sidebar.present?)
%>
<% end %>
</div>
<% end %>
<% end %>
I'm not so sure about "faking" or forcing views. It can easily get confusing. view.show? can return true on forms etc... Or things that are supposed to work on form views will not work. Having 2 views view and forced_view it's also not ideal...
WDYT about using the build in components feature instead?
field :name, as: :text, components: {
edit_component: Avo::Fields::TextField::ShowComponent
} do
"Test"
end
components accepts a proc that ca have some logic and apply this configuration only when user is admin or some other condition.
Also, we can consider adding support for computed fields on form views
@Paul-Bob this works, but breaks on as: :external_image
How does it break?
@adrianthedev
undefined method `as_html' for #<Avo::Fields::ExternalImageField:0x0000000115eb2088