diff --git a/app/controllers/admin/books_controller.rb b/app/controllers/admin/books_controller.rb new file mode 100644 index 00000000..8b943772 --- /dev/null +++ b/app/controllers/admin/books_controller.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +module Admin + class BooksController < ApplicationController + before_action :set_book, only: %i[show edit update destroy] + + def index + @q = Book.ransack(params[:q]) + @books = @q.result(distinct: true).includes(:author) + end + + def show; end + + def new + @book = Book.new + end + + def edit; end + + def create + @book = Book.new(book_params) + + if @book.save + redirect_to admin_book_url(@book), notice: "Book was successfully created." + else + render :new, status: :unprocessable_entity + end + end + + def update + if @book.update(book_params) + @book.assemblies.clear + + assembly_ids = Array(params[:book][:assembly_ids]).select(&:present?) + @book.assembly_ids = assembly_ids + + redirect_to admin_book_url(@book), notice: "Book was successfully updated." + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @book.destroy + + redirect_to admin_books_url, notice: "Book was successfully destroyed." + end + + def report + @book = Book.includes(assemblies: :parts).find(params[:id]) + end + + private + + def set_book + @book = Book.find(params[:id]) + end + + def book_params + params.require(:book).permit(:title, :published_at, :isbn, :author_id, assembly_ids: []) + end + end +end diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb deleted file mode 100644 index 09b36e1d..00000000 --- a/app/controllers/books_controller.rb +++ /dev/null @@ -1,61 +0,0 @@ -# frozen_string_literal: true - -class BooksController < ApplicationController - before_action :set_book, only: %i[show edit update destroy] - - def index - @q = Book.ransack(params[:q]) - @books = @q.result(distinct: true).includes(:author) - end - - def show; end - - def new - @book = Book.new - end - - def edit; end - - def create - @book = Book.new(book_params) - - if @book.save - redirect_to book_url(@book), notice: "Book was successfully created." - else - render :new, status: :unprocessable_entity - end - end - - def update - if @book.update(book_params) - @book.assemblies.clear - - assembly_ids = Array(params[:book][:assembly_ids]).select(&:present?) - @book.assembly_ids = assembly_ids - - redirect_to book_url(@book), notice: "Book was successfully updated." - else - render :edit, status: :unprocessable_entity - end - end - - def destroy - @book.destroy - - redirect_to books_url, notice: "Book was successfully destroyed." - end - - def report - @book = Book.includes(assemblies: :parts).find(params[:id]) - end - - private - - def set_book - @book = Book.find(params[:id]) - end - - def book_params - params.require(:book).permit(:title, :published_at, :isbn, :author_id, assembly_ids: []) - end -end diff --git a/app/views/books/_book.html.erb b/app/views/admin/books/_book.html.erb similarity index 100% rename from app/views/books/_book.html.erb rename to app/views/admin/books/_book.html.erb diff --git a/app/views/books/_form.html.erb b/app/views/admin/books/_form.html.erb similarity index 96% rename from app/views/books/_form.html.erb rename to app/views/admin/books/_form.html.erb index 7a2b791f..51bbc8b9 100644 --- a/app/views/books/_form.html.erb +++ b/app/views/admin/books/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: book) do |form| %> +<%= form_with(model: [:admin, book]) do |form| %> <% if book.errors.any? %>

<%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:

diff --git a/app/views/admin/books/edit.html.erb b/app/views/admin/books/edit.html.erb new file mode 100644 index 00000000..1a5eda89 --- /dev/null +++ b/app/views/admin/books/edit.html.erb @@ -0,0 +1,10 @@ +

Editing book

+ +<%= render "form", book: @book %> + +
+ +
+ <%= link_to "Show this book", admin_book_path(@book) %> | + <%= link_to "Back to books", admin_books_path %> +
diff --git a/app/views/books/index.html.erb b/app/views/admin/books/index.html.erb similarity index 71% rename from app/views/books/index.html.erb rename to app/views/admin/books/index.html.erb index 8a130c45..087599a0 100644 --- a/app/views/books/index.html.erb +++ b/app/views/admin/books/index.html.erb @@ -3,7 +3,7 @@

