From 81265309366b142b3378a7730a674ff667f62b72 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 13 Aug 2021 12:07:10 +0200
Subject: [PATCH] front

---
 app/assets/stylesheets/application.sass       |  1 +
 .../stylesheets/application/layout.sass       |  2 +
 app/controllers/application_controller.rb     |  4 +
 .../research/journal/volumes_controller.rb    | 73 ++++---------------
 .../research/journals_controller.rb           | 11 +++
 app/models/research.rb                        |  3 +
 .../research/journal/volumes/_volume.html.erb |  9 +++
 .../research/journal/volumes/index.html.erb   | 40 +++-------
 .../research/journal/volumes/show.html.erb    | 14 +---
 .../research/journals/_journal.json.jbuilder  |  6 +-
 app/views/research/journals/index.html.erb    | 13 ++--
 .../research/journals/index.json.jbuilder     |  5 +-
 app/views/research/journals/show.html.erb     |  8 ++
 .../research/journals/show.json.jbuilder      |  7 +-
 config/locales/fr.yml                         |  1 +
 config/routes.rb                              |  8 +-
 16 files changed, 90 insertions(+), 115 deletions(-)
 create mode 100644 app/assets/stylesheets/application/layout.sass
 create mode 100644 app/views/research/journal/volumes/_volume.html.erb

diff --git a/app/assets/stylesheets/application.sass b/app/assets/stylesheets/application.sass
index 79ffd2395..d5b57dee4 100644
--- a/app/assets/stylesheets/application.sass
+++ b/app/assets/stylesheets/application.sass
@@ -1 +1,2 @@
 @import "bootstrap"
+@import 'application/*'
diff --git a/app/assets/stylesheets/application/layout.sass b/app/assets/stylesheets/application/layout.sass
new file mode 100644
index 000000000..16ae7bbad
--- /dev/null
+++ b/app/assets/stylesheets/application/layout.sass
@@ -0,0 +1,2 @@
+footer
+    margin-top: 100px
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index e91846c9b..9af4384eb 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,3 +1,7 @@
 class ApplicationController < ActionController::Base
   include WithContext
+
+  def breadcrumb
+    add_breadcrumb t('home'), root_path
+  end
 end
diff --git a/app/controllers/research/journal/volumes_controller.rb b/app/controllers/research/journal/volumes_controller.rb
index 95c9eff70..db04246a2 100644
--- a/app/controllers/research/journal/volumes_controller.rb
+++ b/app/controllers/research/journal/volumes_controller.rb
@@ -1,69 +1,24 @@
 class Research::Journal::VolumesController < ApplicationController
-  before_action :set_research_journal_volume, only: %i[ show edit update destroy ]
-
-  # GET /research/journal/volumes or /research/journal/volumes.json
   def index
-    @research_journal_volumes = Research::Journal::Volume.all
+    @journal = current_university.research_journals.find params[:journal_id]
+    @volumes = @journal.volumes
+    breadcrumb
   end
 
-  # GET /research/journal/volumes/1 or /research/journal/volumes/1.json
   def show
+    @journal = current_university.research_journals.find params[:journal_id]
+    @volume = @journal.volumes.find params[:id]
+    breadcrumb
+    add_breadcrumb @volume
   end
 
-  # GET /research/journal/volumes/new
-  def new
-    @research_journal_volume = Research::Journal::Volume.new
-  end
-
-  # GET /research/journal/volumes/1/edit
-  def edit
-  end
-
-  # POST /research/journal/volumes or /research/journal/volumes.json
-  def create
-    @research_journal_volume = Research::Journal::Volume.new(research_journal_volume_params)
+  protected
 
-    respond_to do |format|
-      if @research_journal_volume.save
-        format.html { redirect_to @research_journal_volume, notice: "Volume was successfully created." }
-        format.json { render :show, status: :created, location: @research_journal_volume }
-      else
-        format.html { render :new, status: :unprocessable_entity }
-        format.json { render json: @research_journal_volume.errors, status: :unprocessable_entity }
-      end
-    end
+  def breadcrumb
+    super
+    add_breadcrumb Research.model_name.human
+    add_breadcrumb Research::Journal.model_name.human(count: 2), research_journals_path
+    add_breadcrumb @journal, @journal
+    add_breadcrumb Research::Journal::Volume.model_name.human(count: 2), research_journal_volumes_path(journal_id: @journal)
   end
