avo 2.15.3 → 2.16.1.pre.1.nativefields

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of avo might be problematic. Click here for more details.

Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -2
  3. data/Gemfile.lock +78 -86
  4. data/app/components/avo/base_component.rb +7 -1
  5. data/app/components/avo/field_wrapper_component.html.erb +40 -0
  6. data/app/components/avo/field_wrapper_component.rb +102 -0
  7. data/app/components/avo/fields/badge_field/show_component.html.erb +1 -1
  8. data/app/components/avo/fields/belongs_to_field/edit_component.html.erb +3 -3
  9. data/app/components/avo/fields/belongs_to_field/index_component.html.erb +2 -1
  10. data/app/components/avo/fields/belongs_to_field/show_component.html.erb +2 -2
  11. data/app/components/avo/fields/belongs_to_field/show_component.rb +8 -0
  12. data/app/components/avo/fields/boolean_field/edit_component.html.erb +1 -1
  13. data/app/components/avo/fields/boolean_field/show_component.html.erb +1 -1
  14. data/app/components/avo/fields/boolean_group_field/edit_component.html.erb +1 -1
  15. data/app/components/avo/fields/boolean_group_field/show_component.html.erb +1 -1
  16. data/app/components/avo/fields/code_field/edit_component.html.erb +2 -2
  17. data/app/components/avo/fields/code_field/show_component.html.erb +2 -2
  18. data/app/components/avo/fields/country_field/edit_component.html.erb +1 -1
  19. data/app/components/avo/fields/country_field/show_component.html.erb +1 -1
  20. data/app/components/avo/fields/date_field/edit_component.html.erb +1 -1
  21. data/app/components/avo/fields/date_field/show_component.html.erb +1 -1
  22. data/app/components/avo/fields/date_time_field/edit_component.html.erb +1 -1
  23. data/app/components/avo/fields/date_time_field/show_component.html.erb +1 -1
  24. data/app/components/avo/fields/edit_component.rb +22 -4
  25. data/app/components/avo/fields/external_image_field/edit_component.html.erb +1 -1
  26. data/app/components/avo/fields/external_image_field/index_component.html.erb +1 -1
  27. data/app/components/avo/fields/external_image_field/show_component.html.erb +1 -1
  28. data/app/components/avo/fields/file_field/edit_component.html.erb +1 -1
  29. data/app/components/avo/fields/file_field/index_component.html.erb +2 -2
  30. data/app/components/avo/fields/file_field/show_component.html.erb +1 -1
  31. data/app/components/avo/fields/files_field/edit_component.html.erb +1 -1
  32. data/app/components/avo/fields/files_field/show_component.html.erb +1 -1
  33. data/app/components/avo/fields/gravatar_field/index_component.html.erb +1 -1
  34. data/app/components/avo/fields/gravatar_field/show_component.html.erb +1 -1
  35. data/app/components/avo/fields/has_one_field/show_component.html.erb +14 -1
  36. data/app/components/avo/fields/has_one_field/show_component.rb +21 -0
  37. data/app/components/avo/fields/id_field/edit_component.html.erb +1 -1
  38. data/app/components/avo/fields/id_field/index_component.html.erb +1 -1
  39. data/app/components/avo/fields/id_field/show_component.html.erb +1 -1
  40. data/app/components/avo/fields/index_component.rb +9 -4
  41. data/app/components/avo/fields/key_value_field/edit_component.html.erb +2 -2
  42. data/app/components/avo/fields/key_value_field/show_component.html.erb +1 -1
  43. data/app/components/avo/fields/markdown_field/edit_component.html.erb +2 -2
  44. data/app/components/avo/fields/markdown_field/show_component.html.erb +1 -1
  45. data/app/components/avo/fields/number_field/edit_component.html.erb +1 -1
  46. data/app/components/avo/fields/number_field/show_component.html.erb +1 -1
  47. data/app/components/avo/fields/password_field/edit_component.html.erb +1 -1
  48. data/app/components/avo/fields/progress_bar_field/edit_component.html.erb +1 -1
  49. data/app/components/avo/fields/progress_bar_field/show_component.html.erb +1 -1
  50. data/app/components/avo/fields/select_field/edit_component.html.erb +1 -2
  51. data/app/components/avo/fields/select_field/show_component.html.erb +1 -1
  52. data/app/components/avo/fields/show_component.rb +36 -1
  53. data/app/components/avo/fields/status_field/edit_component.html.erb +1 -1
  54. data/app/components/avo/fields/status_field/show_component.html.erb +1 -1
  55. data/app/components/avo/fields/tags_field/edit_component.html.erb +1 -5
  56. data/app/components/avo/fields/tags_field/show_component.html.erb +1 -1
  57. data/app/components/avo/fields/text_field/edit_component.html.erb +2 -2
  58. data/app/components/avo/fields/text_field/index_component.html.erb +1 -1
  59. data/app/components/avo/fields/text_field/show_component.html.erb +1 -1
  60. data/app/components/avo/fields/textarea_field/edit_component.html.erb +1 -1
  61. data/app/components/avo/fields/textarea_field/show_component.html.erb +1 -1
  62. data/app/components/avo/fields/trix_field/edit_component.html.erb +13 -13
  63. data/app/components/avo/fields/trix_field/edit_component.rb +19 -1
  64. data/app/components/avo/fields/trix_field/show_component.html.erb +1 -1
  65. data/app/components/avo/index/grid_item_component.html.erb +6 -6
  66. data/app/components/avo/index/grid_item_component.rb +15 -0
  67. data/app/components/avo/index/resource_controls_component.rb +3 -0
  68. data/app/components/avo/index/resource_grid_component.rb +1 -1
  69. data/app/components/avo/resource_component.rb +5 -1
  70. data/app/components/avo/sidebar/item_switcher_component.html.erb +3 -3
  71. data/app/components/avo/sidebar/link_component.html.erb +2 -2
  72. data/app/components/avo/sidebar/link_component.rb +3 -1
  73. data/app/components/avo/views/resource_edit_component.html.erb +47 -28
  74. data/app/components/avo/views/resource_edit_component.rb +26 -9
  75. data/app/components/avo/views/resource_show_component.rb +0 -8
  76. data/app/controllers/avo/actions_controller.rb +7 -5
  77. data/app/controllers/avo/application_controller.rb +22 -3
  78. data/app/controllers/avo/associations_controller.rb +2 -2
  79. data/app/controllers/avo/base_controller.rb +24 -11
  80. data/app/helpers/avo/application_helper.rb +31 -3
  81. data/app/helpers/avo/resources_helper.rb +4 -8
  82. data/app/helpers/avo/url_helpers.rb +8 -0
  83. data/app/javascript/js/controllers/action_controller.js +3 -1
  84. data/app/javascript/js/controllers/fields/date_field_controller.js +21 -1
  85. data/app/javascript/js/controllers/search_controller.js +122 -118
  86. data/app/views/avo/actions/show.html.erb +5 -1
  87. data/avo.gemspec +1 -1
  88. data/config/master.key +1 -0
  89. data/lib/avo/base_action.rb +25 -6
  90. data/lib/avo/base_resource.rb +6 -4
  91. data/lib/avo/base_resource_tool.rb +1 -1
  92. data/lib/avo/concerns/fetches_things.rb +2 -0
  93. data/lib/avo/configuration.rb +2 -0
  94. data/lib/avo/fields/base_field.rb +5 -1
  95. data/lib/avo/fields/belongs_to_field.rb +1 -1
  96. data/lib/avo/fields/has_and_belongs_to_many_field.rb +1 -1
  97. data/lib/avo/fields/has_base_field.rb +5 -1
  98. data/lib/avo/fields/has_many_field.rb +1 -1
  99. data/lib/avo/fields/select_field.rb +1 -0
  100. data/lib/avo/menu/base_item.rb +1 -0
  101. data/lib/avo/menu/builder.rb +4 -2
  102. data/lib/avo/menu/menu.rb +2 -0
  103. data/lib/avo/services/authorization_service.rb +24 -20
  104. data/lib/avo/version.rb +1 -1
  105. data/lib/generators/avo/templates/field/components/edit_component.html.erb.tt +1 -1
  106. data/lib/generators/avo/templates/field/components/show_component.html.erb.tt +1 -1
  107. data/public/avo-assets/avo.base.css +26 -8
  108. data/public/avo-assets/avo.base.js +79 -79
  109. data/public/avo-assets/avo.base.js.map +3 -3
  110. metadata +8 -11
  111. data/app/components/avo/common_field_wrapper_component.html.erb +0 -26
  112. data/app/components/avo/common_field_wrapper_component.rb +0 -46
  113. data/app/components/avo/edit/field_wrapper_component.html.erb +0 -11
  114. data/app/components/avo/edit/field_wrapper_component.rb +0 -21
  115. data/app/components/avo/show/field_wrapper_component.html.erb +0 -9
  116. data/app/components/avo/show/field_wrapper_component.rb +0 -12
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <% if @field.display_code %>
3
3
  <%= @field.value %>
