Skip to content

Commit

Permalink
Merge pull request #221 from cassiosantana/refactor/219-books-resourc…
Browse files Browse the repository at this point in the history
…e-should-be-moved-to-the-admin-namespace

refactor/219-books-resource-should-be-moved-to-the-admin-namespace
  • Loading branch information
cassiosantana authored Sep 21, 2023
2 parents 7ecf24f + 12ea84d commit 9714ca4
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 129 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
2 changes: 1 addition & 1 deletion spec/features/book_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

before do
visit books_path
visit admin_books_path
end

context "when initializing the page" do
Expand Down
38 changes: 19 additions & 19 deletions spec/routing/books_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
# frozen_string_literal: true

require 'rails_helper'
require "rails_helper"

RSpec.describe BooksController, type: :routing do
describe 'routing' do
it 'routes to #index' do
expect(get: '/books').to route_to('books#index')
RSpec.describe Admin::BooksController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "admin/books").to route_to("admin/books#index")
end

it 'routes to #new' do
expect(get: '/books/new').to route_to('books#new')
it "routes to #new" do
expect(get: "admin/books/new").to route_to("admin/books#new")
end

it 'routes to #show' do
expect(get: '/books/1').to route_to('books#show', id: '1')
it "routes to #show" do
expect(get: "admin/books/1").to route_to("admin/books#show", id: "1")
end

it 'routes to #edit' do
expect(get: '/books/1/edit').to route_to('books#edit', id: '1')
it "routes to #edit" do
expect(get: "admin/books/1/edit").to route_to("admin/books#edit", id: "1")
end

it 'routes to #create' do
expect(post: '/books').to route_to('books#create')
it "routes to #create" do
expect(post: "admin/books").to route_to("admin/books#create")
end

it 'routes to #update via PUT' do
expect(put: '/books/1').to route_to('books#update', id: '1')
it "routes to #update via PUT" do
expect(put: "admin/books/1").to route_to("admin/books#update", id: "1")
end

it 'routes to #update via PATCH' do
expect(patch: '/books/1').to route_to('books#update', id: '1')
it "routes to #update via PATCH" do
expect(patch: "admin/books/1").to route_to("admin/books#update", id: "1")
end

it 'routes to #destroy' do
expect(delete: '/books/1').to route_to('books#destroy', id: '1')
it "routes to #destroy" do
expect(delete: "admin/books/1").to route_to("admin/books#destroy", id: "1")
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe "books/edit", type: :view do
RSpec.describe "admin/books/edit", type: :view do
let(:authors) { create_list(:author, 3) }
let(:not_associated_assemblies) { create_list(:assembly, 3) }
let(:associated_assemblies) { create_list(:assembly, 4) }
Expand All @@ -16,11 +16,11 @@
end

it "render the page title" do
expect_page_title("Editing book")
expect(rendered).to have_selector("h1", text: "Editing book")
end

it "renders the book form" do
form = "form[action='#{book_path(book)}'][method='post']"
form = "form[action='#{admin_book_path(book)}'][method='post']"
title = "input[type='text'][name='book[title]'][value='#{book.title}']"
datetime = "input[type='datetime-local'][name='book[published_at]']"
isbn = "input[type='text'][name='book[isbn]'][value='#{book.isbn}']"
Expand All @@ -44,14 +44,14 @@
datetime_input_value = Capybara.string(rendered).find(datetime).value
expect(DateTime.parse(datetime_input_value).utc.iso8601).to eq(book.published_at.utc.iso8601)

expect_submit_button("Update Book")
expect(rendered).to have_button("Update Book")
end

it "render the show book link" do
expect_link_to_show(book)
expect(rendered).to have_link("Show this book", href: admin_book_path(book))
end

it "render the back link" do
expect_link_back_to("books")
expect(rendered).to have_link("Back to books", href: admin_books_path)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe "books/index", type: :view do
RSpec.describe "admin/books/index", type: :view do
let(:author) { create(:author) }
let(:books) { create_list(:book, 5, author:) }

Expand All @@ -13,17 +13,17 @@
end

it "render the page title" do
expect_page_title("Books")
expect(rendered).to have_selector("h1", text: "Books")
end

it "render a list of books" do
books.each do |book|
expect(rendered).to have_text("Title: #{book.title}", normalize_ws: true)
expect(rendered).to have_link("Show this book", href: book_path(book))
expect(rendered).to have_link("Show this book", href: admin_book_path(book))
end
end

it "render a link to new book" do
expect_link_to_new("book")
expect(rendered).to have_link("New book", href: new_admin_book_path)
end
end
Loading

0 comments on commit 9714ca4

Please sign in to comment.