Skip to content

Commit

Permalink
enhancement: persistence setting (#3540)
Browse files Browse the repository at this point in the history
* enhancement: persistence setting

* fix tests
  • Loading branch information
Paul-Bob authored Dec 17, 2024
1 parent e46909c commit f35feeb
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 42 deletions.
4 changes: 2 additions & 2 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 2 additions & 10 deletions lib/avo/concerns/filters_session_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
16 changes: 12 additions & 4 deletions lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/generators/avo/templates/initializer/avo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions spec/dummy/config/initializers/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
14 changes: 2 additions & 12 deletions spec/system/avo/filters/persist_filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
11 changes: 5 additions & 6 deletions spec/system/avo/tabs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

0 comments on commit f35feeb

Please sign in to comment.