Skip to content

Commit

Permalink
Merge branch '#3509' of github.com:Shub3am/avo into #3509
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Dec 18, 2024
2 parents 63d5bdd + f60edb4 commit 9147e95
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
avo (3.15.3)
avo (3.15.4)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
4 changes: 2 additions & 2 deletions app/components/avo/field_wrapper_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
<% end %>
<% if on_edit? && @field.is_required? %> <span class="text-red-600 ml-1">*</span> <% 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 %>
<div class="self-center w-full <% unless full_width? || compact? || stacked? %> md:w-8/12 has-sidebar:w-full <% end %>">
<div class="self-center w-full break-words <% unless full_width? || compact? || stacked? %> md:w-8/12 has-sidebar:w-full <% end %>">
<% if on_show? %>
<% if render_dash? %>
Expand Down
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
2 changes: 1 addition & 1 deletion gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
2 changes: 1 addition & 1 deletion lib/avo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Avo
VERSION = "3.15.3" unless const_defined?(:VERSION)
VERSION = "3.15.4" unless const_defined?(:VERSION)
end
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
3 changes: 3 additions & 0 deletions spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@ def permissions
delete: true,
}
end

def some_token = @some_token ||= SecureRandom.hex(64)
end
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 9147e95

Please sign in to comment.