Skip to content
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
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.3.0
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ gem 'activeadmin'
gem 'activeadmin_addons'
gem 'devise'

# serializer
gem 'alba'

group :development, :test do
gem 'bundler-audit'
gem 'capybara'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ GEM
uri (>= 0.13.1)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
alba (3.5.0)
ostruct (~> 0.6)
arbre (1.7.0)
activesupport (>= 3.0.0)
ruby2_keywords (>= 0.0.2)
Expand Down Expand Up @@ -448,6 +450,7 @@ DEPENDENCIES
activeadmin
activeadmin_addons
activerecord-import
alba
bundler-audit
capybara
config
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern

private

def serialize(obj, with: nil)
serializer = with || begin
model = obj.try(:model) || obj.class
"#{model.name}Serializer".constantize
end
serializer.new(obj).serialize
end
end
17 changes: 17 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class BooksController < ApplicationController
def index
books = Book.order(:created_at).limit(per_page).offset(page * per_page)

render json: serialize(books)
end

private

def per_page
::Settings.app.items_per_page
end

def page
[ params[:page].to_i, 0 ].max
end
end
7 changes: 7 additions & 0 deletions app/serializers/application_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApplicationSerializer
include Alba::Serializer

def self.one(name, **options)
options[:resource] ||= proc { "#{_1.class.name}Serializer".constantize }
end
end
3 changes: 3 additions & 0 deletions app/serializers/book_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class BookSerializer < ApplicationSerializer
attributes :id, :title, :series, :serno, :libid, :size, :filename, :ext, :del, :insno, :published_at, :created_at, :updated_at
end
1 change: 1 addition & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ development:

test:
<<: *default
<<: *postgre
database: library_test

production:
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
# # end
# end

resources :books, only: :index

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get 'up' => 'rails/health#show', as: :rails_health_check

# Render dynamic PWA files from app/views/pwa/*
get 'service-worker' => 'rails/pwa#service_worker', as: :pwa_service_worker
get 'manifest' => 'rails/pwa#manifest', as: :pwa_manifest
Expand Down
41 changes: 41 additions & 0 deletions spec/controllers/books_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
RSpec.describe BooksController, type: :controller do
let(:per_page) { ::Settings.app.items_per_page }

describe 'GET #index' do
context 'when without page' do
before { get :index }

it 'responds with ok status' do
expect(response).to have_http_status :ok
end

it 'responds first 20 books' do
expect(JSON.parse(response.body).size).to eq(per_page)
end
end

context 'when with page' do
before { get :index, params: { page: 3 } }

it 'responds with ok status' do
expect(response).to have_http_status :ok
end

it 'returns other 20 books' do
expect(JSON.parse(response.body).size).to eq(per_page)
end
end

context 'when page too big' do
before { get :index, params: { page: (Book.count / per_page + per_page) } }

it 'responds with ok status' do
expect(response).to have_http_status :ok
end

it 'returns 0 books' do
expect(JSON.parse(response.body).size).to eq(0)
end
end
end
end