Skip to content

Commit

Permalink
Merge branch 'main' into refactor/pass-parent-to-has-many-scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob authored Sep 27, 2024
2 parents c1a0af9 + 595812c commit bdf0762
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 10 additions & 2 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,15 @@ def cast_nullable(params)
def set_index_params
@index_params = {}

# projects.has_many.users
if @related_resource.present?
key = "#{@record.to_global_id}.has_many.#{@resource.class.to_s.parameterize}"
session[key] = params[:page] || session[key]
page_from_session = session[key]
end

# Pagination
@index_params[:page] = params[:page] || 1
@index_params[:page] = params[:page] || page_from_session || 1
@index_params[:per_page] = Avo.configuration.per_page

if cookies[:per_page].present?
Expand Down Expand Up @@ -608,7 +615,8 @@ def set_component_for(view, fallback_view: nil)
end

def apply_pagination
@pagy, @records = @resource.apply_pagination(index_params: @index_params, query: pagy_query)
# Set `trim_extra` to false in associations so the first page has the `page=1` param assigned
@pagy, @records = @resource.apply_pagination(index_params: @index_params, query: pagy_query, trim_extra: @related_resource.blank?)
end

def apply_sorting
Expand Down
4 changes: 3 additions & 1 deletion lib/avo/concerns/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def pagination_type
@pagination_type ||= ActiveSupport::StringInquirer.new(pagination_hash[:type].to_s)
end

def apply_pagination(index_params:, query:)
def apply_pagination(index_params:, query:, **args)
extra_pagy_params = {}

# Reset open filters when a user navigates to a new page
Expand All @@ -39,6 +39,8 @@ def apply_pagination(index_params:, query:)

send PAGINATION_METHOD[pagination_type.to_sym],
query,
**args,
page: index_params[:page],
items: index_params[:per_page], # Add per page in pagy < 9
limit: index_params[:per_page], # Add per page in pagy >= 9
link_extra: data_turbo_frame, # Add extra arguments in pagy 7.
Expand Down
22 changes: 22 additions & 0 deletions spec/system/avo/tabs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe "Tabs", type: :system do
let!(:user) { create :user, birthday: "10.02.1988" }
let!(:projects) { create_list :project, 9, users: [user] }

describe "doesn't display tabs content" do
context "on index" do
Expand Down Expand Up @@ -172,4 +173,25 @@
find('a[data-selected="false"][data-tabs-tab-name-param="Created at"]').click
expect(page).not_to have_text 'Invalid DateTime'
end

it "keeps the pagination on tab when back is used" do
visit avo.resources_user_path user

find('a[data-selected="false"][data-tabs-tab-name-param="Projects"]').click
expect(page).to have_css('a.current[role="link"][aria-disabled="true"][aria-current="page"]', text: "1")
expect(page).to have_text "Displaying items 1-8 of 9 in total"

find('a[data-turbo-frame="has_and_belongs_to_many_field_show_projects"]', text: "2").click
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"

find('a[aria-label="View project"]').click
wait_for_loaded

page.go_back
wait_for_loaded

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"
end
end

0 comments on commit bdf0762

Please sign in to comment.