Skip to content

Commit

Permalink
refactor(book): Move book to Admin namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
cassiosantana committed Sep 21, 2023
1 parent 7ecf24f commit e8bebdd
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 90 deletions.
63 changes: 63 additions & 0 deletions app/controllers/admin/books_controller.rb
Original file line number Diff line number Diff line change
@@ -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
61 changes: 0 additions & 61 deletions app/controllers/books_controller.rb

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with(model: book) do |form| %>
<%= form_with(model: [:admin, book]) do |form| %>
<% if book.errors.any? %>
<div style="color: red">
<h2><%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:</h2>
Expand Down
10 changes: 10 additions & 0 deletions app/views/admin/books/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing book</h1>

<%= render "form", book: @book %>

<br>

<div>
<%= link_to "Show this book", admin_book_path(@book) %> |
<%= link_to "Back to books", admin_books_path %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1>Books</h1>

<div>
<%= 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 %>
Expand All @@ -17,9 +17,9 @@
<p>
<strong>Title:</strong>
<%= book.title %> |
<%= link_to "Show this book", book %>
<%= link_to "Show this book", admin_book_path(book) %>
</p>
<% end %>
</div>

<%= link_to "New book", new_book_path %>
<%= link_to "New book", new_admin_book_path %>
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<br>

<div>
<%= link_to "Back to books", books_path %>
<%= link_to "Back to books", admin_books_path %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@
</section>

<div>
<%= link_to "Back to books", books_path %>
<%= link_to "Back to books", admin_books_path %>
</div>
11 changes: 11 additions & 0 deletions app/views/admin/books/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p style="color: green"><%= notice %></p>

<%= render "admin/books/book", book: @book %>

<div>
<%= 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 %>
</div>
10 changes: 0 additions & 10 deletions app/views/books/edit.html.erb

This file was deleted.

11 changes: 0 additions & 11 deletions app/views/books/show.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</nav>
<div>
Expand Down
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e8bebdd

Please sign in to comment.