vanderleipinto / preparation_mentorship

0 stars 0 forks source link

Create Report - Author #55

Closed vanderleipinto closed 8 months ago

vanderleipinto commented 8 months ago

Add report to author including:

vanderleipinto commented 8 months ago

Add report to author

Create report route.

get'/authors/:id/report', to: 'authors#report', as: 'authors_report'

Create the action report on app/controllers/authors_controller.rb, add this action to before_action set_author

before_action :set_author, only: %i[ report show edit update destroy ]
...

# GET /authors/1/report
  def report
    @books = @author.books      
  end

Create the page app/views/authors/report.html.erb to show the report

<style>
  table, th, td {
    border:1px solid black;
  }
</style>

<h1>Report</h1>

<h2><%=@author.name%></h2>
<div>
  <strong> CPF: <%=@author.cpf%></strong>
</div>

<div>
  <strong>Books:</strong>
  <strong>Total: <%= @books.count %></strong>
</div>

<table border="1" style="width:80%">

    <thead>
      <tr>
        <th>Book</th>
        <th>Description</th>
        <th>Assemblies</th>
        <th>Release date</th>
        <th>Suppliers</th>
      </tr>
    </thead>
    <tbody>
      <% @books.each do |book| %>
        <tr>
          <td><%= link_to book.title, book %></td>
          <td>
            <%= book.description %>
          </td>
          <td>
            <ul>
              <% assemblies = book.assemblies %>
              <% if assemblies.any? %>
                <% assemblies.each do |assembly| %>
                  <li><%= assembly.name %></li>
                <% end %>                
              <% else %>
                <li> <em>No Assemblies</em> </li>
              <% end %>
              </ul>
          </td>

          <td>
            <%= book.created_at.to_date.strftime('%d/%m/%Y') %>
          </td>
          <td>
            <ul>
              <% suppliers = Supplier.joins(parts: { assemblies: :books}).where(books: { id: book.id }) %>
              <% if suppliers.any? %>
                <% suppliers.each do |supplier| %>
                  <li><%= supplier.name %></li>
                <% end %>
              <%else%>
                  <li> <em>No suppliers</em> </li>
              <% end %>

            </ul>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>

  <%= link_to "back", author_path %>

Add the link to report on app/views/authors/show.html.erb

<p style="color: green"><%= notice %></p>

<%= render @author %>

<div>
  <%= link_to "Edit this author", edit_author_path(@author) %>
  <%= link_to "Back to authors", authors_path %>
  <%= link_to "Ver Relatório", authors_report_path(@author) %>

  <%= button_to "Destroy this author", @author, method: :delete %>

</div>