Books

- <%= search_form_for @q do |f| %> + <%= search_form_for [:admin, @q] do |f| %> <%= f.label :title_or_author_name_cont, "Search by title or Author name" %> <%= f.search_field :title_or_author_name_cont %> <%= f.submit %> @@ -17,9 +17,9 @@

Title: <%= book.title %> | - <%= link_to "Show this book", book %> + <%= link_to "Show this book", admin_book_path(book) %>

<% end %>
-<%= link_to "New book", new_book_path %> +<%= link_to "New book", new_admin_book_path %> diff --git a/app/views/books/new.html.erb b/app/views/admin/books/new.html.erb similarity index 58% rename from app/views/books/new.html.erb rename to app/views/admin/books/new.html.erb index 4da5b10e..0095b028 100644 --- a/app/views/books/new.html.erb +++ b/app/views/admin/books/new.html.erb @@ -5,5 +5,5 @@
- <%= link_to "Back to books", books_path %> + <%= link_to "Back to books", admin_books_path %>
diff --git a/app/views/books/report.html.erb b/app/views/admin/books/report.html.erb similarity index 97% rename from app/views/books/report.html.erb rename to app/views/admin/books/report.html.erb index 8386aba0..05f00036 100644 --- a/app/views/books/report.html.erb +++ b/app/views/admin/books/report.html.erb @@ -86,5 +86,5 @@
- <%= link_to "Back to books", books_path %> + <%= link_to "Back to books", admin_books_path %>
\ No newline at end of file diff --git a/app/views/admin/books/show.html.erb b/app/views/admin/books/show.html.erb new file mode 100644 index 00000000..0d0caa46 --- /dev/null +++ b/app/views/admin/books/show.html.erb @@ -0,0 +1,11 @@ +

<%= notice %>

+ +<%= render "admin/books/book", book: @book %> + +
+ <%= link_to "Edit this book", edit_admin_book_path(@book) %> | + <%= link_to "Back to books", admin_books_path %> | + <%= link_to "Show report", report_admin_book_path(@book) %> + + <%= button_to "Destroy this book", admin_book_path(@book), method: :delete %> +
diff --git a/app/views/books/edit.html.erb b/app/views/books/edit.html.erb deleted file mode 100644 index 841061f5..00000000 --- a/app/views/books/edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

Editing book

- -<%= render "form", book: @book %> - -
- -
- <%= link_to "Show this book", @book %> | - <%= link_to "Back to books", books_path %> -
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb deleted file mode 100644 index 81e3d32e..00000000 --- a/app/views/books/show.html.erb +++ /dev/null @@ -1,11 +0,0 @@ -

<%= notice %>

- -<%= render @book %> - -
- <%= link_to "Edit this book", edit_book_path(@book) %> | - <%= link_to "Back to books", books_path %> | - <%= link_to "Show report", report_book_path(@book) %> - - <%= button_to "Destroy this book", @book, method: :delete %> -
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 657921cd..01c11e0a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -18,7 +18,7 @@ <%= link_to "Accounts", accounts_path %> <%= link_to "Assemblies", assemblies_path %> <%= link_to "Parts", parts_path %> - <%= link_to "Books", books_path %> + <%= link_to "Books", admin_books_path %> <%= link_to "Suppliers", suppliers_path %>
diff --git a/config/routes.rb b/config/routes.rb index 28453b0f..8b0cb119 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,10 +2,16 @@ Rails.application.routes.draw do resources :accounts, :assemblies, :parts - resources :authors, :suppliers, :books do + resources :authors, :suppliers do get :report, on: :member end + namespace :admin do + resources :books do + get :report, on: :member + end + end + namespace :api, defaults: { format: :json } do resources :authors, :suppliers, :books, :accounts, :assemblies, :parts, only: %i[index show create update destroy] end