Skip to content

Commit 23c2190

Browse files
committed
add sortable attributes for sorting with ransack
Now pages can be sorted using public_version_public_on field key with "sort" parameter
1 parent 18002db commit 23c2190

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

app/controllers/alchemy/json_api/pages_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ module JsonApi
55
class PagesController < JsonApi::BaseController
66
before_action :load_page_for_cache_key, only: :show
77

8+
ALLOWED_PUBLIC_VERSION_RANSACK_ATTRIBUTES = %w[public_on].map { |a| "public_version_#{a}" }.freeze
9+
810
def index
911
allowed = Alchemy::Page.ransackable_attributes
1012

1113
jsonapi_filter(page_scope, allowed) do |filtered_pages|
1214
@pages = filtered_pages.result
1315
if !@pages.all?(&:cache_page?)
14-
render_pages_json(allowed) && return
16+
render_pages_json(allowed | ALLOWED_PUBLIC_VERSION_RANSACK_ATTRIBUTES) && return
1517
elsif stale?(last_modified: @pages.maximum(:published_at), etag: @pages.max_by(&:cache_key)&.cache_key)
16-
render_pages_json(allowed)
18+
render_pages_json(allowed | ALLOWED_PUBLIC_VERSION_RANSACK_ATTRIBUTES)
1719
end
1820
end
1921

0 commit comments

Comments
 (0)