avo 1.6.3.pre.3 → 1.6.4.pre.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/app/components/avo/edit/field_wrapper_component.html.erb +1 -1
- data/app/components/avo/edit/field_wrapper_component.rb +1 -6
- data/app/components/avo/fields/belongs_to_field/edit_component.html.erb +15 -65
- data/app/components/avo/fields/belongs_to_field/edit_component.rb +1 -1
- data/app/controllers/avo/base_controller.rb +0 -8
- data/db/factories.rb +0 -4
- data/lib/avo/base_resource.rb +19 -36
- data/lib/avo/fields/base_field.rb +3 -5
- data/lib/avo/fields/belongs_to_field.rb +11 -95
- data/lib/avo/fields/boolean_group_field.rb +1 -1
- data/lib/avo/fields/date_time_field.rb +1 -1
- data/lib/avo/fields/files_field.rb +1 -1
- data/lib/avo/fields/has_one_field.rb +1 -1
- data/lib/avo/fields/key_value_field.rb +1 -1
- data/lib/avo/fields/select_field.rb +17 -4
- data/lib/avo/fields_collector.rb +2 -0
- data/lib/avo/version.rb +1 -1
- data/public/avo-packs/css/application-797341b7.css.map +1 -1
- data/public/avo-packs/css/application-797341b7.css.map.br +0 -0
- data/public/avo-packs/css/application-797341b7.css.map.gz +0 -0
- data/public/avo-packs/js/application-b444cbf11135b4b23654.js +26 -0
- data/public/avo-packs/js/{application-947ed727440d5b5d73ab.js.LICENSE.txt → application-b444cbf11135b4b23654.js.LICENSE.txt} +0 -0
- data/public/avo-packs/js/application-b444cbf11135b4b23654.js.br +0 -0
- data/public/avo-packs/js/application-b444cbf11135b4b23654.js.gz +0 -0
- data/public/avo-packs/js/application-b444cbf11135b4b23654.js.map +1 -0
- data/public/avo-packs/js/application-b444cbf11135b4b23654.js.map.br +0 -0
- data/public/avo-packs/js/application-b444cbf11135b4b23654.js.map.gz +0 -0
- data/public/avo-packs/manifest.json +8 -8
- metadata +8 -9
- data/app/packs/js/controllers/fields/belongs_to_field_controller.js +0 -65
- data/public/avo-packs/js/application-947ed727440d5b5d73ab.js +0 -26
- data/public/avo-packs/js/application-947ed727440d5b5d73ab.js.br +0 -0
- data/public/avo-packs/js/application-947ed727440d5b5d73ab.js.gz +0 -0
- data/public/avo-packs/js/application-947ed727440d5b5d73ab.js.map +0 -1
- data/public/avo-packs/js/application-947ed727440d5b5d73ab.js.map.br +0 -0
- data/public/avo-packs/js/application-947ed727440d5b5d73ab.js.map.gz +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76444abfc4fae22802837e03aa92b0d16558983c096d0c865208fc783ca1e01a
|
4
|
+
data.tar.gz: 6c8e39471f030161b642511418122425356f8678bbfcda531104160e4836d6b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bba3763a17001a1a7706cb65b2c95ec73907293af9f446f8b0aa51e0ecd0d1e00f52953a49e9fc9450834584ec620ea77b86a68e1dade07ac5190cf5fade154
|
7
|
+
data.tar.gz: 6d0cf9a063eb31f6db21198efc0d9ac80654193901bbd2734074b267aa9a147561502e7f1a5585dfdba0de47680dfa5809c69078012518932ea5561043516f19
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
avo (1.6.
|
4
|
+
avo (1.6.4.pre.1)
|
5
5
|
active_link_to
|
6
6
|
addressable
|
7
7
|
breadcrumbs_on_rails
|
@@ -333,7 +333,7 @@ GEM
|
|
333
333
|
thread_safe (~> 0.1)
|
334
334
|
unicode-display_width (2.0.0)
|
335
335
|
unicode_utils (1.4.0)
|
336
|
-
view_component (2.
|
336
|
+
view_component (2.28.0)
|
337
337
|
activesupport (>= 5.0.0, < 7.0)
|
338
338
|
warden (1.2.9)
|
339
339
|
rack (>= 2.0.9)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
<div class="flex items-center py-0 leading-tight <%= @classes %> min-h-16" data-field-id="<%= @field.id %>" data-field-type="<%= @field.type %>">
|
2
2
|
<div class="h-16 flex self-start items-center text-blue-gray-800">
|
3
3
|
<div class="<% if @displayed_in_modal %> md:w-48 <% else %> md:w-64 <% end %> w-48 px-8 flex" data-slot="label">
|
4
|
-
<%= @form.label @field.id,
|
4
|
+
<%= @form.label @field.id, @field.name %> <% if @field.required %> <span class="text-red-600">*</span> <% end %>
|
5
5
|
</div>
|
6
6
|
</div>
|
7
7
|
<div class="flex-1 flex flex-row min-h-inherit">
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Avo::Edit::FieldWrapperComponent < ViewComponent::Base
|
4
|
-
def initialize(field: nil, dash_if_blank: true, full_width: false, displayed_in_modal: false, form: nil, resource: {},
|
4
|
+
def initialize(field: nil, dash_if_blank: true, full_width: false, displayed_in_modal: false, form: nil, resource: {}, **args)
|
5
5
|
@field = field
|
6
6
|
@dash_if_blank = dash_if_blank
|
7
7
|
@classes = args[:class].present? ? args[:class] : ""
|
@@ -11,14 +11,9 @@ class Avo::Edit::FieldWrapperComponent < ViewComponent::Base
|
|
11
11
|
@resource = resource
|
12
12
|
@model = resource.present? ? resource.model : nil
|
13
13
|
@full_width = full_width
|
14
|
-
@label = label
|
15
14
|
|
16
15
|
if (@index != 0) || @displayed_in_modal
|
17
16
|
@classes += " border-t"
|
18
17
|
end
|
19
18
|
end
|
20
|
-
|
21
|
-
def label
|
22
|
-
@label || @field.name
|
23
|
-
end
|
24
19
|
end
|
@@ -1,68 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
disabled: disabled,
|
11
|
-
'data-belongs-to-field-target': "select",
|
12
|
-
'data-action': 'change->belongs-to-field#changedType'
|
13
|
-
}
|
14
|
-
%>
|
15
|
-
<%
|
16
|
-
# If the select field is disabled, no value will be sent. It's how HTML works.
|
17
|
-
# Thus the extra hidden field to actually send the related id to the server.
|
18
|
-
if disabled
|
19
|
-
%>
|
20
|
-
<%= @form.hidden_field "#{@field.foreign_key}_type" %>
|
21
|
-
<% end %>
|
22
|
-
<% end %>
|
23
|
-
<% @field.types.each do |type| %>
|
24
|
-
<div class="hidden"
|
25
|
-
data-belongs-to-field-target="type"
|
26
|
-
data-type="<%= type %>"
|
27
|
-
>
|
28
|
-
<%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal, label: type.to_s do %>
|
29
|
-
<%= @form.select "#{@field.foreign_key}_id", @field.values_for_type(type),
|
30
|
-
{
|
31
|
-
include_blank: @field.placeholder,
|
32
|
-
},
|
33
|
-
{
|
34
|
-
class: helpers.input_classes('w-full', has_error: @field.model_errors.include?(@field.id)),
|
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
|
-
%>
|
43
|
-
<%= @form.hidden_field "#{@field.foreign_key}_id" %>
|
44
|
-
<% end %>
|
45
|
-
<% end %>
|
46
|
-
</div>
|
47
|
-
<% end %>
|
48
|
-
</div>
|
49
|
-
<% else %>
|
50
|
-
<%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
|
51
|
-
<%= @form.select @field.foreign_key, @field.options.map { |o| [o[:label], o[:value]] },
|
52
|
-
{
|
53
|
-
include_blank: @field.placeholder,
|
54
|
-
},
|
55
|
-
{
|
56
|
-
class: helpers.input_classes('w-full', has_error: @field.model_errors.include?(@field.id)),
|
57
|
-
disabled: disabled
|
58
|
-
}
|
59
|
-
%>
|
60
|
-
<%
|
61
|
-
# If the select field is disabled, no value will be sent. It's how HTML works.
|
62
|
-
# Thus the extra hidden field to actually send the related id to the server.
|
63
|
-
if disabled
|
1
|
+
<%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
|
2
|
+
<%= @form.select @field.foreign_key, @field.options.map { |o| [o[:label], o[:value]] },
|
3
|
+
{
|
4
|
+
include_blank: @field.placeholder,
|
5
|
+
},
|
6
|
+
{
|
7
|
+
class: helpers.input_classes('w-full', has_error: @field.model_errors.include?(@field.id)),
|
8
|
+
disabled: disabled
|
9
|
+
}
|
64
10
|
%>
|
65
|
-
|
66
|
-
|
11
|
+
<%
|
12
|
+
# If the select field is disabled, no value will be sent. It's how HTML works.
|
13
|
+
# Thus the extra hidden field to actually send the related id to the server.
|
14
|
+
if disabled
|
15
|
+
%>
|
16
|
+
<%= @form.hidden_field @field.foreign_key %>
|
67
17
|
<% end %>
|
68
18
|
<% end %>
|
@@ -3,7 +3,7 @@
|
|
3
3
|
class Avo::Fields::BelongsToField::EditComponent < Avo::Fields::EditComponent
|
4
4
|
def disabled
|
5
5
|
return true if @field.readonly
|
6
|
-
return true if @field.target_resource.
|
6
|
+
return true if @field.target_resource.model_class.name == params[:via_resource_class]
|
7
7
|
return true if @field.id.to_s == params[:via_relation].to_s
|
8
8
|
|
9
9
|
false
|
@@ -167,14 +167,6 @@ module Avo
|
|
167
167
|
|
168
168
|
def permitted_params
|
169
169
|
@resource.get_field_definitions.select(&:updatable).map(&:to_permitted_param)
|
170
|
-
# ppp = []
|
171
|
-
|
172
|
-
# @resource.get_field_definitions.select(&:updatable).each do |param|
|
173
|
-
# ppp.push(*param.to_permitted_param)
|
174
|
-
# end
|
175
|
-
# # abort ppp.inspect
|
176
|
-
# puts [':ppp ->', ppp].inspect
|
177
|
-
# ppp
|
178
170
|
end
|
179
171
|
|
180
172
|
def cast_nullable(params)
|
data/db/factories.rb
CHANGED
@@ -40,8 +40,4 @@ FactoryBot.define do
|
|
40
40
|
meta { [{foo: "bar", hey: "hi"}, {bar: "baz"}, {hoho: "hohoho"}].sample }
|
41
41
|
progress { Faker::Number.between(from: 0, to: 100) }
|
42
42
|
end
|
43
|
-
|
44
|
-
factory :comment do
|
45
|
-
body { Faker::Lorem.paragraphs(number: rand(4...10)).join("\n") }
|
46
|
-
end
|
47
43
|
end
|
data/lib/avo/base_resource.rb
CHANGED
@@ -87,31 +87,20 @@ module Avo
|
|
87
87
|
end
|
88
88
|
|
89
89
|
def get_fields(panel: nil, reflection: nil)
|
90
|
-
fields = get_field_definitions
|
91
|
-
.
|
92
|
-
|
93
|
-
end
|
90
|
+
fields = get_field_definitions.select do |field|
|
91
|
+
field.send("show_on_#{@view}")
|
92
|
+
end
|
94
93
|
.select do |field|
|
95
|
-
|
96
|
-
|
94
|
+
field.visible?
|
95
|
+
end
|
97
96
|
.select do |field|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
field.polymorphic_as.to_s == reflection.options[:as].to_s
|
103
|
-
next
|
104
|
-
end
|
105
|
-
# if !field.respond_to?(:polymorphic_as) &&
|
106
|
-
# field.respond_to?(:foreign_key) &&
|
107
|
-
# reflection.present? &&
|
108
|
-
# reflection.respond_to?(:foreign_key) &&
|
109
|
-
# reflection.foreign_key != field.foreign_key
|
110
|
-
# next
|
111
|
-
# end
|
112
|
-
|
97
|
+
unless field.respond_to?(:foreign_key) &&
|
98
|
+
reflection.present? &&
|
99
|
+
reflection.respond_to?(:foreign_key) &&
|
100
|
+
reflection.foreign_key == field.foreign_key
|
113
101
|
true
|
114
102
|
end
|
103
|
+
end
|
115
104
|
|
116
105
|
if panel.present?
|
117
106
|
fields = fields.select do |field|
|
@@ -261,17 +250,14 @@ module Avo
|
|
261
250
|
.reject do |field|
|
262
251
|
field.computed
|
263
252
|
end
|
264
|
-
.map
|
265
|
-
[field.database_id(model).to_s, field]
|
266
|
-
end
|
267
|
-
.to_h
|
253
|
+
.map { |field| [field.database_id(model).to_s, field] }.to_h
|
268
254
|
|
269
255
|
params.each do |key, value|
|
270
256
|
field = fields_by_database_id[key]
|
271
257
|
|
272
258
|
next unless field.present?
|
273
259
|
|
274
|
-
model = field.fill_field model, key, value
|
260
|
+
model = field.fill_field model, key, value
|
275
261
|
end
|
276
262
|
|
277
263
|
model
|
@@ -309,22 +295,19 @@ module Avo
|
|
309
295
|
|
310
296
|
# We will not overwrite any attributes that come pre-filled in the model.
|
311
297
|
def hydrate_model_with_default_values
|
312
|
-
default_values = get_fields
|
313
|
-
.
|
314
|
-
|
315
|
-
end
|
298
|
+
default_values = get_fields.select do |field|
|
299
|
+
!field.computed
|
300
|
+
end
|
316
301
|
.map do |field|
|
317
302
|
id = field.id
|
318
303
|
value = field.value
|
319
304
|
|
320
|
-
if field.
|
305
|
+
if field.respond_to? :foreign_key
|
321
306
|
id = field.foreign_key.to_sym
|
322
307
|
|
323
308
|
reflection = @model._reflections[@params[:via_relation]]
|
324
309
|
|
325
|
-
if
|
326
|
-
value = @params["via_relation_class"].safe_constantize.find(@params[:via_resource_id])
|
327
|
-
elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
|
310
|
+
if reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
|
328
311
|
value = @params[:via_resource_id]
|
329
312
|
end
|
330
313
|
end
|
@@ -333,8 +316,8 @@ module Avo
|
|
333
316
|
end
|
334
317
|
.to_h
|
335
318
|
.select do |id, value|
|
336
|
-
|
337
|
-
|
319
|
+
value.present?
|
320
|
+
end
|
338
321
|
|
339
322
|
default_values.each do |id, value|
|
340
323
|
if @model.send(id).nil?
|
@@ -91,11 +91,9 @@ module Avo
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
|
-
def value
|
95
|
-
property ||= id
|
96
|
-
|
94
|
+
def value
|
97
95
|
# Get model value
|
98
|
-
final_value = @model.send(
|
96
|
+
final_value = @model.send(id) if (model_or_class(@model) == "model") && @model.respond_to?(id)
|
99
97
|
|
100
98
|
if (@view === :new) || @action.present?
|
101
99
|
final_value = if default.present? && default.respond_to?(:call)
|
@@ -116,7 +114,7 @@ module Avo
|
|
116
114
|
final_value
|
117
115
|
end
|
118
116
|
|
119
|
-
def fill_field(model, key, value
|
117
|
+
def fill_field(model, key, value)
|
120
118
|
return model unless model.methods.include? key.to_sym
|
121
119
|
|
122
120
|
model.send("#{key}=", value)
|
@@ -2,9 +2,7 @@ module Avo
|
|
2
2
|
module Fields
|
3
3
|
class BelongsToField < BaseField
|
4
4
|
attr_reader :searchable
|
5
|
-
attr_reader :polymorphic_as
|
6
5
|
attr_reader :relation_method
|
7
|
-
attr_reader :types
|
8
6
|
|
9
7
|
def initialize(id, **args, &block)
|
10
8
|
args[:placeholder] ||= I18n.t("avo.choose_an_option")
|
@@ -12,15 +10,9 @@ module Avo
|
|
12
10
|
super(id, **args, &block)
|
13
11
|
|
14
12
|
@searchable = args[:searchable] == true
|
15
|
-
@polymorphic_as = args[:polymorphic_as]
|
16
|
-
@types = args[:types]
|
17
13
|
@relation_method = name.to_s.parameterize.underscore
|
18
14
|
end
|
19
15
|
|
20
|
-
def value
|
21
|
-
super(polymorphic_as)
|
22
|
-
end
|
23
|
-
|
24
16
|
def options
|
25
17
|
target_resource.model_class.all.map do |model|
|
26
18
|
{
|
@@ -30,32 +22,22 @@ module Avo
|
|
30
22
|
end
|
31
23
|
end
|
32
24
|
|
33
|
-
def values_for_type(type)
|
34
|
-
type.all.map do |model|
|
35
|
-
[model.send(App.get_resource_by_model_name(type).class.title), model.id]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
25
|
def database_value
|
40
26
|
target_resource.id
|
41
27
|
end
|
42
28
|
|
43
29
|
def foreign_key
|
44
|
-
return polymorphic_as if polymorphic_as.present?
|
45
|
-
|
46
30
|
if @model.present?
|
47
|
-
|
48
|
-
|
31
|
+
if @model.instance_of?(Class)
|
32
|
+
@model.reflections[@relation_method].foreign_key
|
33
|
+
else
|
34
|
+
@model.class.reflections[@relation_method].foreign_key
|
35
|
+
end
|
36
|
+
elsif @resource.present?
|
49
37
|
@resource.model_class.reflections[@relation_method].foreign_key
|
50
38
|
end
|
51
39
|
end
|
52
40
|
|
53
|
-
def reflection_for_key(key)
|
54
|
-
get_model_class(get_model).reflections[key.to_s]
|
55
|
-
rescue
|
56
|
-
nil
|
57
|
-
end
|
58
|
-
|
59
41
|
def relation_model_class
|
60
42
|
@resource.model_class
|
61
43
|
end
|
@@ -65,82 +47,16 @@ module Avo
|
|
65
47
|
end
|
66
48
|
|
67
49
|
def to_permitted_param
|
68
|
-
if polymorphic_as.present?
|
69
|
-
return ["#{polymorphic_as}_type".to_sym, "#{polymorphic_as}_id".to_sym]
|
70
|
-
end
|
71
|
-
|
72
50
|
foreign_key.to_sym
|
73
51
|
end
|
74
52
|
|
75
|
-
def fill_field(model, key, value, params)
|
76
|
-
return model unless model.methods.include? key.to_sym
|
77
|
-
|
78
|
-
if polymorphic_as.present?
|
79
|
-
model.send("#{polymorphic_as}_type=", params["#{polymorphic_as}_type"])
|
80
|
-
|
81
|
-
# If the type is blank, reset the id too.
|
82
|
-
if params["#{polymorphic_as}_type"].blank?
|
83
|
-
model.send("#{polymorphic_as}_id=", nil)
|
84
|
-
else
|
85
|
-
model.send("#{polymorphic_as}_id=", params["#{polymorphic_as}_id"])
|
86
|
-
end
|
87
|
-
else
|
88
|
-
model.send("#{key}=", value)
|
89
|
-
end
|
90
|
-
|
91
|
-
model
|
92
|
-
end
|
93
|
-
|
94
|
-
def database_id(model)
|
95
|
-
# If the field is a polymorphic value, return the polymorphic_type as key and pre-fill the _id in fill_field.
|
96
|
-
return "#{polymorphic_as}_type" if polymorphic_as.present?
|
97
|
-
|
98
|
-
foreign_key
|
99
|
-
rescue
|
100
|
-
id
|
101
|
-
end
|
102
|
-
|
103
53
|
def target_resource
|
104
|
-
if
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
return nil
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
reflection_key = polymorphic_as || id
|
113
|
-
|
114
|
-
if @model._reflections[reflection_key.to_s].klass.present?
|
115
|
-
App.get_resource_by_model_name @model._reflections[reflection_key.to_s].klass.to_s
|
116
|
-
elsif @model._reflections[reflection_key.to_s].options[:class_name].present?
|
117
|
-
App.get_resource_by_model_name @model._reflections[reflection_key.to_s].options[:class_name]
|
118
|
-
else
|
119
|
-
App.get_resource_by_name reflection_key.to_s
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
def get_model
|
124
|
-
return @model if @model.present?
|
125
|
-
|
126
|
-
@resource.model
|
127
|
-
rescue
|
128
|
-
nil
|
129
|
-
end
|
130
|
-
|
131
|
-
def name
|
132
|
-
return polymorphic_as.to_s.humanize if polymorphic_as.present? && view == :index
|
133
|
-
|
134
|
-
super
|
135
|
-
end
|
136
|
-
|
137
|
-
private
|
138
|
-
|
139
|
-
def get_model_class(model)
|
140
|
-
if model.instance_of?(Class)
|
141
|
-
model
|
54
|
+
if @model._reflections[id.to_s].klass.present?
|
55
|
+
App.get_resource_by_model_name @model._reflections[id.to_s].klass.to_s
|
56
|
+
elsif @model._reflections[id.to_s].options[:class_name].present?
|
57
|
+
App.get_resource_by_model_name @model._reflections[id.to_s].options[:class_name]
|
142
58
|
else
|
143
|
-
|
59
|
+
App.get_resource_by_name id.to_s
|
144
60
|
end
|
145
61
|
end
|
146
62
|
end
|