Skip to content

Commit

Permalink
add sortable attributes for sorting with ransack
Browse files Browse the repository at this point in the history
Now pages can be sorted using public_version_public_on field key with "sort" parameter
  • Loading branch information
gr8bit committed Sep 3, 2023
1 parent 18002db commit 23c2190
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ module JsonApi
class PagesController < JsonApi::BaseController
before_action :load_page_for_cache_key, only: :show

ALLOWED_PUBLIC_VERSION_RANSACK_ATTRIBUTES = %w[public_on].map { |a| "public_version_#{a}" }.freeze

def index
allowed = Alchemy::Page.ransackable_attributes

jsonapi_filter(page_scope, allowed) do |filtered_pages|
@pages = filtered_pages.result
if !@pages.all?(&:cache_page?)
render_pages_json(allowed) && return
render_pages_json(allowed | ALLOWED_PUBLIC_VERSION_RANSACK_ATTRIBUTES) && return
elsif stale?(last_modified: @pages.maximum(:published_at), etag: @pages.max_by(&:cache_key)&.cache_key)
render_pages_json(allowed)
render_pages_json(allowed | ALLOWED_PUBLIC_VERSION_RANSACK_ATTRIBUTES)
end
end

Expand Down

0 comments on commit 23c2190

Please sign in to comment.