avo 3.1.0 → 3.1.2

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
@@ -41,6 +41,7 @@ class Avo::Fields::HasOneField::ShowComponent < Avo::Fields::ShowComponent
41
41
  args = {
42
42
  via_relation: association_id,
43
43
  via_relation_class: @resource.model_class.to_s,
44
+ via_resource_class: @resource.class,
44
45
  via_record_id: @resource.record.to_param,
45
46
  via_association_type: :has_one
46
47
  }
@@ -1,39 +1,15 @@
1
1
  module Avo
2
2
  class DynamicRouter
3
- def self.eager_load(entity)
4
- paths = Avo::ENTITIES.fetch entity
5
-
6
- return unless paths.present?
7
-
8
- pathname = Rails.root.join(*paths)
9
- if pathname.directory?
10
- Rails.autoloaders.main.eager_load_dir(pathname.to_s)
11
- end
12
- end
13
-
14
3
  def self.routes
15
4
  Avo::Engine.routes.draw do
16
5
  scope "resources", as: "resources" do
17
- # Check if the user chose to manually register the resource files.
18
- # If so, eager_load the resources dir.
19
- if Avo.configuration.resources.nil?
20
- Avo::DynamicRouter.eager_load(:resources) unless Rails.application.config.eager_load
21
- end
22
-
23
- Avo::Resources::ResourceManager.fetch_resources
24
- .select do |resource|
25
- resource != :BaseResource
26
- end
27
- .select do |resource|
28
- resource.is_a? Class
29
- end
30
- .map do |resource|
31
- resources resource.route_key do
32
- member do
33
- get :preview
34
- end
6
+ Avo::Resources::ResourceManager.fetch_resources.map do |resource|
7
+ resources resource.route_key do
8
+ member do
9
+ get :preview
35
10
  end
36
11
  end
12
+ end
37
13
  end
38
14
  end
39
15
  end
@@ -8,7 +8,6 @@ module Avo
8
8
  class << self
9
9
  def build
10
10
  instance = new
11
- instance.init_resources
12
11
  instance.check_bad_resources
13
12
  instance
14
13
  end
@@ -35,37 +34,30 @@ module Avo
35
34
  # "FishResource",
36
35
  # ]
37
36
  def fetch_resources
38
- resources = if Avo.configuration.resources.nil?
39
- BaseResource.descendants
37
+ if Avo.configuration.resources.present?
38
+ load_configured_resources
40
39
  else
41
- Avo.configuration.resources
40
+ load_resources_namespace
42
41
  end
43
42
 
44
- resources.map do |resource|
45
- if resource.is_a?(Class)
46
- resource
47
- else
48
- resource.to_s.safe_constantize
49
- end
43
+ BaseResource.descendants
44
+ end
45
+
46
+ def load_resources_namespace
47
+ Rails.autoloaders.main.eager_load_namespace(Avo::Resources)
48
+ end
49
+
50
+ def load_configured_resources
51
+ raise 'Resources configuration must be an array' unless Avo.configuration.resources.is_a? Array
52
+
53
+ Avo.configuration.resources.each do |resource|
54
+ resource.to_s.safe_constantize
50
55
  end
51
56
  end
52
57
  end
53
58
 
54
59
  def initialize
55
- @resources = []
56
- end
57
-
58
- def init_resources
59
- self.resources = self.class.fetch_resources
60
- .reject do |resource|
61
- # Remove the BaseResource. We only need the descendants
62
- resource == Avo::BaseResource
63
- end
64
- .uniq do |klass|
65
- # On invalid resource configuration the resource classes get duplicated in `ObjectSpace`
66
- # We need to de-duplicate them
67
- klass.name
68
- end
60
+ @resources = self.class.fetch_resources
69
61
  end
70
62
 
71
63
  def check_bad_resources
data/lib/avo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Avo
2
- VERSION = "3.1.0" unless const_defined?(:VERSION)
2
+ VERSION = "3.1.2" unless const_defined?(:VERSION)
3
3
  end
data/lib/avo.rb CHANGED
@@ -18,16 +18,6 @@ module Avo
18
18
  IN_DEVELOPMENT = ENV["AVO_IN_DEVELOPMENT"] == "1"
19
19
  PACKED = !IN_DEVELOPMENT
20
20
  COOKIES_KEY = "avo"
21
- ENTITIES = {
22
- cards: ["app", "avo", "cards"],
23
- scopes: ["app", "avo", "scopes"],
24
- fields: ["app", "avo", "fields"],
25
- filters: ["app", "avo", "filters"],
26
- actions: ["app", "avo", "actions"],
27
- resources: ["app", "avo", "resources"],
28
- dashboards: ["app", "avo", "dashboards"],
29
- resource_tools: ["app", "avo", "resource_tools"]
30
- }
31
21
 
32
22
  class LicenseVerificationTemperedError < StandardError; end
33
23
 
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.1.0
4
+ version: 3.1.2
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-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord