avo 2.25.1.pre.1.pr1579 → 2.26.1.pr1584.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae32fd05a8d35cbd8d5253344b8ebd26a96d98414430020ffa0e1ea80eb4b761
4
- data.tar.gz: b5a36ad70462507937a96bf563842b986f086c72fcd8b489fbeafb5779228ed9
3
+ metadata.gz: 7a5ed608bd7bb2383b9d541e6628cd09ae0f59c074fc01a809c48ad9c8286fb5
4
+ data.tar.gz: 2bea6d2082fb7d5c991ea7a948c3530566752d9d386987b15da6c718339b0ed8
5
5
  SHA512:
6
- metadata.gz: a3e978151ea7f78ce700368e91bfc5bb0a181c12532a693b998a61c0fc93416fb679c59c2bec892caf5b2f356fa7d431c2c6ff1f540435438a5576b04dd4958b
7
- data.tar.gz: 801644a98e243eea9ae43728cf4e321baddc19f2703700ed83b6f701be1e2a89bcd086c1b02381dca52fc161650e70c61770c80dd0a3575f93170ace07b8c9e6
6
+ metadata.gz: 7216406691593c3219f9a8977ff4c9fdc4520afac0280dd006e28e1b7ad7bf018813434cff06f91f1eaccb96f54e98a29f2932e7e6f892e8e2400d19cfce152e
7
+ data.tar.gz: ff1c08ce8d21d27ad23421298f03fc35cb16f3cfefb21c04fc82a6842c2f2b90083cfb185ae5b40b918c415201bf58dc59f0c5d28af5f61c9f49be54544b5162
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- avo (2.25.1.pre.1.pr1579)
4
+ avo (2.26.1.pr1584.pre.1)
5
5
  actionview (>= 6.0)
6
6
  active_link_to
7
7
  activerecord (>= 6.0)
@@ -23,9 +23,9 @@ class Avo::BaseComponent < ViewComponent::Base
23
23
 
24
24
  model_klass = ::Avo::BaseResource.valid_model_class model_class_name
25
25
 
26
- resource = ::Avo::App.get_resource_by_model_name model_klass if resource.blank?
26
+ model = model_klass.find params[:via_resource_id]
27
27
 
28
- model = resource.find_record params[:via_resource_id], query: model_klass
28
+ resource = ::Avo::App.get_resource_by_model_name model_klass if resource.blank?
29
29
 
30
30
  resource.dup.hydrate model: model
31
31
  end
@@ -44,7 +44,7 @@ class Avo::Fields::BelongsToField::EditComponent < Avo::Fields::EditComponent
44
44
 
45
45
  return @polymorphic_record if @polymorphic_record.present?
46
46
 
47
- @polymorphic_record = @resource.find_record polymorphic_id, query: polymorphic_class.safe_constantize
47
+ @polymorphic_record = polymorphic_class.safe_constantize.find polymorphic_id
48
48
 
49
49
  @polymorphic_record
50
50
  end
@@ -6,7 +6,7 @@ class Avo::Fields::BelongsToField::ShowComponent < Avo::Fields::ShowComponent
6
6
  model: @field.value,
7
7
  resource: @field.target_resource,
8
8
  via_resource_class: @resource.class.to_s,
9
- via_resource_id: @resource.model.to_param
9
+ via_resource_id: @resource.model.id
10
10
  )
11
11
  end
12
12
  end
@@ -21,11 +21,11 @@ class Avo::Fields::HasOneField::ShowComponent < Avo::Fields::ShowComponent
21
21
  end
22
22
 
23
23
  def attach_path
24
- helpers.avo.resources_associations_new_path(@resource.singular_model_key, @resource.model.to_param, @field.id)
24
+ helpers.avo.resources_associations_new_path(@resource.singular_model_key, @resource.model.id, @field.id)
25
25
  end
26
26
 
27
27
  def can_see_the_create_button?
28
- create = "create_#{@field.id}?"
28
+ create = "create_#{@field.id.to_s}?"
29
29
 
30
30
  authorization_service = @resource.authorization
31
31
 
@@ -39,7 +39,7 @@ class Avo::Fields::HasOneField::ShowComponent < Avo::Fields::ShowComponent
39
39
  args = {
40
40
  via_relation: @resource.singular_model_key,
41
41
  via_relation_class: @resource.model_class.to_s,
42
- via_resource_id: @resource.model.to_param
42
+ via_resource_id: @resource.model.id
43
43
  }
44
44
  helpers.new_resource_path(resource: @field.target_resource, **args)
