Skip to content

Commit

Permalink
Merge branch 'main' into refactor/improve-has-many-interations
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Sep 28, 2024
2 parents 84e3f38 + b0d8e48 commit 2e6cdcd
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
icon: 'heroicons/outline/link',
color: :primary,
style: :text,
data_turbo_frame: Avo::MODAL_FRAME_ID do %>
data: {
turbo_frame: Avo::MODAL_FRAME_ID,
target: :attach
} do %>
<%= t('avo.attach_item', item: @field.name.humanize(capitalize: false)) %>
<% end %>
<% end %>
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ def index
@association_field = find_association_field(resource: @parent_resource, association: params[:related_name])

if @association_field.present? && @association_field.scope.present?
@query = Avo::ExecutionContext.new(target: @association_field.scope, query: @query, parent: @parent_record).handle
@query = Avo::ExecutionContext.new(
target: @association_field.scope,
query: @query,
parent: @parent_record,
resource: @resource,
parent_resource: @parent_resource
).handle
end

super
Expand Down Expand Up @@ -58,6 +64,16 @@ def new
[@attachment_resource.new(record: record).record_title, record.to_param]
end
end

@url = Avo::Services::URIService.parse(avo.root_url.to_s)
.append_paths("resources", params[:resource_name], params[:id], params[:related_name])
.append_query(
{
view: @resource&.view&.to_s,
for_attribute: @field&.try(:for_attribute)
}.compact
)
.to_s
end

def create
Expand Down
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 @@ -609,7 +616,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
15 changes: 2 additions & 13 deletions app/views/avo/associations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<%
url = Avo::Services::URIService.parse(avo.root_url.to_s)
.append_paths('resources', params[:resource_name], params[:id], params[:related_name])
.append_query(
{
view: @resource&.view&.to_s,
for_attribute: @field&.try(:for_attribute)
}.compact
)
.to_s
%>
<%= form_with scope: 'fields',
url: url,
url: @url,
local: true,
data: {
'turbo-frame': '_top'
turbo_frame: :_top
} do |form| %>
<%= render Avo::ModalComponent.new do |c| %>
<% c.with_heading do %>
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
6 changes: 5 additions & 1 deletion spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ def second_tabs_group
field :comments,
as: :has_many,
# show_on: :edit,
scope: -> { query.starts_with parent.first_name[0].downcase },
scope: -> {
TestBuddy.hi("parent_resource:#{parent_resource.present?},resource:#{resource.present?}")

query.starts_with parent.first_name[0].downcase
},
description: "The comments listed in the attach modal all start with the name of the parent user."
field :comment, as: :has_one, name: "Main comment"
end
Expand Down
1 change: 1 addition & 0 deletions spec/features/avo/has_many_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
let!(:a_comment) { create :comment, user: user, body: "A comment that starts with the letter A" }

subject do
expect(TestBuddy).to receive(:hi).with("parent_resource:true,resource:true").at_least :once
visit "/admin/resources/users/#{user.id}/comments?turbo_frame=has_many_field_show_comments"
page
end
Expand Down
4 changes: 2 additions & 2 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@

click_on "Actions"
click_on "Do Nothing"
expect(page).to have_css('turbo-frame#modal_frame')
expect(page).to have_css("turbo-frame#modal_frame")
expect(page).to have_selector(modal = "[role='dialog']")
click_on "Run"
expect(page).not_to have_selector(modal)
Expand All @@ -214,7 +214,7 @@

click_on "Actions"
click_on "Close modal"
expect(page).to have_css('turbo-frame#modal_frame')
expect(page).to have_css("turbo-frame#modal_frame")
expect(page).to have_selector(modal = "[role='dialog']")
click_on "Run"
expect(page).not_to have_selector(modal)
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 2e6cdcd

Please sign in to comment.