From 4ea6a85172d75766210295a0d3238f32c9c2a10a Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Thu, 12 Aug 2021 11:32:40 +0200
Subject: [PATCH] journals

---
 .../admin/research/application_controller.rb  |  9 +++
 .../admin/research/journals_controller.rb     | 55 +++++++++++++++++++
 .../research/journals_controller.rb           |  9 +++
 app/models/research.rb                        |  5 ++
 app/models/research/journal.rb                | 26 +++++++++
 app/models/university.rb                      |  1 +
 app/models/university/with_research.rb        |  7 +++
 .../admin/research/journals/_form.html.erb    | 13 +++++
 .../admin/research/journals/edit.html.erb     |  3 +
 .../admin/research/journals/index.html.erb    | 26 +++++++++
 .../admin/research/journals/new.html.erb      |  3 +
 .../admin/research/journals/show.html.erb     |  9 +++
 .../research/journals/_journal.json.jbuilder  |  2 +
 app/views/research/journals/index.html.erb    | 13 +++++
 .../research/journals/index.json.jbuilder     |  1 +
 app/views/research/journals/show.html.erb     |  8 +++
 .../research/journals/show.json.jbuilder      |  1 +
 config/admin_navigation.rb                    | 10 ++--
 config/locales/fr.yml                         |  3 +
 config/routes.rb                              |  4 ++
 config/routes/admin.rb                        |  4 ++
 ...20210812085608_create_research_journals.rb | 11 ++++
 db/schema.rb                                  | 12 +++-
 .../research/journals_controller_test.rb      | 48 ++++++++++++++++
 test/fixtures/research/journals.yml           | 28 ++++++++++
 test/models/research/journal_test.rb          | 26 +++++++++
 test/system/research/journals_test.rb         | 45 +++++++++++++++
 27 files changed, 376 insertions(+), 6 deletions(-)
 create mode 100644 app/controllers/admin/research/application_controller.rb
 create mode 100644 app/controllers/admin/research/journals_controller.rb
 create mode 100644 app/controllers/research/journals_controller.rb
 create mode 100644 app/models/research.rb
 create mode 100644 app/models/research/journal.rb
 create mode 100644 app/models/university/with_research.rb
 create mode 100644 app/views/admin/research/journals/_form.html.erb
 create mode 100644 app/views/admin/research/journals/edit.html.erb
 create mode 100644 app/views/admin/research/journals/index.html.erb
 create mode 100644 app/views/admin/research/journals/new.html.erb
 create mode 100644 app/views/admin/research/journals/show.html.erb
 create mode 100644 app/views/research/journals/_journal.json.jbuilder
 create mode 100644 app/views/research/journals/index.html.erb
 create mode 100644 app/views/research/journals/index.json.jbuilder
 create mode 100644 app/views/research/journals/show.html.erb
 create mode 100644 app/views/research/journals/show.json.jbuilder
 create mode 100644 db/migrate/20210812085608_create_research_journals.rb
 create mode 100644 test/controllers/research/journals_controller_test.rb
 create mode 100644 test/fixtures/research/journals.yml
 create mode 100644 test/models/research/journal_test.rb
 create mode 100644 test/system/research/journals_test.rb

diff --git a/app/controllers/admin/research/application_controller.rb b/app/controllers/admin/research/application_controller.rb
new file mode 100644
index 000000000..3d6ceb0fb
--- /dev/null
+++ b/app/controllers/admin/research/application_controller.rb
@@ -0,0 +1,9 @@
+class Admin::Research::ApplicationController < Admin::ApplicationController
+
+  protected
+
+  def breadcrumb
+    super
+    add_breadcrumb 'Recherche'
+  end
+end
diff --git a/app/controllers/admin/research/journals_controller.rb b/app/controllers/admin/research/journals_controller.rb
new file mode 100644
index 000000000..593493749
--- /dev/null
+++ b/app/controllers/admin/research/journals_controller.rb
@@ -0,0 +1,55 @@
+class Admin::Research::JournalsController < Admin::Research::ApplicationController
+  load_and_authorize_resource class: Research::Journal
+
+  def index
+    breadcrumb
+  end
+
+  def show
+    breadcrumb
+  end
+
+  def new
+    breadcrumb
+  end
+
+  def edit
+    breadcrumb
+  end
+
+  def create
+    @journal.university = current_university
+    if @journal.save
+      redirect_to [:admin, @journal], notice: "Journal was successfully created."
+    else
+      breadcrumb
+      render :new, status: :unprocessable_entity
+    end
+  end
+
+  def update
+    if @journal.update(journal_params)
+      redirect_to [:admin, @journal], notice: "Journal was successfully updated."
+    else
+      breadcrumb
+      render :edit, status: :unprocessable_entity
+    end
+  end
+
+  def destroy
+    @journal.destroy
+    redirect_to admin_research_journals_url, notice: "Journal was successfully destroyed."
+  end
+
+  protected
+
+  def breadcrumb
+    super
+    add_breadcrumb Research::Journal.model_name.human(count: 2), admin_research_journals_path
+    breadcrumb_for @journal
+  end
+
+  def journal_params
+    params.require(:research_journal).permit(:title, :description)
+  end
+end
diff --git a/app/controllers/research/journals_controller.rb b/app/controllers/research/journals_controller.rb
new file mode 100644
index 000000000..2063cefc0
--- /dev/null
+++ b/app/controllers/research/journals_controller.rb
@@ -0,0 +1,9 @@
+class Research::JournalsController < ApplicationController
+  def index
+    @journals = current_university.research_journals
+  end
+
+  def show
+    @journal = current_university.research_journals.find params[:id]
+  end
+end
diff --git a/app/models/research.rb b/app/models/research.rb
new file mode 100644
index 000000000..24ccdcbe3
--- /dev/null
+++ b/app/models/research.rb
@@ -0,0 +1,5 @@
+module Research
+  def self.table_name_prefix
+    'research_'
+  end
+end
diff --git a/app/models/research/journal.rb b/app/models/research/journal.rb
new file mode 100644
index 000000000..fcdf35c01
--- /dev/null
+++ b/app/models/research/journal.rb
@@ -0,0 +1,26 @@
+# == Schema Information
+#
+# Table name: research_journals
+#
+#  id            :uuid             not null, primary key
+#  description   :text
+#  title         :string
+#  created_at    :datetime         not null
+#  updated_at    :datetime         not null
+#  university_id :uuid             not null
+#
+# Indexes
+#
+#  index_research_journals_on_university_id  (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_...  (university_id => universities.id)
+#
+class Research::Journal < ApplicationRecord
+  belongs_to :university
+
+  def to_s
+    "#{title}"
+  end
+end
diff --git a/app/models/university.rb b/app/models/university.rb
index 9354058d4..97da77b18 100644
--- a/app/models/university.rb
+++ b/app/models/university.rb
@@ -20,6 +20,7 @@ class University < ApplicationRecord
   include WithIdentifier
   include WithUsers
   include WithFeatures
+  include WithResearch
 
   def to_s
     "#{name}"
diff --git a/app/models/university/with_research.rb b/app/models/university/with_research.rb
new file mode 100644
index 000000000..67cef2638
--- /dev/null
+++ b/app/models/university/with_research.rb
@@ -0,0 +1,7 @@
+module University::WithResearch
+  extend ActiveSupport::Concern
+
+  included do
+    has_many :research_journals, class_name: 'Research::Journal', dependent: :destroy
+  end
+end
diff --git a/app/views/admin/research/journals/_form.html.erb b/app/views/admin/research/journals/_form.html.erb
new file mode 100644
index 000000000..0350626e5
--- /dev/null
+++ b/app/views/admin/research/journals/_form.html.erb
@@ -0,0 +1,13 @@
+<%= simple_form_for [:admin, journal] do |f| %>
+  <div class="row">
+    <div class="col-md-6">
+      <%= f.input :title %>
+    </div>
+    <div class="col-md-6">
+      <%= f.input :description %>
+    </div>
+  </div>
+  <% content_for :buttons do %>
+    <%= submit f %>
+  <% end %>
+<% end %>
diff --git a/app/views/admin/research/journals/edit.html.erb b/app/views/admin/research/journals/edit.html.erb
new file mode 100644
index 000000000..2ff5b9eb8
--- /dev/null
+++ b/app/views/admin/research/journals/edit.html.erb
@@ -0,0 +1,3 @@
+<% content_for :title, @journal %>
+
+<%= render 'form', journal: @journal %>
diff --git a/app/views/admin/research/journals/index.html.erb b/app/views/admin/research/journals/index.html.erb
new file mode 100644
index 000000000..851be78e0
--- /dev/null
+++ b/app/views/admin/research/journals/index.html.erb
@@ -0,0 +1,26 @@
+<% content_for :title, Research::Journal.model_name.human(count: 2) %>
+
+<table class="table">
+  <thead>
+    <tr>
+      <th>Title</th>
+      <th></th>
+    </tr>
+  </thead>
+
+  <tbody>
+    <% @journals.each do |journal| %>
+      <tr>
+        <td><%= link_to journal, [:admin, journal] %></td>
+        <td class="text-end">
+          <%= edit_link journal %>
+          <%= destroy_link journal %>
+        </td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
+
+<% content_for :buttons do %>
+  <%= create_link Research::Journal %>
+<% end %>
diff --git a/app/views/admin/research/journals/new.html.erb b/app/views/admin/research/journals/new.html.erb
new file mode 100644
index 000000000..926018c99
--- /dev/null
+++ b/app/views/admin/research/journals/new.html.erb
@@ -0,0 +1,3 @@
+<% content_for :title, Research::Journal.model_name.human %>
+
+<%= render 'form', journal: @journal %>
diff --git a/app/views/admin/research/journals/show.html.erb b/app/views/admin/research/journals/show.html.erb
new file mode 100644
index 000000000..a43f749a7
--- /dev/null
+++ b/app/views/admin/research/journals/show.html.erb
@@ -0,0 +1,9 @@
+<% content_for :title, @journal %>
+
+<p>
+  <%= @journal.description %>
+</p>
+
+<% content_for :buttons do %>
+  <%= edit_link @journal %>
+<% end %>
diff --git a/app/views/research/journals/_journal.json.jbuilder b/app/views/research/journals/_journal.json.jbuilder
new file mode 100644
index 000000000..74b1dbbe6
--- /dev/null
+++ b/app/views/research/journals/_journal.json.jbuilder
@@ -0,0 +1,2 @@
+json.extract! journal, :id, :title, :description, :created_at, :updated_at
+json.url research_journal_url(journal, format: :json)
diff --git a/app/views/research/journals/index.html.erb b/app/views/research/journals/index.html.erb
new file mode 100644
index 000000000..c5e1cad2e
--- /dev/null
+++ b/app/views/research/journals/index.html.erb
@@ -0,0 +1,13 @@
+<% content_for :title, Research::Journal.model_name.human(count: 2) %>
+
+<h1><%= Research::Journal.model_name.human(count: 2) %></h1>
+
+<div class="row">
+  <% @journals.each do |journal| %>
+    <div class="col-md-6">
+      <h2 class="mt-5"><%= journal %></h2>
+      <p><%= journal.description %></p>
+      <%= link_to 'Lire', journal, class: 'btn btn-primary stretched-link' %>
+    </div>
+  <% end %>
+</div>
diff --git a/app/views/research/journals/index.json.jbuilder b/app/views/research/journals/index.json.jbuilder
new file mode 100644
index 000000000..5e48c2f4b
--- /dev/null
+++ b/app/views/research/journals/index.json.jbuilder
@@ -0,0 +1 @@
+json.array! @journals, partial: "research/journals/journal", as: :journal
diff --git a/app/views/research/journals/show.html.erb b/app/views/research/journals/show.html.erb
new file mode 100644
index 000000000..e9aa77443
--- /dev/null
+++ b/app/views/research/journals/show.html.erb
@@ -0,0 +1,8 @@
+<% content_for :title, @journal %>
+
+<div class="row">
+  <div class="col-md-8">
+    <h1><%= @journal %></h1>
+    <p><%= @journal.description %></p>
+  </div>
+</div>
diff --git a/app/views/research/journals/show.json.jbuilder b/app/views/research/journals/show.json.jbuilder
new file mode 100644
index 000000000..fd0abfd12
--- /dev/null
+++ b/app/views/research/journals/show.json.jbuilder
@@ -0,0 +1 @@
+json.partial! "research/journals/journal", journal: @journal
diff --git a/config/admin_navigation.rb b/config/admin_navigation.rb
index dcf622602..8750b49a9 100644
--- a/config/admin_navigation.rb
+++ b/config/admin_navigation.rb
@@ -7,17 +7,17 @@ SimpleNavigation::Configuration.run do |navigation|
     primary.item :dashboard, t('dashboard'), admin_root_path, { icon: 'tachometer-alt', highlights_on: /admin$/ }
 
     primary.item :teaching, 'Enseignement', nil, { kind: :header }
