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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ coverage
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml

# IDE files
/.idea/*
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 'jbuilder'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не очень понравился выбор в виде jbuilder - он конечно работает из коробки и хорошо описан, но уж больно он медленный по сравнению с другими сериалайзерами. У нас он сейчас на проекте, очень страдаем от его скорости и везде где только можно переходим на alab (у которого Си под капотом)


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

Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ GEM
irb (1.14.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
jbuilder (2.13.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jquery-rails (4.6.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand Down Expand Up @@ -460,6 +463,7 @@ DEPENDENCIES
factory_bot_rails
fasterer
ffaker
jbuilder
parallel
pg
pry-byebug
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class BooksController < ApplicationController
def index
page = params[:page].to_i.positive? ? params[:page].to_i : 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Методу page(), который добавляет гем Kaminary можно сразу передать params[:page] - он обработает все случаи: отрицательные значение, nil, просто вандальные параметры. Т.е. можем избавиться вот от этой строки

page = params[:page].to_i.positive? ? params[:page].to_i : 1


@books = Book.all.page(page)

render formats: :json
end
end
1 change: 1 addition & 0 deletions app/views/books/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @books, :id, :title
5 changes: 3 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ default: &default
postgre: &postgre
host: <%= ENV.fetch('POSTGRES_HOST', 'localhost') %>
port: <%= ENV.fetch('POSTGRES_PORT', '5432') %>
username: <%= ENV.fetch('POSTGRES_USER') { 'igorsimdyanov' } %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') { '' } %>
username: <%= ENV.fetch('POSTGRES_USER') { 'postgres' } %>
password: <%= ENV.fetch('postgres') { '' } %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В config/database.yml помоему опечатка в следующей строке

  password: <%= ENV.fetch('postgres') { '' } %>

Тут лучше оставить значение переменной окружения POSTGRES_PASSWORD, если нужно установить пароль, то лучше это сделать в блоке метода fetch

  password: <%= ENV.fetch('POSTGRES_PASSWORD') { 'postgres' } %>


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/initializers/kaminari_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Kaminari.configure do |config|
config.default_per_page = Settings.app.items_per_page
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

devise_for :admin_users
root to: redirect('/admin/') # if Routing::Admin.present?

get "books/(:page)" => 'books#index'
# namespace :admin do
# # root to: 'home#index', as: :root

Expand Down