Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginate has_many show view #736

Merged
merged 3 commits into from
Mar 3, 2017

Conversation

acrogenesis
Copy link
Contributor

This PR paginates the has_many show and takes into account that the show view can have several has_many fields.

Here you can see how the Attendee model is on page 1 and the User model is on page 2
screen shot 2017-01-19 at 10 21 19 am

PS. I blurred some personal information.

@BenMorganIO
Copy link
Collaborator

Woot! Awesome! Thanks @acrogenesis!

@BenMorganIO
Copy link
Collaborator

BenMorganIO commented Jan 19, 2017

Could you add a simple feature spec please? This way no one breaks it in the future.

@acrogenesis
Copy link
Contributor Author

@BenMorganIO I've added the test, I'm not entirely sure if it's the best way to make it. What do you think?

it "displays the customers orders paginated" do
customer = create(:customer)
orders = []
10.times do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor suggestion:

orders = 10.times.each_with_object([]) do |_index, arr|
  arr << create(:order, customer: customer)
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to do this 10 times. That could slow the tests down a bit. You could do 2 orders and set Kaminari.per_page = 2. Something like:

first_order, second_order = Array.new(2) { create :order }

visit admin_customer_path customer

expect(page).to have_content(first_order.id)
expect(page).to have_no_content(second_order.id)
click_on 'Next >'
expect(page).to have_content(second_order.id)
expect(page).to have_no_content(first_order.id)

@acrogenesis
Copy link
Contributor Author

Hey @BenMorganIO I've done some improvements on the tests, sorry for taking so long I've had a lot of work.

@nickcharlton
Copy link
Member

I like it! Going to squash and merge.

@nickcharlton nickcharlton merged commit 41a5e25 into thoughtbot:master Mar 3, 2017
@acrogenesis
Copy link
Contributor Author

Thanks!

@acrogenesis acrogenesis deleted the paginate_has_many branch March 3, 2017 18:09
@BenMorganIO
Copy link
Collaborator

No, thank you @acrogenesis!

fwolfst pushed a commit to fwolfst/administrate that referenced this pull request Mar 8, 2017
* paginate has_many show view

* test paginated has_many show view
* improve has_many#show tests
@pauldub pauldub mentioned this pull request Jul 21, 2017
@@ -34,7 +34,7 @@ def svg_tag(asset, svg_id, options = {})
end

def sanitized_order_params
params.permit(:search, :id, :order, :page, :per_page, :direction)
params.permit(:search, :id, :order, :page, :per_page, :direction, :orders)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, a bit late to the party but: was this change intended? Looks to me like it comes from "orders" being one of the examples, and it slipped through.

Copy link
Contributor Author

@acrogenesis acrogenesis Sep 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I think you are right. I'll run the tests without that and report back.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, don't spend too long on this though! Sorry, I was looking at something else and this confused me and thought of commenting here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails:

1) customer show page displays the customers orders paginated when the second page is specified displays the second page of results
     Failure/Error: params.permit(:search, :id, :order, :page, :per_page, :direction)
     
     ActionView::Template::Error:
       found unpermitted parameter: :orders

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for looking into that 👍

After your finding, I dug a bit deeper: it's a bug! orders is only relevant in that very specific example, where a "customer" has many "orders". The orders param tells the page number for the orders has_many list.

If I try to paginate through any other relationship, it breaks because the param has a different name.

No matter, I'll fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants