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

Sortable fields with scope:name_of_scope #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jsonapi/scopes/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def apply_filter(params)
filtering_params = params.dig(:filter) || {}

filtering_params.each do |key, value|
value = value.to_s.split(',').reject(&:blank?) if value.include?(',')
value = value.to_s.split(',').reject(&:blank?) if value&.include?(',')

raise InvalidAttributeError, "#{key} is not valid as filter attribute." unless @filters.include?(key.to_sym)

Expand Down
15 changes: 13 additions & 2 deletions lib/jsonapi/scopes/sorts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,20 @@ def apply_sort(params = {}, options = { allowed: [], default: {} })
raise InvalidAttributeError, "#{field} is not valid as sort attribute." unless allowed_fields.include?(field)
end

order = ordered_fields.presence || default_order
res = self

self.order(order)
order_to_follow = ordered_fields.presence || default_order
order_to_follow.each do |single_order_q, direction|
if single_order_q.to_s.include? 'scope:'
scope_name = single_order_q.to_s.split(':').last

res = send(scope_name.to_sym, direction)
else
res = order("#{single_order_q}": direction)
end
end

res
end

private
Expand Down