-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from cassiosantana/refactor/219-books-resourc…
…e-should-be-moved-to-the-admin-namespace refactor/219-books-resource-should-be-moved-to-the-admin-namespace
- Loading branch information
Showing
19 changed files
with
137 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
app/views/books/_form.html.erb → app/views/admin/books/_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,5 @@ | |
<br> | ||
|
||
<div> | ||
<%= link_to "Back to books", books_path %> | ||
<%= link_to "Back to books", admin_books_path %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
end | ||
|
||
before do | ||
visit books_path | ||
visit admin_books_path | ||
end | ||
|
||
context "when initializing the page" do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.