Skip to content

Commit

Permalink
Merge pull request #223 from cassiosantana/refactor/222-authors-resou…
Browse files Browse the repository at this point in the history
…rce-should-be-moved-to-the-admin-namespace

refactor/222-authors-resource-should-be-moved-to-the-admin-namespace
  • Loading branch information
cassiosantana authored Sep 24, 2023
2 parents 9714ca4 + a825079 commit 181e946
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 142 deletions.
57 changes: 57 additions & 0 deletions app/controllers/admin/authors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Admin
class AuthorsController < ApplicationController
before_action :set_author, only: %i[show edit update destroy]

def index
@authors = Author.all
end

def show; end

def new
@author = Author.new
end

def edit; end

def create
@author = Author.new(author_params)

if @author.save
redirect_to admin_author_url(@author), notice: "Author was successfully created."
else
render :new, status: :unprocessable_entity
end
end

def update
if @author.update(author_params)
redirect_to admin_author_url(@author), notice: "Author was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@author.destroy

redirect_to admin_authors_url, notice: "Author was successfully destroyed."
end

def report
@author = Author.includes(:books).find(params[:id])
end

private

def set_author
@author = Author.find(params[:id])
end

def author_params
params.require(:author).permit(:name, :cpf)
end
end
end
55 changes: 0 additions & 55 deletions app/controllers/authors_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: author) do |form| %>
<%= form_with(model: [:admin, author]) do |form| %>
<% if author.errors.any? %>
<div style="color: red">
<h2><%= pluralize(author.errors.count, "error") %> prohibited this author from being saved:</h2>
Expand Down
10 changes: 10 additions & 0 deletions app/views/admin/authors/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing author</h1>

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

<br>

<div>
<%= link_to "Show this author", admin_author_path(@author) %> |
<%= link_to "Back to authors", admin_authors_path %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<p>
<strong>Name:</strong>
<%= author.name %> |
<%= link_to "Show this author", author %>
<%= link_to "Show this author", admin_author_path(author) %>
</p>
<% end %>
</div>

<%= link_to "New author", new_author_path %>
<%= link_to "New author", new_admin_author_path %>
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<br>

<div>
<%= link_to "Back to authors", authors_path %>
<%= link_to "Back to authors", admin_authors_path %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@
</section>

<div>
<%= link_to "Back to authors", authors_path %>
<%= link_to "Back to authors", admin_authors_path %>
</div>
11 changes: 11 additions & 0 deletions app/views/admin/authors/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/authors/author", author: @author %>

<div>
<%= link_to "Edit this author", edit_admin_author_path(@author) %> |
<%= link_to "Back to authors", admin_authors_path %> |
<%= link_to "Show report", report_admin_author_path(@author) %>
<%= button_to "Destroy this author", admin_author_path(@author), method: :delete %>
</div>
10 changes: 0 additions & 10 deletions app/views/authors/edit.html.erb

This file was deleted.

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

This file was deleted.

4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<%= javascript_importmap_tags %>
</head>

<body>
<body class="prose">
<nav>
<%= link_to "Home", root_path %>
<%= link_to "Authors", authors_path %>
<%= link_to "Authors", admin_authors_path %>
<%= link_to "Accounts", accounts_path %>
<%= link_to "Assemblies", assemblies_path %>
<%= link_to "Parts", parts_path %>
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

Rails.application.routes.draw do
resources :accounts, :assemblies, :parts
resources :authors, :suppliers do
resources :suppliers do
get :report, on: :member
end

namespace :admin do
resources :books do
resources :books, :authors do
get :report, on: :member
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/routing/admin/authors_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "rails_helper"

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

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

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

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

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

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

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

it "routes to #destroy" do
expect(delete: "admin/authors/1").to route_to("admin/authors#destroy", id: "1")
end
end
end
File renamed without changes.
39 changes: 0 additions & 39 deletions spec/routing/authors_routing_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe "authors/edit", type: :view do
RSpec.describe "admin/authors/edit", type: :view do
let(:author) { create(:author) }

before(:each) do
Expand All @@ -11,25 +11,25 @@
end

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

context "editing the author" do
it "renders the edit author form" do
expect(rendered).to have_selector("form[action='#{author_path(author)}'][method='post']")
expect(rendered).to have_selector("form[action='#{admin_author_path(author)}'][method='post']")
expect(rendered).to have_selector("input[name='author[name]'][value='#{author.name}']")
expect(rendered).to have_selector("input[name='author[cpf]'][value='#{author.cpf}']")
expect_submit_button("Update Author")
expect(rendered).to have_button("Update Author")
end
end

context "rendering links" do
it "renders the back link" do
expect_link_back_to("authors")
expect(rendered).to have_link("Back to authors", href: admin_authors_path)
end

it "renders the show link" do
expect_link_to_show(author)
expect(rendered).to have_link("Show this author", href: admin_author_path(author))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe "authors/index", type: :view do
RSpec.describe "admin/authors/index", type: :view do
let(:authors) { create_list(:author, 3) }

before(:each) do
Expand All @@ -11,17 +11,17 @@
end

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

it "renders the link to create a new author" do
expect_link_to_new("author")
expect(rendered).to have_link("New author", href: new_admin_author_path)
end

it "renders the list of authors" do
authors.each do |author|
expect(rendered).to have_text("Name: #{author.name}", normalize_ws: true)
expect(rendered).to have_link("Show this author", href: author_path(author))
expect(rendered).to have_link("Show this author", href: admin_author_path(author))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

require "rails_helper"

RSpec.describe "authors/new", type: :view do
RSpec.describe "admin/authors/new", type: :view do
before(:each) do
assign(:author, build(:author))
render
end

it "renders the page title" do
expect_page_title("New author")
expect(rendered).to have_selector("h1", text: "New author")
end

it "renders new author form" do
expect(rendered).to have_selector("form[action='#{authors_path}'][method='post']")
expect(rendered).to have_selector("form[action='#{admin_authors_path}'][method='post']")
expect(rendered).to have_selector("input[name='author[name]']")
expect(rendered).to have_selector("input[name='author[cpf]']")
end

it "renders the back link" do
expect_link_back_to("authors")
expect(rendered).to have_link("Back to authors", href: admin_authors_path)
end
end
Loading

0 comments on commit 181e946

Please sign in to comment.