-
-  # PATCH/PUT /research/journal/volumes/1 or /research/journal/volumes/1.json
-  def update
-    respond_to do |format|
-      if @research_journal_volume.update(research_journal_volume_params)
-        format.html { redirect_to @research_journal_volume, notice: "Volume was successfully updated." }
-        format.json { render :show, status: :ok, location: @research_journal_volume }
-      else
-        format.html { render :edit, status: :unprocessable_entity }
-        format.json { render json: @research_journal_volume.errors, status: :unprocessable_entity }
-      end
-    end
-  end
-
-  # DELETE /research/journal/volumes/1 or /research/journal/volumes/1.json
-  def destroy
-    @research_journal_volume.destroy
-    respond_to do |format|
-      format.html { redirect_to research_journal_volumes_url, notice: "Volume was successfully destroyed." }
-      format.json { head :no_content }
-    end
-  end
-
-  private
-    # Use callbacks to share common setup or constraints between actions.
-    def set_research_journal_volume
-      @research_journal_volume = Research::Journal::Volume.find(params[:id])
-    end
-
-    # Only allow a list of trusted parameters through.
-    def research_journal_volume_params
-      params.require(:research_journal_volume).permit(:title, :number, :published_at)
-    end
 end
diff --git a/app/controllers/research/journals_controller.rb b/app/controllers/research/journals_controller.rb
index 2063cefc0..03a97e9f5 100644
--- a/app/controllers/research/journals_controller.rb
+++ b/app/controllers/research/journals_controller.rb
@@ -1,9 +1,20 @@
 class Research::JournalsController < ApplicationController
   def index
     @journals = current_university.research_journals
+    breadcrumb
   end
 
   def show
     @journal = current_university.research_journals.find params[:id]
+    breadcrumb
+  end
+
+  protected
+
+  def breadcrumb
+    super
+    add_breadcrumb Research.model_name.human
+    add_breadcrumb Research::Journal.model_name.human(count: 2), research_journals_path
+    add_breadcrumb @journal, @journal if @journal
   end
 end
diff --git a/app/models/research.rb b/app/models/research.rb
index 24ccdcbe3..ac5b622d1 100644
--- a/app/models/research.rb
+++ b/app/models/research.rb
@@ -1,4 +1,7 @@
 module Research
+  extend ActiveModel::Naming
+  extend ActiveModel::Translation
+
   def self.table_name_prefix
     'research_'
   end
diff --git a/app/views/research/journal/volumes/_volume.html.erb b/app/views/research/journal/volumes/_volume.html.erb
new file mode 100644
index 000000000..a52112617
--- /dev/null
+++ b/app/views/research/journal/volumes/_volume.html.erb
@@ -0,0 +1,9 @@
+<article class="card mt-4">
+  <div class="card-body">
+    <h1 class="h4">
+      <%= link_to volume,
+      research_journal_volume_path(journal_id: journal, id: volume),
+      class: 'stretched-link' %>
+    </h1>
+  </div>
+</article>
diff --git a/app/views/research/journal/volumes/index.html.erb b/app/views/research/journal/volumes/index.html.erb
index 728aa29d5..7c29510cd 100644
--- a/app/views/research/journal/volumes/index.html.erb
+++ b/app/views/research/journal/volumes/index.html.erb
@@ -1,31 +1,9 @@
-<p id="notice"><%= notice %></p>
-
-<h1>Research Journal Volumes</h1>
-
-<table>
-  <thead>
-    <tr>
-      <th>Title</th>
-      <th>Number</th>
-      <th>Published at</th>
-      <th colspan="3"></th>
-    </tr>
-  </thead>
-
-  <tbody>
-    <% @research_journal_volumes.each do |research_journal_volume| %>
-      <tr>
-        <td><%= research_journal_volume.title %></td>
-        <td><%= research_journal_volume.number %></td>
-        <td><%= research_journal_volume.published_at %></td>
-        <td><%= link_to 'Show', research_journal_volume %></td>
-        <td><%= link_to 'Edit', edit_research_journal_volume_path(research_journal_volume) %></td>
-        <td><%= link_to 'Destroy', research_journal_volume, method: :delete, data: { confirm: 'Are you sure?' } %></td>
-      </tr>
-    <% end %>
-  </tbody>
-</table>
-
-<br>
-
-<%= link_to 'New Research Journal Volume', new_research_journal_volume_path %>
+<% content_for :title, Research::Journal::Volume.model_name.human(count: 2) %>
+
+<div class="row">
+  <% @journal.volumes.each do |volume| %>
+    <div class="col-md-3">
+      <%= render volume, journal: @journal %>
+    </div>
+  <% end %>
+</div>
diff --git a/app/views/research/journal/volumes/show.html.erb b/app/views/research/journal/volumes/show.html.erb
index 2b3f84d47..ace032fb1 100644
--- a/app/views/research/journal/volumes/show.html.erb
+++ b/app/views/research/journal/volumes/show.html.erb
@@ -1,19 +1,11 @@
-<p id="notice"><%= notice %></p>
-
-<p>
-  <strong>Title:</strong>
-  <%= @research_journal_volume.title %>
-</p>
+<% content_for :title, @volume %>
 
 <p>
   <strong>Number:</strong>
