From 30329bb5ca09636eab43a065d3d9a17763cd62b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=80=D1=83=D1=81=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=95?= =?UTF-8?q?=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=93=D0=B5=D1=80=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2=D0=B8=D1=87?= Date: Tue, 11 Mar 2025 20:19:32 +0300 Subject: [PATCH] base books#index action --- .gitignore | 3 +++ Gemfile | 3 +++ Gemfile.lock | 4 ++++ app/controllers/books_controller.rb | 9 +++++++++ app/views/books/index.json.jbuilder | 1 + config/database.yml | 5 +++-- config/initializers/kaminari_config.rb | 3 +++ config/routes.rb | 2 ++ 8 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 app/controllers/books_controller.rb create mode 100644 app/views/books/index.json.jbuilder create mode 100644 config/initializers/kaminari_config.rb diff --git a/.gitignore b/.gitignore index 2f761fe..c9f05b0 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,6 @@ coverage config/settings.local.yml config/settings/*.local.yml config/environments/*.local.yml + +# IDE files +/.idea/* diff --git a/Gemfile b/Gemfile index fc76bca..334ef91 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,9 @@ gem 'activerecord-import' gem 'sprockets-rails' gem 'slim-rails' +# Serializers +gem 'jbuilder' + # Распаковка архивов gem 'rubyzip', require: 'zip' diff --git a/Gemfile.lock b/Gemfile.lock index 63e5241..164ad56 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -460,6 +463,7 @@ DEPENDENCIES factory_bot_rails fasterer ffaker + jbuilder parallel pg pry-byebug diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb new file mode 100644 index 0000000..6c1ffcf --- /dev/null +++ b/app/controllers/books_controller.rb @@ -0,0 +1,9 @@ +class BooksController < ApplicationController + def index + page = params[:page].to_i.positive? ? params[:page].to_i : 1 + + @books = Book.all.page(page) + + render formats: :json + end +end diff --git a/app/views/books/index.json.jbuilder b/app/views/books/index.json.jbuilder new file mode 100644 index 0000000..223ad63 --- /dev/null +++ b/app/views/books/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @books, :id, :title diff --git a/config/database.yml b/config/database.yml index 48f30ee..ec725ed 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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') { '' } %> development: <<: *default @@ -17,6 +17,7 @@ development: test: <<: *default + <<: *postgre database: library_test production: diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb new file mode 100644 index 0000000..0c0e1ac --- /dev/null +++ b/config/initializers/kaminari_config.rb @@ -0,0 +1,3 @@ +Kaminari.configure do |config| + config.default_per_page = Settings.app.items_per_page +end diff --git a/config/routes.rb b/config/routes.rb index ae608d2..e7e9b52 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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