45
45
  end
@@ -22,7 +22,7 @@ class Avo::Fields::IndexComponent < Avo::BaseComponent
22
22
  if @parent_model.present?
23
23
  args = {
24
24
  via_resource_class: @parent_resource.class,
25
- via_resource_id: @parent_model.to_param
25
+ via_resource_id: @parent_model.id
26
26
  }
27
27
  end
28
28
 
@@ -33,7 +33,7 @@ class Avo::Index::GridItemComponent < Avo::BaseComponent
33
33
  if @parent_model.present?
34
34
  args = {
35
35
  via_resource_class: parent_resource.class.to_s,
36
- via_resource_id: @parent_model.to_param
36
+ via_resource_id: @parent_model.id
37
37
  }
38
38
  end
39
39
 
@@ -18,6 +18,6 @@ class Avo::Index::Ordering::ButtonComponent < Avo::Index::Ordering::BaseComponen
18
18
  end
19
19
 
20
20
  def order_path(args)
21
- Avo::App.view_context.avo.reorder_order_path(resource.route_key, resource.model.to_param, **args)
21
+ Avo::App.view_context.avo.reorder_order_path(resource.route_key, resource.model.id, **args)
22
22
  end
23
23
  end
@@ -37,7 +37,7 @@ class Avo::Index::ResourceControlsComponent < Avo::ResourceComponent
37
37
  if @parent_model.present?
38
38
  args = {
39
39
  via_resource_class: parent_resource.class.to_s,
40
- via_resource_id: @parent_model.to_param
40
+ via_resource_id: @parent_model.id
41
41
  }
42
42
  end
43
43
 
@@ -51,7 +51,7 @@ class Avo::Index::ResourceControlsComponent < Avo::ResourceComponent
51
51
  if @parent_model.present?
52
52
  args = {
53
53
  via_resource_class: parent_resource.class.to_s,
54
- via_resource_id: @parent_model.to_param
54
+ via_resource_id: @parent_model.id
55
55
  }
56
56
  end
57
57
 
@@ -26,7 +26,7 @@ class Avo::ResourceComponent < Avo::BaseComponent
26
26
  def detach_path
27
27
  return "/" if @reflection.blank?
28
28
 
29
- helpers.resource_detach_path(params[:resource_name], params[:id], @reflection.name.to_s, @resource.model.to_param)
29
+ helpers.resource_detach_path(params[:resource_name], params[:id], @reflection.name.to_s, @resource.model.id)
30
30
  end
31
31
 
32
32
  def can_see_the_edit_button?
@@ -74,7 +74,7 @@ class Avo::Views::ResourceIndexComponent < Avo::ResourceComponent
74
74
  if @reflection.present?
75
75
  args = {
76
76
  via_relation_class: reflection_model_class,
77
- via_resource_id: @parent_model.to_param
77
+ via_resource_id: @parent_model.id
78
78
  }
79
79
 
80
80
  if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection
@@ -98,9 +98,7 @@ class Avo::Views::ResourceIndexComponent < Avo::ResourceComponent
98
98
  end
99
99
 
100
100
  def attach_path
101
- current_path = CGI.unescape(request.env["PATH_INFO"]).split("/").select(&:present?)
102
-
103
- Avo::App.root_path(paths: [*current_path, "new"])
101
+ Avo::App.root_path(paths: [request.env["PATH_INFO"], "new"])
104
102
  end
105
103
 
106
104
  def singular_resource_name
@@ -144,10 +142,10 @@ class Avo::Views::ResourceIndexComponent < Avo::ResourceComponent
144
142
  return unless @reflection.present?
145
143
 
146
144
  {
147
- association: "has_many",
145
+ association: 'has_many',
148
146
  association_id: @reflection.name,
149
147
  class: reflection_model_class,
150
- id: @parent_model.to_param
148
+ id: @parent_model.id
151
149
  }
152
150
  end
153
151
  end
@@ -35,7 +35,7 @@ module Avo
35
35
  args[:models] = if @selected_query.present?
36
36
  @resource.model_class.find_by_sql decrypted_query
37
37
  else
38
- @resource.find_record resource_ids
38
+ @resource.class.find_scope.find resource_ids
39
39
  end
40
40
  end
41
41
 
@@ -128,7 +128,7 @@ module Avo
128
128
  end
129
129
 
130
130
  def set_model
131
- @model = @resource.find_record(params[:id], query: model_find_scope)
131
+ @model = model_find_scope.find params[:id]
132
132
  end
