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

Add sort_by and direction option for HasMany field #810

Merged
merged 3 commits into from
Apr 29, 2017
Merged

Add sort_by and direction option for HasMany field #810

merged 3 commits into from
Apr 29, 2017

Conversation

klaseskilson
Copy link
Contributor

@klaseskilson klaseskilson commented Apr 3, 2017

Hey,

As always, thanks for a great gem.

This PR adds a sort_by option to the HasMany field, allowing the developer to control in what order the data is displayed. It uses Administrate::Order in order to support future features (such as allowing the user to select what param to sort by).

Hit me with your feedback! 👍

(Note: This tackles the same problem as in #750, but with a different approach. Not sure which is preferable here.)

field = association.new(:orders, customer.orders, :show)

reversed_order = customer.orders.sort_by(&:address_line_two).map(&:id)
correct_order = customer.orders.sort { |a, b| b.address_line_two <=> a.address_line_two }

Choose a reason for hiding this comment

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

Line is too long. [97/80]

context "with `direction` option" do
it "returns the resources in correct order" do
customer = FactoryGirl.create(:customer, :with_orders)
association = Administrate::Field::HasMany.with_options(sort_by: :address_line_two, direction: :desc)

Choose a reason for hiding this comment

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

Line is too long. [109/80]

field = association.new(:orders, customer.orders, :show)

correct_order = customer.orders.sort_by(&:address_line_two).map(&:id)
reversed_order = customer.orders.sort { |a, b| b.address_line_two <=> a.address_line_two }

Choose a reason for hiding this comment

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

Line is too long. [98/80]

context "with `sort_by` option" do
it "returns the resources in correct order" do
customer = FactoryGirl.create(:customer, :with_orders)
association = Administrate::Field::HasMany.with_options(sort_by: :address_line_two)

Choose a reason for hiding this comment

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

Line is too long. [91/80]

@@ -1,6 +1,7 @@
require "administrate/field/has_many"
require "support/constant_helpers"
require "support/mock_relation"
require "rails_helper"
Copy link
Collaborator

Choose a reason for hiding this comment

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

huh, was this required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently for some reason when running only one file of the spec. Will remove 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BenMorganIO There you go. Good catch, thank you!

@@ -74,6 +74,10 @@ which are specified through the `.with_options` class method:
`:limit` - Set the number of resources to display in the show view. Default is
`5`.

`:sort_by` - What to sort the association by in the show view.

`:direction` - What direction the sort should be in, `:asc` (default) or `:desc`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not call it :order?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean direction -> order? I chose direction in order to keep things consistent with the naming in Administrate::Order.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, OK :)


expect(field.resources).to eq([:a] * limit)
expect(field.resources.length).to eq(limit)
Copy link
Collaborator

Choose a reason for hiding this comment

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

.size would probably be better here.

field = association.new(:customers, resources, :show)
field = association.new(:orders, resources, :show)

expect(field.resources.length).to eq(1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

expect(field.resources).to be_one

@klaseskilson
Copy link
Contributor Author

@BenMorganIO Thanks for your feedback. Updated the spec commit and rebased on top of master.

@klaseskilson
Copy link
Contributor Author

Not really sure what's up with CircleCI there, and I can't trigger a rebuild.

@BenMorganIO BenMorganIO self-requested a review April 12, 2017 16:38

expect(field.resources).to eq([:a] * limit)
expect(field.resources.size).to eq(limit)
Copy link
Member

Choose a reason for hiding this comment

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

These few lines read much nicer!

@nickcharlton
Copy link
Member

Great, thank you! I'm going to merge.

@nickcharlton nickcharlton merged commit 4ef7215 into thoughtbot:master Apr 29, 2017
@klaseskilson klaseskilson deleted the sort-option-for-has-many branch April 29, 2017 20:42
iarie pushed a commit to iarie/administrate that referenced this pull request Jun 17, 2017
* Add `sort_by` and `direction` options to HasMany field
* Add spec for `sort_by` and `direction` in HasMany field
* [Docs] Add instructions for HasMany sort_by and direction options
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