-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add sort_by
and direction
option for HasMany field
#810
Conversation
spec/lib/fields/has_many_spec.rb
Outdated
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 } |
There was a problem hiding this comment.
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]
spec/lib/fields/has_many_spec.rb
Outdated
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) |
There was a problem hiding this comment.
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]
spec/lib/fields/has_many_spec.rb
Outdated
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 } |
There was a problem hiding this comment.
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]
spec/lib/fields/has_many_spec.rb
Outdated
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) |
There was a problem hiding this comment.
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]
spec/lib/fields/has_many_spec.rb
Outdated
@@ -1,6 +1,7 @@ | |||
require "administrate/field/has_many" | |||
require "support/constant_helpers" | |||
require "support/mock_relation" | |||
require "rails_helper" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huh, was this required?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, OK :)
spec/lib/fields/has_many_spec.rb
Outdated
|
||
expect(field.resources).to eq([:a] * limit) | ||
expect(field.resources.length).to eq(limit) |
There was a problem hiding this comment.
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.
spec/lib/fields/has_many_spec.rb
Outdated
field = association.new(:customers, resources, :show) | ||
field = association.new(:orders, resources, :show) | ||
|
||
expect(field.resources.length).to eq(1) |
There was a problem hiding this comment.
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
@BenMorganIO Thanks for your feedback. Updated the spec commit and rebased on top of |
Not really sure what's up with CircleCI there, and I can't trigger a rebuild. |
|
||
expect(field.resources).to eq([:a] * limit) | ||
expect(field.resources.size).to eq(limit) |
There was a problem hiding this comment.
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!
Great, thank you! I'm going to merge. |
* 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
Hey,
As always, thanks for a great gem.
This PR adds a
sort_by
option to theHasMany
field, allowing the developer to control in what order the data is displayed. It usesAdministrate::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.)