From f35feeb5db2da46413c7139394bad1e3330d8068 Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Tue, 17 Dec 2024 19:36:09 +0200 Subject: [PATCH 1/3] enhancement: persistence setting (#3540) * enhancement: persistence setting * fix tests --- app/controllers/avo/associations_controller.rb | 4 ++-- lib/avo/concerns/filters_session_handler.rb | 12 ++---------- lib/avo/configuration.rb | 16 ++++++++++++---- lib/generators/avo/templates/initializer/avo.tt | 6 ------ spec/dummy/config/initializers/avo.rb | 2 -- spec/system/avo/filters/persist_filters_spec.rb | 14 ++------------ spec/system/avo/tabs_spec.rb | 11 +++++------ 7 files changed, 23 insertions(+), 42 deletions(-) diff --git a/app/controllers/avo/associations_controller.rb b/app/controllers/avo/associations_controller.rb index ce05ebddeb..3dd24b3175 100644 --- a/app/controllers/avo/associations_controller.rb +++ b/app/controllers/avo/associations_controller.rb @@ -304,7 +304,7 @@ def set_page_param # avo-resources-project.has_many.avo-resources-user.page page_key = "#{pagination_key}.page" - @index_params[:page] = if Avo.configuration.cache_associations_pagination + @index_params[:page] = if Avo.configuration.session_persistence_enabled? session[page_key] = params[:page] || session[page_key] || 1 else params[:page] || 1 @@ -315,7 +315,7 @@ def set_per_page_param # avo-resources-project.has_many.avo-resources-user.per_page per_page_key = "#{pagination_key}.per_page" - @index_params[:per_page] = if Avo.configuration.cache_associations_pagination + @index_params[:per_page] = if Avo.configuration.session_persistence_enabled? session[per_page_key] = params[:per_page] || session[per_page_key] || Avo.configuration.via_per_page else params[:per_page] || Avo.configuration.via_per_page diff --git a/lib/avo/concerns/filters_session_handler.rb b/lib/avo/concerns/filters_session_handler.rb index cd93f93ffe..abca060fca 100644 --- a/lib/avo/concerns/filters_session_handler.rb +++ b/lib/avo/concerns/filters_session_handler.rb @@ -2,13 +2,13 @@ module Avo module Concerns module FiltersSessionHandler def reset_filters - return unless cache_resource_filters? + return unless Avo.configuration.session_persistence_enabled? session.delete(filters_session_key) end def fetch_filters - return filters_from_params unless cache_resource_filters? + return filters_from_params unless Avo.configuration.session_persistence_enabled? (filters_from_params && save_filters_to_session) || filters_from_session end @@ -31,14 +31,6 @@ def filters_session_key action id ].map { |key| params[key] }.compact.join("/") end - - def cache_resource_filters? - Avo::ExecutionContext.new( - target: Avo.configuration.cache_resource_filters, - current_user: _current_user, - resource: @resource - ).handle - end end end end diff --git a/lib/avo/configuration.rb b/lib/avo/configuration.rb index c89123749b..6176103d8f 100644 --- a/lib/avo/configuration.rb +++ b/lib/avo/configuration.rb @@ -11,6 +11,7 @@ class Configuration attr_writer :pagination attr_writer :explicit_authorization attr_writer :exclude_from_status + attr_writer :persistence attr_accessor :timezone attr_accessor :per_page attr_accessor :per_page_steps @@ -26,8 +27,6 @@ class Configuration attr_accessor :full_width_container attr_accessor :full_width_index_view attr_accessor :cache_resources_on_index_view - attr_accessor :cache_resource_filters - attr_accessor :cache_associations_pagination attr_accessor :context attr_accessor :display_breadcrumbs attr_accessor :hide_layout_when_printing @@ -87,8 +86,9 @@ def initialize @full_width_container = false @full_width_index_view = false @cache_resources_on_index_view = Avo::PACKED - @cache_resource_filters = false - @cache_associations_pagination = false + @persistence = { + driver: nil + } @context = proc {} @initial_breadcrumbs = proc { add_breadcrumb I18n.t("avo.home").humanize, avo.root_path @@ -278,6 +278,14 @@ def default_locale def explicit_authorization Avo::ExecutionContext.new(target: @explicit_authorization).handle end + + def persistence + Avo::ExecutionContext.new(target: @persistence).handle + end + + def session_persistence_enabled? + persistence[:driver] == :session + end end def self.configuration diff --git a/lib/generators/avo/templates/initializer/avo.tt b/lib/generators/avo/templates/initializer/avo.tt index 7e5b56b113..dfe93766d4 100644 --- a/lib/generators/avo/templates/initializer/avo.tt +++ b/lib/generators/avo/templates/initializer/avo.tt @@ -78,12 +78,6 @@ Avo.configure do |config| # ActiveSupport::Cache.lookup_store(:solid_cache_store) # } # config.cache_resources_on_index_view = true - ## permanent enable or disable cache_resource_filters, default value is false - # config.cache_resource_filters = false - ## provide a lambda to enable or disable cache_resource_filters per user/resource. - # config.cache_resource_filters = -> { current_user.cache_resource_filters? } - ## Control persistence for the page and per-page options in association tables. - # config.cache_associations_pagination = false ## == Turbo options == # config.turbo = -> do diff --git a/spec/dummy/config/initializers/avo.rb b/spec/dummy/config/initializers/avo.rb index efb3b1a5fc..0823ef04bd 100644 --- a/spec/dummy/config/initializers/avo.rb +++ b/spec/dummy/config/initializers/avo.rb @@ -44,8 +44,6 @@ config.resource_default_view = :show config.search_debounce = 300 # config.field_wrapper_layout = :stacked - config.cache_resource_filters = false - config.cache_associations_pagination = false config.click_row_to_view_record = true config.turbo = { diff --git a/spec/system/avo/filters/persist_filters_spec.rb b/spec/system/avo/filters/persist_filters_spec.rb index b6bceb128c..d3a31a7e6e 100644 --- a/spec/system/avo/filters/persist_filters_spec.rb +++ b/spec/system/avo/filters/persist_filters_spec.rb @@ -18,12 +18,9 @@ context "cache resource filter enabled" do around(:each) do |it| - saved_value = Avo.configuration.cache_resource_filters - Avo.configuration.cache_resource_filters = lambda { - true - } + Avo.configuration.persistence = {driver: :session} it.run - Avo.configuration.cache_resource_filters = saved_value + Avo.configuration.persistence = {driver: nil} end it "persist filters by name" do @@ -53,13 +50,6 @@ end context "cache resource filter disabled" do - around(:each) do |it| - saved_value = Avo.configuration.cache_resource_filters - Avo.configuration.cache_resource_filters = false - it.run - Avo.configuration.cache_resource_filters = saved_value - end - it "doesn't persist filters by name" do visit url expect(page).to have_text("Displaying 2 item") diff --git a/spec/system/avo/tabs_spec.rb b/spec/system/avo/tabs_spec.rb index 0e37e5d518..a684e3f124 100644 --- a/spec/system/avo/tabs_spec.rb +++ b/spec/system/avo/tabs_spec.rb @@ -181,8 +181,7 @@ end it "keeps the pagination on tab when back is used" do - default_cache_associations_pagination_value = Avo.configuration.cache_associations_pagination - Avo.configuration.cache_associations_pagination = true + Avo.configuration.persistence = {driver: :session} visit avo.resources_user_path user @@ -203,12 +202,12 @@ expect(page).to have_css('a.current[role="link"][aria-disabled="true"][aria-current="page"]', text: "2") expect(page).to have_text "Displaying items 9-9 of 9 in total" - Avo.configuration.cache_associations_pagination = default_cache_associations_pagination_value + Avo.configuration.persistence = {driver: nil} end it "keeps the per_page on association when back is used" do - default_cache_associations_pagination_value = Avo.configuration.cache_associations_pagination - Avo.configuration.cache_associations_pagination = true + Avo.configuration.persistence = {driver: :session} + visit avo.resources_user_path user find('a[data-selected="false"][data-tabs-tab-name-param="Projects"]').click @@ -229,6 +228,6 @@ expect(page).to have_text "Displaying 9 items" expect(find("select#per_page.appearance-none").find("option[selected]").text).to eq("24") - Avo.configuration.cache_associations_pagination = default_cache_associations_pagination_value + Avo.configuration.persistence = {driver: nil} end end From 2b99dc1554eaca5eb5a44ff17600e90a9ee5cfa5 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Wed, 18 Dec 2024 12:15:39 +0200 Subject: [PATCH 2/3] Bumped avo to 3.15.4 --- Gemfile.lock | 2 +- gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock | 2 +- gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock | 2 +- gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock | 2 +- gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock | 2 +- gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock | 2 +- lib/avo/version.rb | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b218ad3154..c69e8d410e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - avo (3.15.3) + avo (3.15.4) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock b/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock index 69c223683b..ccfa5f726b 100644 --- a/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock +++ b/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock @@ -6,7 +6,7 @@ PATH PATH remote: .. specs: - avo (3.15.3) + avo (3.15.4) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock b/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock index 1d810c781f..0a31c3a953 100644 --- a/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock +++ b/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock @@ -6,7 +6,7 @@ PATH PATH remote: .. specs: - avo (3.15.3) + avo (3.15.4) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock b/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock index dc810900e4..efc554be8d 100644 --- a/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock +++ b/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock @@ -6,7 +6,7 @@ PATH PATH remote: .. specs: - avo (3.15.3) + avo (3.15.4) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock b/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock index 8a3b1e62f5..12cf1613ba 100644 --- a/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock +++ b/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock @@ -6,7 +6,7 @@ PATH PATH remote: .. specs: - avo (3.15.3) + avo (3.15.4) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock b/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock index 98cdddd9c2..f9792e836d 100644 --- a/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock +++ b/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock @@ -6,7 +6,7 @@ PATH PATH remote: .. specs: - avo (3.15.3) + avo (3.15.4) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/lib/avo/version.rb b/lib/avo/version.rb index 65780e9050..4e9e0e0caf 100644 --- a/lib/avo/version.rb +++ b/lib/avo/version.rb @@ -1,3 +1,3 @@ module Avo - VERSION = "3.15.3" unless const_defined?(:VERSION) + VERSION = "3.15.4" unless const_defined?(:VERSION) end From 10f1a54f7d0ae1a9d41174d8c7c07b1ecc9fc839 Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:18:24 +0200 Subject: [PATCH 3/3] fix: `break-words` on show content (#3542) --- app/components/avo/field_wrapper_component.html.erb | 4 ++-- spec/dummy/app/avo/resources/user.rb | 3 +++ spec/dummy/app/models/user.rb | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/components/avo/field_wrapper_component.html.erb b/app/components/avo/field_wrapper_component.html.erb index e0eedd610a..45c0a8ebde 100644 --- a/app/components/avo/field_wrapper_component.html.erb +++ b/app/components/avo/field_wrapper_component.html.erb @@ -17,14 +17,14 @@ <% end %> <% if on_edit? && @field.is_required? %> * <% end %> <% end %> - <%= content_tag :div, class: class_names("flex-1 flex flex-row md:min-h-inherit px-6", + <%= content_tag :div, class: class_names("flex-1 flex flex-row md:min-h-inherit px-6 overflow-x-auto", @field.get_html(:classes, view: @view, element: :content), { "pb-4": stacked?, "py-2": !compact?, "py-1": compact?, }), data: {slot: "value"} do %> -
+
<% if on_show? %> <% if render_dash? %> — diff --git a/spec/dummy/app/avo/resources/user.rb b/spec/dummy/app/avo/resources/user.rb index 81636ac29c..1e790dd3f0 100644 --- a/spec/dummy/app/avo/resources/user.rb +++ b/spec/dummy/app/avo/resources/user.rb @@ -120,6 +120,8 @@ def main_panel_fields required: true, only_on: [:index] + field :some_token, only_on: :show + field :is_writer, as: :text, sortable: -> { # Order by something else completely, just to make a test case that clearly and reliably does what we want. @@ -154,6 +156,7 @@ def test_sidebar def main_panel_sidebar sidebar do + field :some_token, only_on: :show test_field("Inside main_panel_sidebar") with_options only_on: :show do field :email, as: :gravatar, link_to_record: true, as_avatar: :circle diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index f57b859372..55595f3627 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -104,4 +104,6 @@ def permissions delete: true, } end + + def some_token = @some_token ||= SecureRandom.hex(64) end