-    primary.item :education, 'Formations', admin_features_education_programs_path, { icon: 'graduation-cap' }
-    primary.item :teaching, 'Ecoles', nil, { icon: 'university' }
     primary.item :teaching, 'Enseignants', nil, { icon: 'user-graduate' }
-    primary.item :teaching, 'Feedbacks', nil, { icon: 'comments' }
+    primary.item :teaching, 'Ecoles', nil, { icon: 'university' }
+    primary.item :education, 'Formations', admin_features_education_programs_path, { icon: 'graduation-cap' }
     primary.item :teaching, 'Ressources éducatives', nil, { icon: 'laptop' }
+    primary.item :teaching, 'Feedbacks', nil, { icon: 'comments' }
 
     primary.item :teaching, 'Recherche', nil, { kind: :header }
-    primary.item :teaching, 'Laboratoires', nil, { icon: 'flask' }
     primary.item :teaching, 'Chercheurs', nil, { icon: 'microscope' }
-    primary.item :teaching, 'Journaux', nil, { icon: 'newspaper' }
+    primary.item :teaching, 'Laboratoires', nil, { icon: 'flask' }
     primary.item :teaching, 'Veille', nil, { icon: 'eye' }
+    primary.item :journals, Research::Journal.model_name.human(count: 2), admin_research_journals_path, { icon: 'newspaper' }
 
     primary.item :teaching, 'Communication', nil, { kind: :header }
     primary.item :websites, 'Sites Web', admin_features_websites_sites_path, { icon: 'sitemap' }
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 9e129820f..adb539dc5 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -14,6 +14,9 @@ fr:
       user:
         one: Utilisateur
         other: Utilisateurs
+      research/journal:
+        one: Revue scientifique
+        other: Revues scientifiques
     attributes:
       university:
         name: Nom
diff --git a/config/routes.rb b/config/routes.rb
index 5a01e8091..1de595c48 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -13,5 +13,9 @@ Rails.application.routes.draw do
     end
   end
 
+  namespace :research do
+    resources :journals, only: [:index, :show]
+  end
+
   root to: 'home#index'
 end
diff --git a/config/routes/admin.rb b/config/routes/admin.rb
index ae3c23f7f..7949fef20 100644
--- a/config/routes/admin.rb
+++ b/config/routes/admin.rb
@@ -7,5 +7,9 @@ namespace :admin do
     end
   end
 
+  namespace :research do
+    resources :journals
+  end
+
   root to: 'dashboard#index'
 end
diff --git a/db/migrate/20210812085608_create_research_journals.rb b/db/migrate/20210812085608_create_research_journals.rb
new file mode 100644
index 000000000..f780c359a
--- /dev/null
+++ b/db/migrate/20210812085608_create_research_journals.rb
@@ -0,0 +1,11 @@
+class CreateResearchJournals < ActiveRecord::Migration[6.1]
+  def change
+    create_table :research_journals, id: :uuid do |t|
+      t.references :university, null: false, foreign_key: true, type: :uuid
+      t.string :title
+      t.text :description
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 153042639..db8f63cce 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2021_08_11_171021) do
+ActiveRecord::Schema.define(version: 2021_08_12_085608) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -68,6 +68,15 @@ ActiveRecord::Schema.define(version: 2021_08_11_171021) do
     t.index ["university_id"], name: "index_features_websites_sites_on_university_id"
   end
 
+  create_table "research_journals", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+    t.uuid "university_id", null: false
+    t.string "title"
+    t.text "description"
+    t.datetime "created_at", precision: 6, null: false
+    t.datetime "updated_at", precision: 6, null: false
+    t.index ["university_id"], name: "index_research_journals_on_university_id"
+  end
+
   create_table "universities", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.string "name"
     t.string "identifier"
@@ -114,5 +123,6 @@ ActiveRecord::Schema.define(version: 2021_08_11_171021) do
   add_foreign_key "features_education_programs", "universities"
   add_foreign_key "features_education_qualiopi_indicators", "features_education_qualiopi_criterions", column: "criterion_id"
   add_foreign_key "features_websites_sites", "universities"
+  add_foreign_key "research_journals", "universities"
   add_foreign_key "users", "universities"
 end