-  <%= @research_journal_volume.number %>
+  <%= @volume.number %>
 </p>
 
 <p>
   <strong>Published at:</strong>
-  <%= @research_journal_volume.published_at %>
+  <%= @volume.published_at %>
 </p>
-
-<%= link_to 'Edit', edit_research_journal_volume_path(@research_journal_volume) %> |
-<%= link_to 'Back', research_journal_volumes_path %>
diff --git a/app/views/research/journals/_journal.json.jbuilder b/app/views/research/journals/_journal.json.jbuilder
index 74b1dbbe6..e0145a24d 100644
--- a/app/views/research/journals/_journal.json.jbuilder
+++ b/app/views/research/journals/_journal.json.jbuilder
@@ -1,2 +1,6 @@
-json.extract! journal, :id, :title, :description, :created_at, :updated_at
+json.extract! journal, :id, :title, :description
 json.url research_journal_url(journal, format: :json)
+json.volumes journal.volumes do |volume|
+  json.extract! volume, :id, :title, :published_at
+  json.url research_journal_volume_url(journal_id: journal, id: volume, format: :json)
+end
diff --git a/app/views/research/journals/index.html.erb b/app/views/research/journals/index.html.erb
index c5e1cad2e..b6ccc1d40 100644
--- a/app/views/research/journals/index.html.erb
+++ b/app/views/research/journals/index.html.erb
@@ -2,12 +2,15 @@
 
 <h1><%= Research::Journal.model_name.human(count: 2) %></h1>
 
-<div class="row">
-  <% @journals.each do |journal| %>
-    <div class="col-md-6">
+<% @journals.each do |journal| %>
+  <div class="row">
+    <div class="col-md-8">
       <h2 class="mt-5"><%= journal %></h2>
       <p><%= journal.description %></p>
       <%= link_to 'Lire', journal, class: 'btn btn-primary stretched-link' %>
     </div>
-  <% end %>
-</div>
+    <div class="offset-md-1 col-md-3">
+      <%= render journal.volumes.first, journal: journal if journal.volumes.any? %>
+    </div>
+  </div>
+<% end %>
diff --git a/app/views/research/journals/index.json.jbuilder b/app/views/research/journals/index.json.jbuilder
index 5e48c2f4b..465e2b7cc 100644
--- a/app/views/research/journals/index.json.jbuilder
+++ b/app/views/research/journals/index.json.jbuilder
@@ -1 +1,4 @@
-json.array! @journals, partial: "research/journals/journal", as: :journal
+json.array! @journals do |journal|
+  json.extract! journal, :id, :title, :description
+  json.url research_journal_url(journal, format: :json)
+end
diff --git a/app/views/research/journals/show.html.erb b/app/views/research/journals/show.html.erb
index e9aa77443..ec810e918 100644
--- a/app/views/research/journals/show.html.erb
+++ b/app/views/research/journals/show.html.erb
@@ -6,3 +6,11 @@
     <p><%= @journal.description %></p>
   </div>
 </div>
+
+<div class="row">
+  <% @journal.volumes.each do |volume| %>
+    <div class="col-md-3">
+      <%= render volume, journal: @journal %>
+    </div>
+  <% end %>
+</div>
diff --git a/app/views/research/journals/show.json.jbuilder b/app/views/research/journals/show.json.jbuilder
index fd0abfd12..90c860bf4 100644
--- a/app/views/research/journals/show.json.jbuilder
+++ b/app/views/research/journals/show.json.jbuilder
@@ -1 +1,6 @@
-json.partial! "research/journals/journal", journal: @journal
+json.extract! @journal, :id, :title, :description
+json.url research_journal_url(@journal, format: :json)
+json.volumes @journal.volumes do |volume|
+  json.extract! volume, :id, :title, :published_at
+  json.url research_journal_volume_url(journal_id: @journal, id: volume, format: :json)
+end
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 9e129820f..0720713ed 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -1,6 +1,7 @@
 fr:
   true: Oui
   false: Non
+  home: Accueil
   dashboard: Tableau de bord
   show: Voir
   create: Créer
diff --git a/config/routes.rb b/config/routes.rb
index 2852bdaeb..5a0cea317 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,9 +1,4 @@
 Rails.application.routes.draw do
-  namespace :research do
-    namespace :journal do
-      resources :volumes
-    end
-  end
   devise_for :users, controllers: {
     registrations: 'users/registrations',
     sessions: 'users/sessions'
@@ -20,7 +15,8 @@ Rails.application.routes.draw do
 
   namespace :research do
     resources :journals, only: [:index, :show] do
-      resources :volumes, only: [:index, :show]
+      resources :volumes, only: [:index, :show], controller: 'journal/volumes'
+      resources :articles, only: [:index, :show], controller: 'journal/articles'
     end
   end
 
-- 
GitLab