133
133
 
134
134
  def model_find_scope
@@ -141,10 +141,11 @@ module Avo
141
141
 
142
142
  def set_related_model
143
143
  association_name = BaseResource.valid_association_name(@model, params[:related_name])
144
+
144
145
  @related_model = if @field.is_a? Avo::Fields::HasOneField
145
146
  @model.send association_name
146
147
  else
147
- @related_resource.find_record params[:related_id], query: eager_load_files(@related_resource, @model.send(association_name))
148
+ eager_load_files(@related_resource, @model.send(association_name)).find params[:related_id]
148
149
  end
149
150
  end
150
151
 
@@ -21,7 +21,7 @@ module Avo
21
21
  def index
22
22
  @parent_resource = @resource.dup
23
23
  @resource = @related_resource
24
- @parent_model = @parent_resource.find_record(params[:id])
24
+ @parent_model = @parent_resource.class.find_scope.find(params[:id])
25
25
  @parent_resource.hydrate(model: @parent_model)
26
26
  association_name = BaseResource.valid_association_name(@parent_model, params[:related_name])
27
27
  @query = @related_authorization.apply_policy @parent_model.send(association_name)
@@ -113,7 +113,7 @@ module Avo
113
113
  end
114
114
 
115
115
  def set_attachment_model
116
- @attachment_model = @related_resource.find_record attachment_id
116
+ @attachment_model = @attachment_class.find attachment_id
117
117
  end
118
118
 
119
119
  def set_reflection_field
@@ -90,7 +90,7 @@ module Avo
90
90
  # If we're accessing this resource via another resource add the parent to the breadcrumbs.
91
91
  if params[:via_resource_class].present? && params[:via_resource_id].present?
92
92
  via_resource = Avo::App.get_resource(params[:via_resource_class]).dup
93
- via_model = via_resource.find_record params[:via_resource_id]
93
+ via_model = via_resource.class.find_scope.find params[:via_resource_id]
94
94
  via_resource.hydrate model: via_model
95
95
 
96
96
  add_breadcrumb via_resource.plural_name, resources_path(resource: via_resource)
@@ -113,7 +113,7 @@ module Avo
113
113
 
114
114
  if is_associated_record?
115
115
  via_resource = Avo::App.get_resource_by_model_name(params[:via_relation_class]).dup
116
- via_model = via_resource.find_record params[:via_resource_id]
116
+ via_model = via_resource.class.find_scope.find params[:via_resource_id]
117
117
  via_resource.hydrate model: via_model
118
118
 
119
119
  add_breadcrumb via_resource.plural_name, resources_path(resource: via_resource)
@@ -137,10 +137,7 @@ module Avo
137
137
  # Fills in the required infor for belongs_to and has_many
138
138
  # Get the foreign key and set it to the id we received in the params
139
139
  if @reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection) || @reflection.is_a?(ActiveRecord::Reflection::HasManyReflection)
140
- related_resource = Avo::App.get_resource_by_model_name params[:via_relation_class]
141
- related_record = related_resource.find_record params[:via_resource_id]
142
-
143
- @model.send("#{@reflection.foreign_key}=", related_record.id)
140
+ @model.send("#{@reflection.foreign_key}=", params[:via_resource_id])
144
141
  @model.save
145
142
  end
146
143
 
@@ -148,7 +145,7 @@ module Avo
148
145
  if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection
149
146
  # find the record
150
147
  via_resource = ::Avo::App.get_resource_by_model_name(params[:via_relation_class]).dup
151
- @related_record = via_resource.find_record params[:via_resource_id]
148
+ @related_record = via_resource.model_class.find params[:via_resource_id]
152
149
  association_name = BaseResource.valid_association_name(@model, params[:via_relation])
153
150
 
154
151
  @model.send(association_name) << @related_record
@@ -388,7 +385,7 @@ module Avo
388
385
  # If we're accessing this resource via another resource add the parent to the breadcrumbs.
389
386
  if params[:via_resource_class].present? && params[:via_resource_id].present?
390
387
  via_resource = Avo::App.get_resource(params[:via_resource_class]).dup
391
- via_model = via_resource.find_record params[:via_resource_id]
388
+ via_model = via_resource.class.find_scope.find params[:via_resource_id]
392
389
  via_resource.hydrate model: via_model
393
390
 
394
391
  add_breadcrumb via_resource.plural_name, resources_path(resource: @resource)
@@ -1,11 +1,6 @@
1
1
  <%= turbo_frame_tag 'attach_modal' do %>
2
- <%
3
- url = Avo::Services::URIService.parse(avo.root_url.to_s)
4
- .append_paths('resources', params[:resource_name], params[:id], params[:related_name])
5
- .to_s
6
- %>
7
2
  <%= form_with scope: 'fields',
8
- url: url,
3
+ url: "#{avo.root_path}resources/#{params[:resource_name]}/#{params[:id]}/#{params[:related_name]}/",
9
4
  local: true,
10
5
  data: {
11
6
  'turbo-frame': '_top'
@@ -1,6 +1,6 @@
1
- <style>
1
+ <%= content_tag(:style, nonce: content_security_policy_nonce) do %>
2
2
  :root {
3
3
  <%= Avo.configuration.branding.css_colors %>
4
4
  }
5
- </style>
5
+ <% end %>
6
6
  <%= favicon_link_tag Avo.configuration.branding.favicon %>
@@ -1,5 +1,4 @@
1
1
  <!DOCTYPE html>
2
- <!-- ✨ Built with Avo • https://www.avohq.io/ -->
3
2
  <html>
4
3
  <head>
5
4
  <meta name="viewport" content="width=device-width, initial-scale=1">
@@ -63,4 +62,3 @@
63
62
  <!-- License valid?: <%= Avo::App.license.valid ? "valid" : "invalid" %> -->
64
63
  </body>
65
64
  </html>
66
- <!-- ✨ Built with Avo • https://www.avohq.io/ -->
data/lib/avo/app.rb CHANGED
@@ -115,8 +115,45 @@ module Avo
115
115
  end
116
116
  end
117
117
 
118
+ # Fetches the resources available to the application.
119
+ # We have two ways of doing that.
120
+ #
121
+ # 1. Through eager loading.
122
+ # We automatically eager load the resources directory and fetch the descendants from the scanned files.
123
+ # This is the simple way to get started.
124
+ #
125
+ # 2. Manually, declared by the user.
126
+ # We have this option to load the resources because when they are loaded automatically through eager loading,
127
+ # those Resource classes and their methods may trigger loading other classes. And that may disrupt Rails booting process.
128
+ # Ex: AdminResource may use self.model_class = User. That will trigger Ruby to load the User class and itself load
129
+ # other classes in a chain reaction.
130
+ # The scenario that comes up most often is when Rails boots, the routes are being computed which eager loads the resource files.
131
+ # At that boot time some migration might have not been run yet, but Rails tries to access them through model associations,
132
+ # and they are not available.
133
+ #
134
+ # To enable this feature add a `resources` array config in your Avo initializer.
135
+ # config.resources = [
136
+ # "UserResource",
137
+ # "FishResource",
138
+ # ]
139
+ def fetch_resources
140
+ resources = if Avo.configuration.resources.nil?
141
+ BaseResource.descendants
142
+ else
143
+ Avo.configuration.resources
144
+ end
145
+
146
+ resources.map do |resource|
147
+ if resource.is_a?(Class)
148
+ resource
149
+ else
150
+ resource.to_s.safe_constantize
151
+ end
152
+ end
153
+ end
154
+
118
155
  def init_resources
119
- self.resources = BaseResource.descendants
156
+ self.resources = fetch_resources
120
157
  .select do |resource|
121
158
  # Remove the BaseResource. We only need the descendants
122
159
  resource != BaseResource
data/lib/avo/base_card.rb CHANGED
@@ -89,7 +89,6 @@ module Avo
89
89
  5 => " h-[45rem]",
90
90
  6 => " h-[54rem]"
91
91
  }
92
- # puts ["cols->", cols, classes_for_cols, classes_for_rows, classes_for_cols[cols.to_i]].inspect
93
92
 
94
93
  result += classes_for_cols[cols.to_i] if classes_for_cols[cols.to_i].present?
95
94
  result += classes_for_rows[rows.to_i] if classes_for_rows[rows.to_i].present?
@@ -98,9 +97,9 @@ module Avo
98
97
  end
99
98
 
100
99
  def type
101
- return :metric if self.class.superclass == ::Avo::Dashboards::MetricCard
102
- return :chartkick if self.class.superclass == ::Avo::Dashboards::ChartkickCard
103
- return :partial if self.class.superclass == ::Avo::Dashboards::PartialCard
100
+ return :metric if is_a?(::Avo::Dashboards::MetricCard)
101
+ return :chartkick if is_a?(::Avo::Dashboards::ChartkickCard)
102
+ return :partial if is_a?(::Avo::Dashboards::PartialCard)
104
103
  end
105
104
 
106
105
  def compute_result
@@ -42,9 +42,6 @@ module Avo
42
42
  class_attribute :unscoped_queries_on_index, default: false
43
43
  class_attribute :resolve_query_scope
44
44
  class_attribute :resolve_find_scope
45
- class_attribute :find_record_method, default: ->(model_class:, id:) {
46
- model_class.find id
47
- }
48
45
  class_attribute :ordering
49
46
  class_attribute :hide_from_global_search, default: false
50
47
  class_attribute :after_create_path, default: :show
@@ -382,11 +379,7 @@ module Avo
382
379
  # set the value to the actual record
383
380
  value = @params[:via_relation_class].safe_constantize.find(@params[:via_resource_id])
384
381
  elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
385
- resource = Avo::App.get_resource_by_model_name params[:via_relation_class]
386
- model = resource.find_record @params[:via_resource_id]
387
- id_param = reflection.options[:primary_key] || :id
388
-
389
- value = model.send(id_param)
382
+ value = @params[:via_resource_id]
390
383
  end
391
384
  end
392
385
 
@@ -495,11 +488,5 @@ module Avo
495
488
  def has_model_id?
496
489
  model.present? && model.id.present?
497
490
  end
498
-
499
- def find_record(id, query: nil)
500
- query ||= self.class.find_scope
501
-
502
- self.class.find_record_method.call(model_class: query, id: id)
503
- end
504
491
  end
505
492
  end
@@ -41,6 +41,7 @@ module Avo
41
41
  attr_accessor :authorization_client
42
42
  attr_accessor :field_wrapper_layout
43
43
  attr_accessor :sign_out_path_name
44
+ attr_accessor :resources
44
45
  attr_writer :branding
45
46
 
46
47
  def initialize
@@ -92,6 +93,7 @@ module Avo
92
93
  @resource_default_view = :show
93
94
  @authorization_client = :pundit
94
95
  @field_wrapper_layout = :inline
96
+ @resources = nil
95
97
  end
96
98
 
97
99
  def current_user_method(&block)
@@ -3,9 +3,13 @@ module Avo
3
3
  def self.routes
4
4
  Avo::Engine.routes.draw do
5
5
  scope "resources", as: "resources" do
6
- Avo::App.eager_load(:resources) unless Rails.application.config.eager_load
6
+ # Check if the user chose to manually register the resource files.
7
+ # If so, eager_load the resources dir.
8
+ if Avo.configuration.resources.nil?
9
+ Avo::App.eager_load(:resources) unless Rails.application.config.eager_load
10
+ end
7
11
 
8
- BaseResource.descendants
12
+ Avo::App.fetch_resources
9
13
  .select do |resource|
10
14
  resource != :BaseResource
11
15
  end
@@ -29,7 +29,6 @@ module Avo
29
29
  end
30
30
 
31
31
  def fill_field(model, key, value, params)
32
- puts ["!!!!!!!!->"].inspect
33
32
  if value.blank?
34
33
  related_model = nil
35
34
  else
@@ -3,13 +3,13 @@ module Avo
3
3
  class URIService
4
4
  class << self
5
5
  def parse(path)
6
- new path
6
+ self.new path
7
7
  end
8
8
  end
9
9
 
10
10
  attr_reader :uri
11
11
 
12
- def initialize(path = "")
12
+ def initialize(path = '')
13
13
  @uri = Addressable::URI.parse(path)
14
14
  end
15
15
 
@@ -56,20 +56,19 @@ module Avo
56
56
  private
57
57
 
58
58
  def join_paths(paths)
59
- paths
60
- .map do |path|
61
- sanitize_path path
62
- end
63
- .join("/")
59
+ paths.map do |path|
60
+ sanitize_path path
61
+ end
62
+ .join("/")
64
63
  end
65
64
 
66
65
  # Removes the forward slash if it's present at the start of the path
67
66
  def sanitize_path(path)
68
- if path.to_s.starts_with? "/"
69
- path = path[1..]
67
+ if path.to_s.starts_with? '/'
68
+ path = path[1..-1]
70
69
  end
71
70
 
72
- ERB::Util.url_encode path
71
+ path
73
72
  end
74
73
  end
75
74
  end
data/lib/avo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Avo
2
- VERSION = "2.25.1.pre.1.pr1579" unless const_defined?(:VERSION)
2
+ VERSION = "2.26.1.pr1584.pre.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: 2.25.1.pre.1.pr1579
4
+ version: 2.26.1.pr1584.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Marin