diff --git a/test/controllers/research/journals_controller_test.rb b/test/controllers/research/journals_controller_test.rb
new file mode 100644
index 000000000..565356dcc
--- /dev/null
+++ b/test/controllers/research/journals_controller_test.rb
@@ -0,0 +1,48 @@
+require "test_helper"
+
+class Research::JournalsControllerTest < ActionDispatch::IntegrationTest
+  setup do
+    @research_journal = research_journals(:one)
+  end
+
+  test "should get index" do
+    get research_journals_url
+    assert_response :success
+  end
+
+  test "should get new" do
+    get new_research_journal_url
+    assert_response :success
+  end
+
+  test "should create research_journal" do
+    assert_difference('Research::Journal.count') do
+      post research_journals_url, params: { research_journal: { description: @research_journal.description, title: @research_journal.title } }
+    end
+
+    assert_redirected_to research_journal_url(Research::Journal.last)
+  end
+
+  test "should show research_journal" do
+    get research_journal_url(@research_journal)
+    assert_response :success
+  end
+
+  test "should get edit" do
+    get edit_research_journal_url(@research_journal)
+    assert_response :success
+  end
+
+  test "should update research_journal" do
+    patch research_journal_url(@research_journal), params: { research_journal: { description: @research_journal.description, title: @research_journal.title } }
+    assert_redirected_to research_journal_url(@research_journal)
+  end
+
+  test "should destroy research_journal" do
+    assert_difference('Research::Journal.count', -1) do
+      delete research_journal_url(@research_journal)
+    end
+
+    assert_redirected_to research_journals_url
+  end
+end
diff --git a/test/fixtures/research/journals.yml b/test/fixtures/research/journals.yml
new file mode 100644
index 000000000..2799b8f96
--- /dev/null
+++ b/test/fixtures/research/journals.yml
@@ -0,0 +1,28 @@
+# == Schema Information
+#
+# Table name: research_journals
+#
+#  id            :uuid             not null, primary key
+#  description   :text
+#  title         :string
+#  created_at    :datetime         not null
+#  updated_at    :datetime         not null
+#  university_id :uuid             not null
+#
+# Indexes
+#
+#  index_research_journals_on_university_id  (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_...  (university_id => universities.id)
+#
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  title: MyString
+  description: MyText
+
+two:
+  title: MyString
+  description: MyText
diff --git a/test/models/research/journal_test.rb b/test/models/research/journal_test.rb
new file mode 100644
index 000000000..91a25b8a4
--- /dev/null
+++ b/test/models/research/journal_test.rb
@@ -0,0 +1,26 @@
+# == Schema Information
+#
+# Table name: research_journals
+#
+#  id            :uuid             not null, primary key
+#  description   :text
+#  title         :string
+#  created_at    :datetime         not null
+#  updated_at    :datetime         not null
+#  university_id :uuid             not null
+#
+# Indexes
+#
+#  index_research_journals_on_university_id  (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_...  (university_id => universities.id)
+#
+require "test_helper"
+
+class Research::JournalTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
diff --git a/test/system/research/journals_test.rb b/test/system/research/journals_test.rb
new file mode 100644
index 000000000..3c83334e6
--- /dev/null
+++ b/test/system/research/journals_test.rb
@@ -0,0 +1,45 @@
+require "application_system_test_case"
+
+class Research::JournalsTest < ApplicationSystemTestCase
+  setup do
+    @research_journal = research_journals(:one)
+  end
+
+  test "visiting the index" do
+    visit research_journals_url
+    assert_selector "h1", text: "Research/Journals"
+  end
+
+  test "creating a Journal" do
+    visit research_journals_url
+    click_on "New Research/Journal"
+
+    fill_in "Description", with: @research_journal.description
+    fill_in "Title", with: @research_journal.title
+    click_on "Create Journal"
+
+    assert_text "Journal was successfully created"
+    click_on "Back"
+  end
+
+  test "updating a Journal" do
+    visit research_journals_url
+    click_on "Edit", match: :first
+
+    fill_in "Description", with: @research_journal.description
+    fill_in "Title", with: @research_journal.title
+    click_on "Update Journal"
+
+    assert_text "Journal was successfully updated"
+    click_on "Back"
+  end
+
+  test "destroying a Journal" do
+    visit research_journals_url
+    page.accept_confirm do
+      click_on "Destroy", match: :first
+    end
+
+    assert_text "Journal was successfully destroyed"
+  end
+end
-- 
GitLab