Commit b780f09
Changed files (3)
app
app/controllers/scim/v2/users_controller.rb
@@ -9,10 +9,8 @@ module Scim
end
def index
- @page, @page_size = page_params
- query = User.all
- @total = query.count
- @users = query.offset(@page - 1).limit(@page_size)
+ @users = paginate(User.all)
+
render formats: :scim, status: :ok
end
@@ -45,18 +43,8 @@ module Scim
params.permit(:schemas, :userName, :locale, :timezone)
end
- def page_params
- [
- page_param(:startIndex, default: 1, bottom: 1, top: 100),
- page_param(:count, default: 25, bottom: 0, top: 25)
- ]
- end
-
- def page_param(key, default:, bottom: 0, top: 250)
- actual = params.fetch(key, default).to_i
- return bottom if actual < bottom
- return top if actual > top
- actual
+ def paginate(query)
+ Paginate.new(query, params)
end
def repository(container = Spank::IOC)
app/models/paginate.rb
@@ -0,0 +1,32 @@
+class Paginate < SimpleDelegator
+ def initialize(query, params)
+ @query = query
+ @params = params
+ super(records)
+ end
+
+ def total_count
+ @total_count ||= @query.count
+ end
+
+ def page
+ @page ||= page_param(:startIndex, default: 1, bottom: 1, top: 100)
+ end
+
+ def page_size
+ @page_size ||= page_param(:count, default: 25, bottom: 0, top: 25)
+ end
+
+ def records
+ @records ||= @query.offset(page - 1).limit(page_size)
+ end
+
+ private
+
+ def page_param(key, default:, bottom: 0, top: 250)
+ actual = @params.fetch(key, default).to_i
+ return bottom if actual < bottom
+ return top if actual > top
+ actual
+ end
+end
app/views/scim/v2/users/index.scim.jbuilder
@@ -1,9 +1,9 @@
# frozen_string_literal: true
json.schemas [Scim::Kit::V2::Messages::LIST_RESPONSE]
-json.totalResults @total
-json.startIndex @page
-json.itemsPerPage @page_size
+json.totalResults @users.total_count
+json.startIndex @users.page
+json.itemsPerPage @users.page_size
json.Resources do
json.array! @users do |user|
json.partial! user, as: :user