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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ coverage
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
/.ruby-gemset
/.idea/*
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-3.3.0
ruby-3.3.6
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ gem 'activerecord-import'
gem 'sprockets-rails'
gem 'slim-rails'

# Serializers
gem 'alba'

# Распаковка архивов
gem 'rubyzip', require: 'zip'

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
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ApplicationController < ActionController::Base
include Pagination
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern
end
6 changes: 6 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BooksController < ApplicationController
def index
books = paginate(Book.all)
render json: BookSerializer.new(books)
end
end
13 changes: 13 additions & 0 deletions app/controllers/concerns/pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# Методы фильтрации сортировки и паджинации
module Pagination
extend ActiveSupport::Concern
# DEFAULT_LIMIT = Settings.app.items_per_page || 100

def paginate(items)
curr_page = params[:page] || 1
limit = params[:per_page] || Settings.app.items_per_page || 100
items.page(curr_page).per(limit)
end
end
8 changes: 8 additions & 0 deletions app/serializers/book_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class BookSerializer
include Alba::Resource
root_key!

attributes :id, :title
end
7 changes: 4 additions & 3 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ default: &default
prepared_statements: false

postgre: &postgre
host: <%= ENV.fetch('POSTGRES_HOST', 'localhost') %>
host: <%= ENV.fetch('POSTGRES_HOST', '127.0.0.1') %>
port: <%= ENV.fetch('POSTGRES_PORT', '5432') %>
username: <%= ENV.fetch('POSTGRES_USER') { 'igorsimdyanov' } %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') { '' } %>
username: <%= ENV.fetch('POSTGRES_USER') { 'admin' } %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') { 'admin' } %>

development:
<<: *default
Expand All @@ -17,6 +17,7 @@ development:

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

production:
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# 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

get "books/(:page)" => 'books#index', page: /\d+/
# resources :books, only: :index - Я бы предпочел маршруты строить ближе к REST, а номер страницы передавать в query параметрах, например /books?page=2&per_page=100

# 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
7 changes: 7 additions & 0 deletions docs/layers.pulm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@startuml
Router -down->[use] Controller
Controller -down->[use] Command
Controller -down->[use] Model
Command -down->[use] Model
Controller -down->[use] Serializer
@enduml
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@
# config.include Devise::Test::ControllerHelpers, type: :request
config.include LoaderHelpers
config.include LoginHelpers, type: :feature
config.include RequestHelper, type: :request
end
24 changes: 24 additions & 0 deletions spec/requests/books_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

RSpec.describe BooksController, type: :request do
let(:language) { create(:language) }
let(:folder) { create(:folder) }
let(:per_page) { 25 }
let!(:books) { create_list(:book, 30, language: language, folder: folder) }

before do
allow(Settings.app).to receive(:items_per_page).and_return(per_page)
end

describe "GET /index" do
it "returns http success" do
get "/books/2"
expect(json[:books].size).to eq(5)
end

it "returns http success" do
get "/books"
expect(json[:books].size).to eq(25)
end
end
end
7 changes: 7 additions & 0 deletions spec/support/request_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module RequestHelper
def json
JSON.parse response.body, symbolize_names: true
end
end