4
4
  <% else %>
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= content_tag :div, data: {
3
3
  controller: "date-field",
4
4
  date_field_view_value: @view,
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= content_tag :div, data: {
3
3
  controller: "date-field",
4
4
  date_field_view_value: @view,
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= content_tag :div, data: {
3
3
  controller: "date-field",
4
4
  date_field_view_value: @view,
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= content_tag :div, data: {
3
3
  controller: "date-field",
4
4
  date_field_view_value: @view,
@@ -3,15 +3,21 @@
3
3
  class Avo::Fields::EditComponent < ViewComponent::Base
4
4
  include Avo::ResourcesHelper
5
5
 
6
+ attr_reader :compact
7
+ attr_reader :displayed_in_modal
6
8
  attr_reader :field
9
+ attr_reader :form
10
+ attr_reader :index
11
+ attr_reader :resource
7
12
  attr_reader :view
8
13
 
9
- def initialize(field: nil, resource: nil, index: 0, form: nil, displayed_in_modal: false)
14
+ def initialize(field: nil, resource: nil, index: 0, form: nil, displayed_in_modal: false, compact: false, **kwargs)
15
+ @compact = compact
16
+ @displayed_in_modal = displayed_in_modal
10
17
  @field = field
11
- @resource = resource
12
- @index = index
13
18
  @form = form
14
- @displayed_in_modal = displayed_in_modal
19
+ @index = index
20
+ @resource = resource
15
21
  @view = :edit
16
22
  end
17
23
 
@@ -22,4 +28,16 @@ class Avo::Fields::EditComponent < ViewComponent::Base
22
28
  def render?
23
29
  !field.computed
24
30
  end
31
+
32
+ def field_wrapper_args
33
+ {
34
+ compact: compact,
35
+ displayed_in_modal: displayed_in_modal,
36
+ field: field,
37
+ form: form,
38
+ index: index,
39
+ resource: resource,
40
+ view: view
41
+ }
42
+ end
25
43
  end
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @form.text_field @field.id,
3
3
  class: classes("w-full"),
4
4
  data: @field.get_html(:data, view: view, element: :input),
@@ -4,7 +4,7 @@
4
4
  image_tag(@field.value,
5
5
  height: @field.height,
6
6
  style: "border-radius: #{@field.radius}px; max-height: #{@field.height}#{@field.height.to_s&.ends_with?('px') ? '' : 'px'};"),
7
- resource_path
7
+ resource_view_path
8
8
  %>
9
9
  <% end %>
10
10
  <% end %>
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <% if @field.value.present? %>
3
3
  <%= image_tag @field.value, style: 'max-width: 340px;' %>
4
4
  <% end %>
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <% if @field.value.present? %>
3
3
  <div class="mb-2">
4
4
  <%= render Avo::Fields::Common::SingleFileViewerComponent.new resource: @resource, field: @field %>
@@ -1,9 +1,9 @@
1
1
  <%= index_field_wrapper field: @field, resource: @resource, flush: flush? do %>
2
2
  <% if @field.value.present? %>
3
3
  <% if @field.value.attached? && @field.value.representable? && @field.is_image %>
4
- <%= link_to_if @field.link_to_resource, image_tag(helpers.main_app.url_for(@field.value), class: 'h-10'), resource_path, class: 'block' %>
4
+ <%= link_to_if @field.link_to_resource, image_tag(helpers.main_app.url_for(@field.value), class: 'h-10'), resource_view_path, class: 'block' %>
5
5
  <% elsif @field.value.attached? && @field.is_audio %>
6
- <%= link_to_if @field.link_to_resource, audio_tag(helpers.main_app.url_for(@field.value), controls: true, preload: false, class: 'max-h-full h-10'), resource_path, class: 'block h-8' %>
6
+ <%= link_to_if @field.link_to_resource, audio_tag(helpers.main_app.url_for(@field.value), controls: true, preload: false, class: 'max-h-full h-10'), resource_view_path, class: 'block h-8' %>
7
7
  <% else %>
8
8
  <%= @field.value.filename %>
9
9
  <% end %>
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= render Avo::Fields::Common::SingleFileViewerComponent.new resource: @resource, field: @field %>
3
3
  <% end %>
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal, full_width: true do %>
1
+ <%= field_wrapper **field_wrapper_args, full_width: true do %>
2
2
  <%= render Avo::Fields::Common::FilesListViewerComponent.new(field: @field, resource: @resource) if @field.value.present? %>
3
3
 
4
4
  <% if @resource.authorization.authorize_action(:upload_attachments?, raise_exception: false) %>
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, full_width: true do %>
1
+ <%= field_wrapper **field_wrapper_args, full_width: true do %>
2
2
  <%= render Avo::Fields::Common::FilesListViewerComponent.new(field: @field, resource: @resource) if @field.value.present? %>
3
3
  <% end %>
@@ -5,7 +5,7 @@
5
5
  size: @field.size,
6
6
  rounded: @field.rounded,
7
7
  link_to_resource: @field.link_to_resource,
8
- link: resource_path,
8
+ link: resource_view_path,
9
9
  title: t('avo.view_item', item: @resource.name).humanize
10
10
  )
11
11
  %>
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= render Avo::Fields::Common::GravatarViewerComponent.new(
3
3
  md5: @field.md5,
4
4
  default: @field.default,
@@ -6,10 +6,23 @@
6
6
  <%= render Avo::PanelComponent.new(name: @field.name) do |c| %>
7
7
  <% c.tools do %>
8
8
  <% if !@field.is_readonly? && can_attach? %>
9
- <%= a_link attach_path, icon: 'heroicons/outline/link', color: :primary, 'data-turbo-frame': 'attach_modal' do %>
9
+ <%= a_link attach_path,
10
+ icon: 'heroicons/outline/link',
11
+ color: :primary,
12
+ 'data-turbo-frame': 'attach_modal' do %>
10
13
  <%= t('avo.attach_item', item: @field.name.downcase) %>
11
14
  <% end %>
12
15
  <% end %>
16
+ <% if !@field.is_readonly? && can_see_the_create_button? %>
17
+ <%= a_link create_path,
18
+ icon: 'heroicons/outline/plus',
19
+ 'data-target': 'create',
20
+ 'data-turbo-frame': '_top',
21
+ style: :primary,
22
+ color: :primary do %>
23
+ <%= t('avo.create_new_item', item: @field.name.downcase ) %>
24
+ <% end %>
25
+ <% end %>
13
26
  <% end %>
14
27
 
15
28
  <% c.body do %>
@@ -23,4 +23,25 @@ class Avo::Fields::HasOneField::ShowComponent < Avo::Fields::ShowComponent
23
23
  def attach_path
24
24
  helpers.avo.resources_associations_new_path(@resource.singular_model_key, @resource.model.id, @field.id)
25
25
  end
26
+
27
+ def can_see_the_create_button?
28
+ create = "create_#{@field.id.to_s}?"
29
+
30
+ authorization_service = @resource.authorization
31
+
32
+ # By default return true if the create method is not defined for this field
33
+ return true unless authorization_service.has_method?(create, raise_exception: false)
34
+
35
+ authorization_service.authorize_action(create, raise_exception: false)
36
+ end
37
+
38
+ def create_path
39
+ args = {
40
+ via_relation: @resource.singular_model_key,
41
+ via_relation_class: @resource.name,
42
+ via_resource_id: @resource.model.id
43
+ }
44
+
45
+ helpers.new_resource_path(resource: @field.target_resource, **args)
46
+ end
26
47
  end
@@ -1,3 +1,3 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @field.value %>
3
3
  <% end %>
@@ -1,3 +1,3 @@
1
1
  <%= index_field_wrapper field: @field, resource: @resource, class: 'whitespace-no-wrap w-[1%]' do %>
2
- <% link_to_if (@field.link_to_resource or Avo.configuration.id_links_to_resource), @field.value, resource_path, title: t('avo.view_item', item: @resource.name).humanize %>
2
+ <% link_to_if (@field.link_to_resource or Avo.configuration.id_links_to_resource), @field.value, resource_view_path, title: t('avo.view_item', item: @resource.name).humanize %>
3
3
  <% end %>
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @field.value %>
3
3
  <% end %>
@@ -13,11 +13,16 @@ class Avo::Fields::IndexComponent < ViewComponent::Base
13
13
  @view = :index
14
14
  end
15
15
 
16
- def resource_path
16
+ def resource_view_path
17
+ args = {}
18
+
17
19
  if @parent_model.present?
18
- helpers.resource_path(model: @resource.model, resource: @resource, via_resource_class: @parent_model.class, via_resource_id: @parent_model.id)
19
- else
20
- helpers.resource_path(model: @resource.model, resource: @resource)
20
+ args = {
21
+ via_resource_class: @parent_model.class,
22
+ via_resource_id: @parent_model.id
23
+ }
21
24
  end
25
+
26
+ helpers.resource_view_path(model: @resource.model, resource: @resource, **args)
22
27
  end
23
28
  end
@@ -1,3 +1,3 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal, full_width: true do %>
2
- <%= render Avo::Fields::Common::KeyValueComponent.new field: @field, form: @form, view: @resource.view %>
1
+ <%= field_wrapper **field_wrapper_args, full_width: true do %>
2
+ <%= render Avo::Fields::Common::KeyValueComponent.new field: @field, form: @form, view: view %>
3
3
  <% end %>
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index, full_width: true do %>
1
+ <%= field_wrapper **field_wrapper_args, full_width: true do %>
2
2
  <%= render Avo::Fields::Common::KeyValueComponent.new field: @field, view: :show %>
3
3
  <% end %>
@@ -1,9 +1,9 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal, full_width: true do %>
1
+ <%= field_wrapper **field_wrapper_args, full_width: true do %>
2
2
  <div data-controller="simple-mde">
3
3
  <%= @form.text_area @field.id,
4
4
  class: classes("w-full js-has-simple-mde-editor"),
5
5
  data: {
6
- view: @resource.view,
6
+ view: view,
7
7
  'simple-mde-target': 'element',
8
8
  'component-options': @field.options.to_json,
9
9
  },
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index, full_width: true do %>
1
+ <%= field_wrapper **field_wrapper_args, full_width: true do %>
2
2
  <div data-controller="simple-mde">
3
3
  <%= text_area_tag @field.id, @field.value,
4
4
  class: helpers.input_classes('w-full js-has-simple-mde-editor'),
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @form.number_field @field.id,
3
3
  class: classes("w-full"),
4
4
  data: @field.get_html(:data, view: view, element: :input),
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @field.value %>
3
3
  <% end %>
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @form.password_field @field.id,
3
3
  class: classes("w-full"),
4
4
  data: @field.get_html(:data, view: view, element: :input),
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <% if @field.display_value %>
3
3
  <div class="text-center text-sm font-semibold w-full leading-none mb-1">
4
4
  <span class="js-progress-bar-value-<%= @field.id %>"><%= @field.value %></span><%= @field.value_suffix if @field.value_suffix.present? %>
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= render Avo::Fields::Common::ProgressBarComponent.new value: @field.value, display_value: @field.display_value, value_suffix: @field.value_suffix, max: @field.max, view: view %>
3
3
  <% end %>
@@ -1,6 +1,5 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @form.select @field.id, @field.options_for_select, {
3
- selected: @field.value,
4
3
  include_blank: @field.include_blank
5
4
  },
6
5
  aria: {
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @field.label %>
3
3
  <% end %>
@@ -3,12 +3,47 @@
3
3
  class Avo::Fields::ShowComponent < ViewComponent::Base
4
4
  include Avo::ResourcesHelper
5
5
 
6
+ attr_reader :field
7
+ attr_reader :index
8
+ attr_reader :resource
6
9
  attr_reader :view
7
10
 
8
11
  def initialize(field: nil, resource: nil, index: 0, form: nil)
9
12
  @field = field
10
- @resource = resource
11
13
  @index = index
14
+ @resource = resource
12
15
  @view = :show
13
16
  end
17
+
18
+ def wrapper_data
19
+ {
20
+ **stimulus_attributes
21
+ }
22
+ end
23
+
24
+ def stimulus_attributes
25
+ attributes = {}
26
+
27
+ if @resource.present?
28
+ @resource.get_stimulus_controllers.split(" ").each do |controller|
29
+ attributes["#{controller}-target"] = "#{@field.id.to_s.underscore}_#{@field.type.to_s.underscore}_wrapper".camelize(:lower)
30
+ end
31
+ end
32
+
33
+ wrapper_data_attributes = @field.get_html :data, view: view, element: :wrapper
34
+ if wrapper_data_attributes.present?
35
+ attributes.merge! wrapper_data_attributes
36
+ end
37
+
38
+ attributes
39
+ end
40
+
41
+ def field_wrapper_args
42
+ {
43
+ field: field,
44
+ index: index,
45
+ resource: resource,
46
+ view: view
47
+ }
48
+ end
14
49
  end
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @form.text_field @field.id,
3
3
  class: classes("w-full"),
4
4
  data: @field.get_html(:data, view: view, element: :input),
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= render Avo::Fields::Common::StatusViewerComponent.new label: @field.value, status: @field.status %>
3
3
  <% end %>
@@ -1,8 +1,4 @@
1
- <%= edit_field_wrapper field: @field,
2
- index: @index,
3
- form: @form,
4
- resource: @resource,
5
- displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
6
2
  <div data-controller="tags-field">
7
3
  <%# dummy field %>
8
4
  <%= text_field_tag "#{@field.id}-dummy", '',
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <div class="flex gap-1 items-center flex-wrap">
3
3
  <% @field.field_value.each do |item| %>
4
4
  <%= render Avo::Fields::TagsField::TagComponent.new(label: label_from_item(item)) %>
@@ -1,5 +1,5 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
2
- <%= @form.text_field @field.id,
1
+ <%= field_wrapper **field_wrapper_args do %>
2
+ <%= form.text_field @field.id,
3
3
  class: classes("w-full"),
4
4
  data: @field.get_html(:data, view: view, element: :input),
5
5
  disabled: @field.is_readonly?,
@@ -4,6 +4,6 @@
4
4
  <% elsif @field.protocol.present? %>
5
5
  <%= link_to @field.value, "#{@field.protocol}:#{@field.value}" %>
6
6
  <% else %>
7
- <%= link_to_if @field.link_to_resource, @field.value, resource_path %>
7
+ <%= link_to_if @field.link_to_resource, @field.value, resource_view_path %>
8
8
  <% end %>
9
9
  <% end %>
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <% if @field.as_html %>
3
3
  <%== @field.value %>
4
4
  <% elsif @field.protocol.present? %>
@@ -1,4 +1,4 @@
1
- <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @form.text_area @field.id,
3
3
  class: classes("w-full"),
4
4
  data: @field.get_html(:data, view: view, element: :input),
@@ -1,3 +1,3 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%= @field.value %>
3
3
  <% end %>
@@ -1,16 +1,16 @@
1
1
  <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal, full_width: true do %>
2
- <div
3
- data-controller="trix-field"
4
- data-trix-field-target="controller"
5
- data-resource-name="<%= @resource.model_key %>"
6
- data-resource-id="<%= @resource.model.id %>"
7
- data-attachments-disabled="<%= @field.attachments_disabled %>"
8
- data-attachment-key="<%= @field.attachment_key %>"
9
- data-hide-attachment-filename="<%= @field.hide_attachment_filename %>"
10
- data-hide-attachment-filesize="<%= @field.hide_attachment_filesize %>"
11
- data-hide-attachment-url="<%= @field.hide_attachment_url %>"
12
- class="relative block overflow-x-auto max-w-full"
13
- >
2
+ <%= content_tag :div, class: "relative block overflow-x-auto max-w-full",
3
+ data: {
4
+ controller: "trix-field",
5
+ trix_field_target: "controller",
6
+ resource_name: resource_name,
7
+ resource_id: resource_id,
8
+ attachments_disabled: @field.attachments_disabled,
9
+ attachment_key: @field.attachment_key,
10
+ hide_attachment_filename: @field.hide_attachment_filename,
11
+ hide_attachment_filesize: @field.hide_attachment_filesize,
12
+ hide_attachment_url: @field.hide_attachment_url,
13
+ } do %>
14
14
  <%= content_tag 'trix-editor',
15
15
  class: 'trix-content',
16
16
  data: {
@@ -29,5 +29,5 @@
29
29
  placeholder: @field.placeholder,
30
30
  style: @field.get_html(:style, view: view, element: :input)
31
31
  %>
32
- </div>
32
+ <% end %>
33
33
  <% end %>
@@ -1,7 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Avo::Fields::TrixField::EditComponent < Avo::Fields::EditComponent
4
+ attr_reader :resource
5
+
6
+ def initialize(**args)
7
+ @resource = args[:resource]
8
+ @resource_id = args[:resource_id]
9
+ @resource_name = args[:resource_name]
10
+
11
+ super(**args)
12
+ end
13
+
14
+ def resource_name
15
+ @resource_name || resource&.singular_route_key
16
+ end
17
+
18
+ def resource_id
19
+ @resource_id || resource&.model&.id
20
+ end
21
+
4
22
  def trix_id
5
- "trix_#{@resource.class_name_without_resource.underscore}_#{@field.id}"
23
+ "trix_#{resource_name}_#{@field.id}"
6
24
  end
7
25
  end
@@ -1,4 +1,4 @@
1
- <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
1
+ <%= field_wrapper **field_wrapper_args do %>
2
2
  <%
3
3
  content_classes = 'trix-content py-2'
4
4
  content_classes << ' hidden' unless @field.always_show
@@ -7,23 +7,23 @@
7
7
  <%== item_selector_input floating: true, size: :lg %>
8
8
  <% end %>
9
9
  <% if cover.blank? %>
10
- <%= link_to cover.link_to_resource do %>
10
+ <%= link_to resource_view_path do %>
11
11
  <%= render Avo::Index::GridCoverEmptyStateComponent.new %>
12
12
  <% end %>
13
13
  <% elsif cover.respond_to?(:to_image) && cover.to_image.present? %>
14
- <%= link_to_if cover.link_to_resource, image_tag(cover.to_image, class: 'absolute h-full w-full object-cover'), helpers.resource_path(model: @resource.model, resource: @resource), class: 'absolute h-full w-full object-cover', title: title.value %>
14
+ <%= link_to_if cover.link_to_resource, image_tag(cover.to_image, class: 'absolute h-full w-full object-cover'), resource_view_path, class: 'absolute h-full w-full object-cover', title: title.value %>
15
15
  <% elsif cover.type == 'file' %>
16
16
  <% if cover.value.attached? && cover.value.representable? %>
17
- <%= link_to_if cover.link_to_resource, image_tag(helpers.main_app.url_for(cover.value.variant(resize_to_limit: [480, 480])), class: 'absolute h-full w-full object-cover'), helpers.resource_path(model: @resource.model, resource: @resource), class: 'absolute h-full w-full object-cover', title: title.value %>
17
+ <%= link_to_if cover.link_to_resource, image_tag(helpers.main_app.url_for(cover.value.variant(resize_to_limit: [480, 480])), class: 'absolute h-full w-full object-cover'), resource_view_path, class: 'absolute h-full w-full object-cover', title: title.value %>
18
18
  <% else %>
19
- <%= link_to @resource.record_path do %>
19
+ <%= link_to resource_view_path do %>
20
20
  <div class="absolute bg-gray-100 w-full h-full">
21
21
  <%= helpers.svg 'avocado', class: 'relative transform -translate-x-1/2 -translate-y-1/2 h-20 text-gray-400 inset-auto top-1/2 left-1/2' %>
22
22
  </div>
23
23
  <% end %>
24
24
  <% end %>
25
25
  <% elsif cover.value.present? %>
26
- <%= link_to_if cover.link_to_resource, image_tag(cover.value, class: 'absolute h-full w-full object-cover'), helpers.resource_path(model: @resource.model, resource: @resource), class: 'absolute h-full w-full object-cover', title: title.value %>
26
+ <%= link_to_if cover.link_to_resource, image_tag(cover.value, class: 'absolute h-full w-full object-cover'), resource_view_path, class: 'absolute h-full w-full object-cover', title: title.value %>
27
27
  <% else %>
28
28
  <%= render Avo::Index::GridCoverEmptyStateComponent.new %>
29
29
  <% end %>
@@ -31,7 +31,7 @@
31
31
  <div class="grid grid-cols-1 place-content-between p-4 h-full">
32
32
  <div class="mb-4 h-full flex flex-col space-between">
33
33
  <div class="grid font-semibold leading-tight text-lg mb-2">
34
- <%= link_to_if title.link_to_resource, title.value, helpers.resource_path(model: @resource.model, resource: @resource) if title.present? %>
34
+ <%= link_to_if title.link_to_resource, title.value, resource_view_path if title.present? %>
35
35
  </div>
36
36
  <div class="text-sm break-words text-gray-500">
37
37
  <%= body.value if body.present? %>
@@ -3,6 +3,8 @@
3
3
  class Avo::Index::GridItemComponent < ViewComponent::Base
4
4
  include Avo::ResourcesHelper
5
5
 
6
+ attr_reader :parent_resource
7
+
6
8
  def initialize(resource: nil, reflection: nil, parent_model: nil, parent_resource: nil)
7
9
  @resource = resource
8
10
  @reflection = reflection
@@ -24,4 +26,17 @@ class Avo::Index::GridItemComponent < ViewComponent::Base
24
26
  def body
25
27
  @grid_fields.body_field
26
28
  end
29
+
30
+ def resource_view_path
31
+ args = {}
32
+
33
+ if @parent_model.present?
34
+ args = {
35
+ via_resource_class: parent_resource.model_class,
36
+ via_resource_id: @parent_model.id
37
+ }
38
+ end
39
+
40
+ helpers.resource_view_path(model: @resource.model, resource: @resource, **args)
41
+ end
27
42
  end
@@ -23,6 +23,8 @@ class Avo::Index::ResourceControlsComponent < Avo::ResourceComponent
23
23
  end
24
24
 
25
25
  def can_view?
26
+ return false if Avo.configuration.resource_default_view == :edit
27
+
26
28
  return authorize_association_for(:show) if @reflection.present?
27
29
 
28
30
  # Even if there's a @reflection object present, for show we're going to fallback to the original policy.
@@ -65,6 +67,7 @@ class Avo::Index::ResourceControlsComponent < Avo::ResourceComponent
65
67
  end
66
68
 
67
69
  def parent_resource
70
+ return @parent_resource if @parent_resource.present?
68
71
  return nil if @parent_model.blank?
69
72
 
70
73
  ::Avo::App.get_resource_by_model_name @parent_model.class
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Avo::Index::ResourceGridComponent < ViewComponent::Base
4
- def initialize(resources: nil, resource: nil, reflection: nil, parent_model: nil, parent_resource: @parent_resource)
4
+ def initialize(resources: nil, resource: nil, reflection: nil, parent_model: nil, parent_resource: nil)
5
5
  @resources = resources
6
6
  @resource = resource
7
7
  @reflection = reflection