diff --git a/app/controllers/admin/authors_controller.rb b/app/controllers/admin/authors_controller.rb new file mode 100644 index 00000000..21eab6bc --- /dev/null +++ b/app/controllers/admin/authors_controller.rb @@ -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 diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb deleted file mode 100644 index 2a65b45f..00000000 --- a/app/controllers/authors_controller.rb +++ /dev/null @@ -1,55 +0,0 @@ -# frozen_string_literal: true - -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 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 author_url(@author), notice: "Author was successfully updated." - else - render :edit, status: :unprocessable_entity - end - end - - def destroy - @author.destroy - - redirect_to 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 diff --git a/app/views/authors/_author.html.erb b/app/views/admin/authors/_author.html.erb similarity index 100% rename from app/views/authors/_author.html.erb rename to app/views/admin/authors/_author.html.erb diff --git a/app/views/authors/_form.html.erb b/app/views/admin/authors/_form.html.erb similarity index 91% rename from app/views/authors/_form.html.erb rename to app/views/admin/authors/_form.html.erb index 7dd9d1b5..1b84f307 100644 --- a/app/views/authors/_form.html.erb +++ b/app/views/admin/authors/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: author) do |form| %> +<%= form_with(model: [:admin, author]) do |form| %> <% if author.errors.any? %>

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

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

Editing author

+ +<%= render "form", author: @author %> + +
+ +
+ <%= link_to "Show this author", admin_author_path(@author) %> | + <%= link_to "Back to authors", admin_authors_path %> +
diff --git a/app/views/authors/index.html.erb b/app/views/admin/authors/index.html.erb similarity index 63% rename from app/views/authors/index.html.erb rename to app/views/admin/authors/index.html.erb index f73d7056..65ed88d3 100644 --- a/app/views/authors/index.html.erb +++ b/app/views/admin/authors/index.html.erb @@ -7,9 +7,9 @@

Name: <%= author.name %> | - <%= link_to "Show this author", author %> + <%= link_to "Show this author", admin_author_path(author) %>

<% end %>
-<%= link_to "New author", new_author_path %> +<%= link_to "New author", new_admin_author_path %> diff --git a/app/views/authors/new.html.erb b/app/views/admin/authors/new.html.erb similarity index 58% rename from app/views/authors/new.html.erb rename to app/views/admin/authors/new.html.erb index 0aed7e20..7d9d1231 100644 --- a/app/views/authors/new.html.erb +++ b/app/views/admin/authors/new.html.erb @@ -5,5 +5,5 @@
- <%= link_to "Back to authors", authors_path %> + <%= link_to "Back to authors", admin_authors_path %>
diff --git a/app/views/authors/report.html.erb b/app/views/admin/authors/report.html.erb similarity index 95% rename from app/views/authors/report.html.erb rename to app/views/admin/authors/report.html.erb index 47aeb1b3..660b7776 100644 --- a/app/views/authors/report.html.erb +++ b/app/views/admin/authors/report.html.erb @@ -55,5 +55,5 @@
- <%= link_to "Back to authors", authors_path %> + <%= link_to "Back to authors", admin_authors_path %>
diff --git a/app/views/admin/authors/show.html.erb b/app/views/admin/authors/show.html.erb new file mode 100644 index 00000000..6f6bebbd --- /dev/null +++ b/app/views/admin/authors/show.html.erb @@ -0,0 +1,11 @@ +

<%= notice %>

+ +<%= render "admin/authors/author", author: @author %> + +
+ <%= 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 %> +
diff --git a/app/views/authors/edit.html.erb b/app/views/authors/edit.html.erb deleted file mode 100644 index a65f7a34..00000000 --- a/app/views/authors/edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

Editing author

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

<%= notice %>

- -<%= render @author %> - -
- <%= link_to "Edit this author", edit_author_path(@author) %> | - <%= link_to "Back to authors", authors_path %> | - <%= link_to "Show report", report_author_path(@author) %> - - <%= button_to "Destroy this author", @author, method: :delete %> -
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 01c11e0a..a7df0b82 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,10 +11,10 @@ <%= javascript_importmap_tags %> - +