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

Sort by default_sort on association class. #750

Closed
wants to merge 2 commits into from

Conversation

lunks
Copy link

@lunks lunks commented Feb 7, 2017

This allows us to sort by these associations using a given attribute that uses name by default but can be overwritten by the static method default_sort on the associated class.

This is an initial implementation, feedback is more than welcome. I'd like to know if this is something that is desirable and/or if this approach is ok for the given problem.

Relates to #278.

@lunks lunks changed the title Sort by default_sort on association klass. Sort by default_sort on association class. Feb 7, 2017
@lunks lunks force-pushed the sort-by-association-attribute branch 2 times, most recently from 2625f24 to 72ae815 Compare February 7, 2017 19:49
@hudsonsferreira
Copy link

hudsonsferreira commented Mar 23, 2017

hi @Graysonwright and @nickcharlton could you pls take a look on this PR? thanks


ordered = order.apply(relation)

expect(relation).to have_received(:order).with('customers.updated_at ASC')

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [82/80]

end

context 'when `order` uses an association with a default_sort method' do
it 'orders by the attribute given in the default_sort method' do

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end

context 'when `order` uses an association with a default_sort method' do

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


ordered = order.apply(relation)

expect(relation).to have_received(:order).with('customers.name DESC')

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -51,6 +52,33 @@
expect(ordered).to eq(relation)
end
end

context 'when `order` uses an association with no default_sort method' do
it 'orders by `name` on the associated record by default' do

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -51,6 +52,33 @@
expect(ordered).to eq(relation)
end
end

context 'when `order` uses an association with no default_sort method' do

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


def order_name
if association.klass.respond_to? :default_sort
association.klass.default_sort

Choose a reason for hiding this comment

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

Use 2 (not 0) spaces for indentation.

end

def relation_with_order
association ? relation.includes(association.name).order("#{association.plural_name}.#{order_name} #{direction_for_sql}") : relation

Choose a reason for hiding this comment

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

Line is too long. [137/80]

This allows us to sort by these associations using a given attribute that uses name by default but can be overwritten by the static method default_sort on the associated class.
@lunks lunks force-pushed the sort-by-association-attribute branch from e91f43c to 5b57358 Compare April 7, 2017 19:13
@nickcharlton
Copy link
Member

Thanks for your PR! I'm going to close this, as I've merged #810 instead.

@lunks lunks deleted the sort-by-association-attribute branch May 6, 2017 04:37
@lunks
Copy link
Author

lunks commented May 6, 2017

Great to have the feature in, thanks!

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