avo 3.0.8 → 3.1.1

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.

@@ -1,137 +1,122 @@
1
- <div data-controller="reload-belongs-to-field"
2
- data-action="turbo:before-stream-render@document->reload-belongs-to-field#beforeStreamRender"
3
- data-reload-belongs-to-field-polymorphic-value="<%= is_polymorphic? %>"
4
- data-reload-belongs-to-field-searchable-value="<%= @field.is_searchable? %>"
5
- data-reload-belongs-to-field-relation-name-value="<%= @field.id %>"
6
- data-reload-belongs-to-field-target-name-value="<%= form.object_name %>[<%= @field.id_input_foreign_key %>]"
7
- >
8
- <% if is_polymorphic? %>
9
- <%
10
- # Set the model keys so we can pass them over
11
- model_keys = @field.types.map do |type|
12
- resource = Avo.resource_manager.get_resource_by_model_class(type.to_s)
13
- [type.to_s, resource.model_key]
14
- end.to_h
15
- %>
16
- <div class="divide-y"
1
+ <% if is_polymorphic? %>
2
+ <div class="divide-y"
17
3
  data-controller="belongs-to-field"
18
4
  data-searchable="<%= @field.is_searchable? %>"
19
5
  data-association="<%= @field.id %>"
20
6
  data-association-class="<%= @field&.target_resource&.model_class || nil %>"
21
7
  >
22
- <%= field_wrapper **field_wrapper_args, help: @field.polymorphic_help || '' do %>
23
- <%= @form.select @field.type_input_foreign_key, @field.types.map { |type| [Avo.resource_manager.get_resource_by_model_class(type.to_s).name, type.to_s] },
24
- {
25
- value: @field.value,
26
- include_blank: @field.placeholder,
8
+ <%= field_wrapper **field_wrapper_args, help: @field.polymorphic_help || '' do %>
9
+ <%= @form.select @field.type_input_foreign_key, @field.types.map { |type| [Avo.resource_manager.get_resource_by_model_class(type.to_s).name, type.to_s] },
10
+ {
11
+ value: @field.value,
12
+ include_blank: @field.placeholder,
13
+ },
14
+ {
15
+ class: classes("w-full"),
16
+ data: {
17
+ **@field.get_html(:data, view: view, element: :input),
18
+ action: "change->belongs-to-field#changeType #{field_html_action}",
19
+ 'belongs-to-field-target': "select",
27
20
  },
28
- {
29
- class: classes("w-full"),
30
- data: {
31
- **@field.get_html(:data, view: view, element: :input),
32
- action: "change->belongs-to-field#changeType #{field_html_action}",
33
- 'belongs-to-field-target': "select",
34
- },
35
- disabled: disabled
36
- }
37
- %>
38
- <%
39
- # If the select field is disabled, no value will be sent. It's how HTML works.
40
- # Thus the extra hidden field to actually send the related id to the server.
41
- if disabled %>
42
- <%= @form.hidden_field @field.type_input_foreign_key %>
43
- <% end %>
21
+ disabled: disabled
22
+ }
23
+ %>
24
+ <%
25
+ # If the select field is disabled, no value will be sent. It's how HTML works.
26
+ # Thus the extra hidden field to actually send the related id to the server.
27
+ if disabled %>
28
+ <%= @form.hidden_field @field.type_input_foreign_key %>
44
29
  <% end %>
45
- <% @field.types.each do |type| %>
46
- <div class="hidden"
47
- data-belongs-to-field-target="type"
48
- data-type="<%= type %>"
49
- >
50
- <%= field_wrapper **field_wrapper_args, label: Avo.resource_manager.get_resource_by_model_class(type.to_s).name do %>
51
- <% if @field.is_searchable? %>
52
- <%= render Avo::Pro::SearchableAssociations::AutocompleteComponent.new form: @form,
53
- disabled: disabled,
54
- field: @field,
55
- foreign_key: @field.id_input_foreign_key,
56
- model_key: model_keys[type.to_s],
57
- polymorphic_record: polymorphic_record,
58
- resource: @resource,
59
- style: @field.get_html(:style, view: view, element: :input),
60
- type: type,
61
- classes: classes("w-full"),
62
- view: view
63
- %>
64
- <% else %>
65
- <%= @form.select @field.id_input_foreign_key,
66
- options_for_select(@field.values_for_type(type), @resource.present? && @resource.record.present? ? @resource.record[@field.id_input_foreign_key] : nil),
67
- {
68
- value: @resource.record[@field.id_input_foreign_key].to_s,
69
- include_blank: @field.placeholder,
70
- },
71
- {
72
- class: classes("w-full"),
73
- data: @field.get_html(:data, view: view, element: :input),
74
- disabled: disabled
75
- }
76
- %>
77
- <%
78
- # If the select field is disabled, no value will be sent. It's how HTML works.
79
- # Thus the extra hidden field to actually send the related id to the server.
80
- if disabled %>
81
- <%= @form.hidden_field @field.id_input_foreign_key %>
82
- <% end %>
30
+ <% end %>
31
+ <% @field.types.each do |type| %>
32
+ <div class="hidden"
33
+ data-belongs-to-field-target="type"
34
+ data-type="<%= type %>"
35
+ >
36
+ <%= field_wrapper **field_wrapper_args.merge!(data: reload_data), label: Avo.resource_manager.get_resource_by_model_class(type.to_s).name do %>
37
+ <% if @field.is_searchable? %>
38
+ <%= render Avo::Pro::SearchableAssociations::AutocompleteComponent.new form: @form,
39
+ disabled: disabled,
40
+ field: @field,
41
+ foreign_key: @field.id_input_foreign_key,
42
+ model_key: model_keys[type.to_s],
43
+ polymorphic_record: polymorphic_record,
44
+ resource: @resource,
45
+ style: @field.get_html(:style, view: view, element: :input),
46
+ type: type,
47
+ classes: classes("w-full"),
48
+ view: view
49
+ %>
50
+ <% else %>
51
+ <%= @form.select @field.id_input_foreign_key,
52
+ options_for_select(@field.values_for_type(type), @resource.present? && @resource.record.present? ? @resource.record[@field.id_input_foreign_key] : nil),
53
+ {
54
+ value: @resource.record[@field.id_input_foreign_key].to_s,
55
+ include_blank: @field.placeholder,
56
+ },
57
+ {
58
+ class: classes("w-full"),
59
+ data: @field.get_html(:data, view: view, element: :input),
60
+ disabled: disabled
61
+ }
62
+ %>
63
+ <%
64
+ # If the select field is disabled, no value will be sent. It's how HTML works.
65
+ # Thus the extra hidden field to actually send the related id to the server.
66
+ if disabled %>
67
+ <%= @form.hidden_field @field.id_input_foreign_key %>
83
68
  <% end %>
84
- <% if field.can_create? %>
85
- <% create_href = create_path(Avo.resource_manager.get_resource_by_model_class(type.to_s)) %>
86
- <% if !disabled && create_href.present? %>
87
- <%= link_to t("avo.create_new_item", item: type.to_s.downcase),
88
- create_href,
89
- class: "text-sm"
90
- %>
91
- <% end %>
69
+ <% end %>
70
+ <% if field.can_create? %>
71
+ <% create_href = create_path(Avo.resource_manager.get_resource_by_model_class(type.to_s)) %>
72
+ <% if !disabled && create_href.present? %>
73
+ <%= link_to t("avo.create_new_item", item: type.to_s.downcase),
74
+ create_href,
75
+ class: "text-sm"
76
+ %>
92
77
  <% end %>
93
78
  <% end %>
94
- </div>
95
- <% end %>
96
- </div>
97
- <% else %>
98
- <%= field_wrapper **field_wrapper_args do %>
99
- <% if @field.is_searchable? %>
100
- <%= render Avo::Pro::SearchableAssociations::AutocompleteComponent.new form: @form,
101
- field: @field,
102
- model_key: @field.target_resource&.model_key,
103
- foreign_key: @field.id_input_foreign_key,
104
- resource: @resource,
79
+ <% end %>
80
+ </div>
81
+ <% end %>
82
+ </div>
83
+ <% else %>
84
+ <%= field_wrapper **field_wrapper_args.merge!(data: reload_data) do %>
85
+ <% if @field.is_searchable? %>
86
+ <%= render Avo::Pro::SearchableAssociations::AutocompleteComponent.new form: @form,
87
+ field: @field,
88
+ model_key: @field.target_resource&.model_key,
89
+ foreign_key: @field.id_input_foreign_key,
90
+ resource: @resource,
91
+ disabled: disabled,
92
+ classes: classes("w-full"),
93
+ view: view,
94
+ style: @field.get_html(:style, view: view, element: :input)
95
+ %>
96
+ <% else %>
97
+ <%= @form.select @field.id_input_foreign_key, @field.options,
98
+ {
99
+ include_blank: @field.placeholder,
100
+ value: @field.value
101
+ },
102
+ {
103
+ class: classes("w-full"),
104
+ data: @field.get_html(:data, view: view, element: :input),
105
105
  disabled: disabled,
106
- classes: classes("w-full"),
107
- view: view,
108
106
  style: @field.get_html(:style, view: view, element: :input)
109
- %>
110
- <% else %>
111
- <%= @form.select @field.id_input_foreign_key, @field.options,
112
- {
113
- include_blank: @field.placeholder,
114
- value: @field.value
115
- },
116
- {
117
- class: classes("w-full"),
118
- data: @field.get_html(:data, view: view, element: :input),
119
- disabled: disabled,
120
- style: @field.get_html(:style, view: view, element: :input)
121
- }
122
- %>
123
- <%
124
- # If the select field is disabled, no value will be sent. It's how HTML works.
125
- # Thus the extra hidden field to actually send the related id to the server.
126
- if disabled %>
127
- <%= @form.hidden_field @field.id_input_foreign_key %>
128
- <% end %>
107
+ }
108
+ %>
109
+ <%
110
+ # If the select field is disabled, no value will be sent. It's how HTML works.
111
+ # Thus the extra hidden field to actually send the related id to the server.
112
+ if disabled %>
113
+ <%= @form.hidden_field @field.id_input_foreign_key %>
129
114
  <% end %>
130
- <% if field.can_create? %>
131
- <% if !disabled && create_path.present? %>
132
- <%= link_to t("avo.create_new_item", item: @field.name.downcase), create_path, class: "text-sm" %>
133
- <% end %>
115
+ <% end %>
116
+ <% if field.can_create? %>
117
+ <% if !disabled && create_path.present? %>
118
+ <%= link_to t("avo.create_new_item", item: @field.name.downcase), create_path, class: "text-sm" %>
134
119
  <% end %>
135
120
  <% end %>
136
121
  <% end %>
137
- </div>
122
+ <% end %>
@@ -73,4 +73,22 @@ class Avo::Fields::BelongsToField::EditComponent < Avo::Fields::EditComponent
73
73
  def visit_through_association?
74
74
  @field.target_resource.to_s == params[:via_resource_class].to_s
75
75
  end
76
+
77
+ def model_keys
78
+ @field.types.map do |type|
79
+ resource = Avo.resource_manager.get_resource_by_model_class(type.to_s)
80
+ [type.to_s, resource.model_key]
81
+ end.to_h
82
+ end
83
+
84
+ def reload_data
85
+ {
86
+ controller: "reload-belongs-to-field",
87
+ action: 'turbo:before-stream-render@document->reload-belongs-to-field#beforeStreamRender',
88
+ reload_belongs_to_field_polymorphic_value: is_polymorphic?,
89
+ reload_belongs_to_field_searchable_value: @field.is_searchable?,
90
+ reload_belongs_to_field_relation_name_value: @field.id,
91
+ reload_belongs_to_field_target_name_value: "#{form.object_name}[#{@field.id_input_foreign_key}]"
92
+ }
93
+ end
76
94
  end
data/lib/avo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Avo
2
- VERSION = "3.0.8" unless const_defined?(:VERSION)
2
+ VERSION = "3.1.1" unless const_defined?(:VERSION)
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avo
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.8
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Marin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-12-05 00:00:00.000000000 Z
12
+ date: 2023-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord