From a9e4a47231557bbde3381e4f6b74b406233cb2dc Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Sun, 7 Jul 2024 23:08:31 +0300 Subject: [PATCH 1/3] performance: perf optimizations --- README.md | 2 +- lib/avo/base_resource.rb | 11 +++++++---- lib/avo/fields/base_field.rb | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 119fb3d030..bdad9c373d 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ ![](./public/avo-assets/logo-on-white.png) -**Avo - Ruby on Rails admin panel framework** +**Avo - Ruby on Rails Admin Panel Framework** Avo is a very custom Admin Panel Framework, Content Management System, and Internal Tool Builder for Ruby on Rails that saves engineers and teams **months of development time**. diff --git a/lib/avo/base_resource.rb b/lib/avo/base_resource.rb index 47dbf23f64..7885f409d9 100644 --- a/lib/avo/base_resource.rb +++ b/lib/avo/base_resource.rb @@ -150,11 +150,11 @@ def model_class(record_class: nil) # With uncountable models route key appends an _index suffix (Fish->fish_index) # Example: User->users, MediaItem->media_items, Fish->fish def model_key - model_class.model_name.plural + @model_key ||= model_class.model_name.plural end def class_name - to_s.demodulize + @class_name ||=to_s.demodulize end def route_key @@ -170,7 +170,7 @@ def translation_key end def name - name_from_translation_key(count: 1, default: class_name.underscore.humanize) + @name ||= name_from_translation_key(count: 1, default: class_name.underscore.humanize) end alias_method :singular_name, :name @@ -448,7 +448,6 @@ def authorization(user: nil) def file_hash content_to_be_hashed = "" - file_name = self.class.underscore_name.tr(" ", "_") resource_path = Rails.root.join("app", "avo", "resources", "#{file_name}.rb").to_s if File.file? resource_path content_to_be_hashed += File.read(resource_path) @@ -463,6 +462,10 @@ def file_hash Digest::MD5.hexdigest(content_to_be_hashed) end + def file_name + @file_name ||= self.class.underscore_name.tr(" ", "_") + end + def cache_hash(parent_record) result = [record, file_hash] diff --git a/lib/avo/fields/base_field.rb b/lib/avo/fields/base_field.rb index 675aaffa65..f52e457273 100644 --- a/lib/avo/fields/base_field.rb +++ b/lib/avo/fields/base_field.rb @@ -246,7 +246,7 @@ def record_errors end def type - self.class.name.demodulize.to_s.underscore.gsub("_field", "") + @type ||= self.class.name.demodulize.to_s.underscore.gsub("_field", "") end def custom? From 53935a14563f57aa27fc33b8f5d50b47aa20eff0 Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Sun, 7 Jul 2024 23:12:46 +0300 Subject: [PATCH 2/3] front-end optimiziations --- .../fields/common/files/view_type/grid_item_component.html.erb | 2 +- .../avo/index/grid_cover_empty_state_component.html.erb | 2 +- app/components/avo/index/grid_item_component.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/components/avo/fields/common/files/view_type/grid_item_component.html.erb b/app/components/avo/fields/common/files/view_type/grid_item_component.html.erb index b1e4ca5da2..f2c9136309 100644 --- a/app/components/avo/fields/common/files/view_type/grid_item_component.html.erb +++ b/app/components/avo/fields/common/files/view_type/grid_item_component.html.erb @@ -2,7 +2,7 @@ <% if file.present? %>
<% if file.representable? && is_image? %> - <%= image_tag helpers.main_app.url_for(file), class: "rounded-lg max-w-full self-start #{@extra_classes}" %> + <%= image_tag helpers.main_app.url_for(file), class: "rounded-lg max-w-full self-start #{@extra_classes}", loading: :lazy, width: file.metadata["width"], height: file.metadata["height"] %> <% elsif is_audio? %> <%= audio_tag(helpers.main_app.url_for(file), controls: true, preload: false, class: 'w-full') %> <% elsif is_video? %> diff --git a/app/components/avo/index/grid_cover_empty_state_component.html.erb b/app/components/avo/index/grid_cover_empty_state_component.html.erb index 938f0af9fb..bd7e527a74 100644 --- a/app/components/avo/index/grid_cover_empty_state_component.html.erb +++ b/app/components/avo/index/grid_cover_empty_state_component.html.erb @@ -1,3 +1,3 @@
- <%= image_tag Avo.configuration.branding.placeholder, class: 'relative transform -translate-x-1/2 -translate-y-1/2 h-20 text-gray-400 inset-auto top-1/2 left-1/2' %> + <%= image_tag Avo.configuration.branding.placeholder, class: "relative transform -translate-x-1/2 -translate-y-1/2 h-20 text-gray-400 inset-auto top-1/2 left-1/2", loading: :lazy %>
diff --git a/app/components/avo/index/grid_item_component.rb b/app/components/avo/index/grid_item_component.rb index 7fa42baa72..202b980ad5 100644 --- a/app/components/avo/index/grid_item_component.rb +++ b/app/components/avo/index/grid_item_component.rb @@ -48,7 +48,7 @@ def render_cover def link_to_cover classes = "absolute h-full w-full object-cover" - link_to image_tag(@card[:cover_url], class: classes), resource_view_path, class: classes, title: @card[:title] + link_to image_tag(@card[:cover_url], class: classes), resource_view_path, class: classes, title: @card[:title], loading: :lazy, width: "640", height: "480" end def render_title From 685d1ad4e8c7493cac6bc14d170c1b0e41164e7d Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Sun, 7 Jul 2024 23:44:04 +0300 Subject: [PATCH 3/3] lint --- lib/avo/base_resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/avo/base_resource.rb b/lib/avo/base_resource.rb index 7885f409d9..87df2cafc8 100644 --- a/lib/avo/base_resource.rb +++ b/lib/avo/base_resource.rb @@ -154,7 +154,7 @@ def model_key end def class_name - @class_name ||=to_s.demodulize + @class_name ||= to_s.demodulize end def route_key