From 209bf79f0fde49700bf0c7fc0308c5d82ebb0245 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Tue, 26 Oct 2021 06:45:59 +0200 Subject: [PATCH] schools --- .../admin/education/schools_controller.rb | 59 +++++++++++++++++++ app/models/ability.rb | 1 + app/models/education/school.rb | 31 ++++++++++ app/models/research/journal/article.rb | 2 +- app/models/university/with_education.rb | 1 + .../admin/education/programs/index.html.erb | 6 +- .../admin/education/schools/_form.html.erb | 22 +++++++ .../admin/education/schools/edit.html.erb | 3 + .../admin/education/schools/index.html.erb | 33 +++++++++++ .../admin/education/schools/new.html.erb | 3 + .../admin/education/schools/show.html.erb | 11 ++++ .../research/journal/volumes/index.html.erb | 6 +- .../admin/research/journals/index.html.erb | 6 +- .../admin/research/researchers/index.html.erb | 6 +- config/admin_navigation.rb | 2 +- config/locales/education/en.yml | 3 + config/locales/education/fr.yml | 3 + config/routes.rb | 3 +- config/routes/admin/education.rb | 2 +- ...0211026035253_create_university_schools.rb | 16 +++++ db/schema.rb | 17 +++++- docs/models.md | 2 + .../university/schools_controller_test.rb | 48 +++++++++++++++ test/fixtures/university/schools.yml | 21 +++++++ test/models/university/school_test.rb | 7 +++ test/system/university/schools_test.rb | 57 ++++++++++++++++++ 26 files changed, 357 insertions(+), 14 deletions(-) create mode 100644 app/controllers/admin/education/schools_controller.rb create mode 100644 app/models/education/school.rb create mode 100644 app/views/admin/education/schools/_form.html.erb create mode 100644 app/views/admin/education/schools/edit.html.erb create mode 100644 app/views/admin/education/schools/index.html.erb create mode 100644 app/views/admin/education/schools/new.html.erb create mode 100644 app/views/admin/education/schools/show.html.erb create mode 100644 db/migrate/20211026035253_create_university_schools.rb create mode 100644 test/controllers/university/schools_controller_test.rb create mode 100644 test/fixtures/university/schools.yml create mode 100644 test/models/university/school_test.rb create mode 100644 test/system/university/schools_test.rb diff --git a/app/controllers/admin/education/schools_controller.rb b/app/controllers/admin/education/schools_controller.rb new file mode 100644 index 000000000..821eb116e --- /dev/null +++ b/app/controllers/admin/education/schools_controller.rb @@ -0,0 +1,59 @@ +class Admin::Education::SchoolsController < Admin::Education::ApplicationController + load_and_authorize_resource class: Education::School + + def index + @schools = current_university.education_schools + breadcrumb + end + + def show + breadcrumb + end + + def new + breadcrumb + end + + def edit + breadcrumb + add_breadcrumb t('edit') + end + + def create + @school.university = current_university + if @school.save + redirect_to [:admin, @school], notice: t('admin.successfully_created_html', model: @school.to_s) + else + breadcrumb + render :new, status: :unprocessable_entity + end + end + + def update + if @school.update(school_params) + redirect_to [:admin, @school], notice: t('admin.successfully_updated_html', model: @school.to_s) + else + breadcrumb + add_breadcrumb t('edit') + render :edit, status: :unprocessable_entity + end + end + + def destroy + @school.destroy + redirect_to admin_university_schools_url, notice: t('admin.successfully_destroyed_html', model: @school.to_s) + end + + private + + def breadcrumb + super + add_breadcrumb Education::School.model_name.human(count: 2), admin_education_schools_path + breadcrumb_for @school + end + + def school_params + params.require(:education_school) + .permit(:university_id, :name, :address, :zipcode, :city, :country, :latitude, :longitude) + end +end diff --git a/app/models/ability.rb b/app/models/ability.rb index ec5b77911..488d9fb67 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -20,6 +20,7 @@ class Ability can :read, Communication::Website::Imported::Page, university_id: @user.university_id can :read, Communication::Website::Imported::Post, university_id: @user.university_id can :read, Education::Program, university_id: @user.university_id + can :read, Education::School, university_id: @user.university_id can :read, Research::Researcher can :read, Research::Journal, university_id: @user.university_id can :read, Research::Journal::Article, university_id: @user.university_id diff --git a/app/models/education/school.rb b/app/models/education/school.rb new file mode 100644 index 000000000..f5352ef5a --- /dev/null +++ b/app/models/education/school.rb @@ -0,0 +1,31 @@ +# == Schema Information +# +# Table name: education_schools +# +# id :uuid not null, primary key +# address :string +# city :string +# country :string +# latitude :float +# longitude :float +# name :string +# zipcode :string +# created_at :datetime not null +# updated_at :datetime not null +# university_id :uuid not null +# +# Indexes +# +# index_education_schools_on_university_id (university_id) +# +# Foreign Keys +# +# fk_rails_... (university_id => universities.id) +# +class Education::School < ApplicationRecord + belongs_to :university + + def to_s + "#{name}" + end +end diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb index b389b460a..6dbbde909 100644 --- a/app/models/research/journal/article.rb +++ b/app/models/research/journal/article.rb @@ -11,7 +11,7 @@ # text :text # title :string # created_at :datetime not null -# updated_at :datetime not null +# updated_at :date not null # research_journal_id :uuid not null # research_journal_volume_id :uuid # university_id :uuid not null diff --git a/app/models/university/with_education.rb b/app/models/university/with_education.rb index 35317c68f..5acddc90f 100644 --- a/app/models/university/with_education.rb +++ b/app/models/university/with_education.rb @@ -3,5 +3,6 @@ module University::WithEducation included do has_many :education_programs, class_name: 'Education::Program', dependent: :destroy + has_many :education_schools, class_name: 'Education::School', dependent: :destroy end end diff --git a/app/views/admin/education/programs/index.html.erb b/app/views/admin/education/programs/index.html.erb index e220d4867..9d3783318 100644 --- a/app/views/admin/education/programs/index.html.erb +++ b/app/views/admin/education/programs/index.html.erb @@ -14,8 +14,10 @@ <td><%= link_to program, [:admin, program] %></td> <td><%= program.level %></td> <td class="text-end"> - <%= edit_link program %> - <%= destroy_link program %> + <div class="btn-group" role="group"> + <%= edit_link program %> + <%= destroy_link program %> + </div> </td> </tr> <% end %> diff --git a/app/views/admin/education/schools/_form.html.erb b/app/views/admin/education/schools/_form.html.erb new file mode 100644 index 000000000..a6859f685 --- /dev/null +++ b/app/views/admin/education/schools/_form.html.erb @@ -0,0 +1,22 @@ +<%= simple_form_for [:admin, school] do |f| %> + <div class="row"> + <div class="col-md-4"> + <%= f.input :name %> + </div> + <div class="col-md-8"> + <%= f.input :address %> + <div class="row"> + <div class="col-md-4"> + <%= f.input :zipcode %> + </div> + <div class="col-md-8"> + <%= f.input :city %> + </div> + </div> + <%= f.input :country %> + </div> + </div> + <% content_for :action_bar_right do %> + <%= submit f %> + <% end %> +<% end %> diff --git a/app/views/admin/education/schools/edit.html.erb b/app/views/admin/education/schools/edit.html.erb new file mode 100644 index 000000000..2ffe2c0ea --- /dev/null +++ b/app/views/admin/education/schools/edit.html.erb @@ -0,0 +1,3 @@ +<% content_for :title, @school %> + +<%= render 'form', school: @school %> diff --git a/app/views/admin/education/schools/index.html.erb b/app/views/admin/education/schools/index.html.erb new file mode 100644 index 000000000..60026b10e --- /dev/null +++ b/app/views/admin/education/schools/index.html.erb @@ -0,0 +1,33 @@ +<% content_for :title, Education::School.model_name.human(count: 2) %> + +<table class="table"> + <thead> + <tr> + <th><%= Education::School.human_attribute_name('name') %></th> + <th><%= Education::School.human_attribute_name('address') %></th> + <th><%= Education::School.human_attribute_name('zipcode') %></th> + <th><%= Education::School.human_attribute_name('city') %></th> + <th></th> + </tr> + </thead> + + <tbody> + <% @schools.each do |school| %> + <tr> + <td><%= link_to school.name, [:admin, school] %></td> + <td><%= school.address %></td> + <td><%= school.zipcode %></td> + <td><%= school.city %></td> + <td class="text-end"> + <div class="btn-group" role="group"> + <%= edit_link school %> + <%= destroy_link school %> + </div> + </td> + <% end %> + </tbody> +</table> + +<% content_for :action_bar_right do %> + <%= create_link Education::School %> +<% end %> diff --git a/app/views/admin/education/schools/new.html.erb b/app/views/admin/education/schools/new.html.erb new file mode 100644 index 000000000..788e600fd --- /dev/null +++ b/app/views/admin/education/schools/new.html.erb @@ -0,0 +1,3 @@ +<% content_for :title, Education::School.model_name.human %> + +<%= render 'form', school: @school %> diff --git a/app/views/admin/education/schools/show.html.erb b/app/views/admin/education/schools/show.html.erb new file mode 100644 index 000000000..948820033 --- /dev/null +++ b/app/views/admin/education/schools/show.html.erb @@ -0,0 +1,11 @@ +<% content_for :title, @school %> + +<p> + <%= @school.address %><br> + <%= @school.zipcode %> <%= @school.city %><br> + <%= @school.country %> +</p> + +<% content_for :action_bar_right do %> + <%= edit_link @school %> +<% end %> diff --git a/app/views/admin/research/journal/volumes/index.html.erb b/app/views/admin/research/journal/volumes/index.html.erb index 34a38c725..6cc3da67a 100644 --- a/app/views/admin/research/journal/volumes/index.html.erb +++ b/app/views/admin/research/journal/volumes/index.html.erb @@ -17,8 +17,10 @@ height: 100 if volume.cover.attached? %></td> <td><%= volume.published_at %></td> <td class="text-end"> - <%= edit_link volume, { journal_id: @journal.id } %> - <%#= destroy_link volume, journal_id: @journal.id %> + <div class="btn-group" role="group"> + <%= edit_link volume, { journal_id: @journal.id } %> + <%#= destroy_link volume, journal_id: @journal.id %> + </div> </td> </tr> <% end %> diff --git a/app/views/admin/research/journals/index.html.erb b/app/views/admin/research/journals/index.html.erb index e3a66dd73..3f4804c69 100644 --- a/app/views/admin/research/journals/index.html.erb +++ b/app/views/admin/research/journals/index.html.erb @@ -17,8 +17,10 @@ <td><%= link_to "#{journal.volumes.count}", admin_research_journal_volumes_path(journal_id: journal) %></td> <td><%= link_to "#{journal.articles.count}", admin_research_journal_articles_path(journal_id: journal) %></td> <td class="text-end"> - <%= edit_link journal %> - <%= destroy_link journal %> + <div class="btn-group" role="group"> + <%= edit_link journal %> + <%= destroy_link journal %> + </div> </td> </tr> <% end %> diff --git a/app/views/admin/research/researchers/index.html.erb b/app/views/admin/research/researchers/index.html.erb index f7307ecab..c975d1220 100644 --- a/app/views/admin/research/researchers/index.html.erb +++ b/app/views/admin/research/researchers/index.html.erb @@ -17,8 +17,10 @@ <td><%= link_to researcher.last_name, [:admin, researcher] %></td> <td><%= link_to researcher.user, [:admin, researcher.user] if researcher.user %></td> <td class="text-end"> - <%= edit_link researcher %> - <%= destroy_link researcher %> + <div class="btn-group" role="group"> + <%= edit_link researcher %> + <%= destroy_link researcher %> + </div> </td> </tr> <% end %> diff --git a/config/admin_navigation.rb b/config/admin_navigation.rb index eb04f04d5..5cee38c3a 100644 --- a/config/admin_navigation.rb +++ b/config/admin_navigation.rb @@ -9,7 +9,7 @@ SimpleNavigation::Configuration.run do |navigation| if can?(:read, Education::Program) primary.item :education, Education.model_name.human, nil, { kind: :header } primary.item :education, 'Enseignants', nil, { icon: 'user-graduate' } - primary.item :education, 'Ecoles', nil, { icon: 'university' } + primary.item :education, Education::School.model_name.human(count: 2), admin_education_schools_path, { icon: 'university' } if can?(:read, Education::School) primary.item :education_programs, Education::Program.model_name.human(count: 2), admin_education_programs_path, { icon: 'graduation-cap' } if can?(:read, Education::Program) primary.item :education, 'Ressources éducatives', nil, { icon: 'laptop' } primary.item :education, 'Feedbacks', nil, { icon: 'comments' } diff --git a/config/locales/education/en.yml b/config/locales/education/en.yml index de3454cea..6fe56f66b 100644 --- a/config/locales/education/en.yml +++ b/config/locales/education/en.yml @@ -7,6 +7,9 @@ en: education/program: one: Program other: Programs + education/school: + one: School + other: Schools attributes: education/program: name: Name diff --git a/config/locales/education/fr.yml b/config/locales/education/fr.yml index ce89d9fb2..983bb8e43 100644 --- a/config/locales/education/fr.yml +++ b/config/locales/education/fr.yml @@ -7,6 +7,9 @@ fr: education/program: one: Formation other: Formations + education/school: + one: École + other: Écoles attributes: education/program: name: Nom diff --git a/config/routes.rb b/config/routes.rb index 7741968a9..47406f626 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,6 @@ Rails.application.routes.draw do - match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post] - + devise_for :users, controllers: { confirmations: 'users/confirmations', passwords: 'users/passwords', diff --git a/config/routes/admin/education.rb b/config/routes/admin/education.rb index c6f58926f..9f158d591 100644 --- a/config/routes/admin/education.rb +++ b/config/routes/admin/education.rb @@ -1,3 +1,3 @@ namespace :education do - resources :programs + resources :programs, :schools end diff --git a/db/migrate/20211026035253_create_university_schools.rb b/db/migrate/20211026035253_create_university_schools.rb new file mode 100644 index 000000000..020a3a4b7 --- /dev/null +++ b/db/migrate/20211026035253_create_university_schools.rb @@ -0,0 +1,16 @@ +class CreateUniversitySchools < ActiveRecord::Migration[6.1] + def change + create_table :education_schools, id: :uuid do |t| + t.references :university, null: false, foreign_key: true, type: :uuid + t.string :name + t.string :address + t.string :zipcode + t.string :city + t.string :country + t.float :latitude + t.float :longitude + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 0d683a415..8f3bd4863 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_10_25_124617) do +ActiveRecord::Schema.define(version: 2021_10_26_035253) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -238,6 +238,20 @@ ActiveRecord::Schema.define(version: 2021_10_25_124617) do t.index ["university_id"], name: "index_education_programs_on_university_id" end + create_table "education_schools", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.uuid "university_id", null: false + t.string "name" + t.string "address" + t.string "zipcode" + t.string "city" + t.string "country" + t.float "latitude" + t.float "longitude" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["university_id"], name: "index_education_schools_on_university_id" + end + create_table "languages", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "name" t.string "iso_code" @@ -390,6 +404,7 @@ ActiveRecord::Schema.define(version: 2021_10_25_124617) do add_foreign_key "communication_website_posts", "universities" add_foreign_key "communication_websites", "universities" add_foreign_key "education_programs", "universities" + add_foreign_key "education_schools", "universities" add_foreign_key "research_journal_articles", "research_journal_volumes" add_foreign_key "research_journal_articles", "research_journals" add_foreign_key "research_journal_articles", "universities" diff --git a/docs/models.md b/docs/models.md index 9df28aff1..b34f9b807 100644 --- a/docs/models.md +++ b/docs/models.md @@ -17,6 +17,8 @@ - zipcode:string - city:string - country:string +- latitude:float +- longitude:float ## university/Campus diff --git a/test/controllers/university/schools_controller_test.rb b/test/controllers/university/schools_controller_test.rb new file mode 100644 index 000000000..cebe7f55a --- /dev/null +++ b/test/controllers/university/schools_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class University::SchoolsControllerTest < ActionDispatch::IntegrationTest + setup do + @university_school = university_schools(:one) + end + + test "should get index" do + get university_schools_url + assert_response :success + end + + test "should get new" do + get new_university_school_url + assert_response :success + end + + test "should create university_school" do + assert_difference('University::School.count') do + post university_schools_url, params: { university_school: { address: @university_school.address, city: @university_school.city, country: @university_school.country, latitude: @university_school.latitude, longitude: @university_school.longitude, name: @university_school.name, university_id: @university_school.university_id, zipcode: @university_school.zipcode } } + end + + assert_redirected_to university_school_url(University::School.last) + end + + test "should show university_school" do + get university_school_url(@university_school) + assert_response :success + end + + test "should get edit" do + get edit_university_school_url(@university_school) + assert_response :success + end + + test "should update university_school" do + patch university_school_url(@university_school), params: { university_school: { address: @university_school.address, city: @university_school.city, country: @university_school.country, latitude: @university_school.latitude, longitude: @university_school.longitude, name: @university_school.name, university_id: @university_school.university_id, zipcode: @university_school.zipcode } } + assert_redirected_to university_school_url(@university_school) + end + + test "should destroy university_school" do + assert_difference('University::School.count', -1) do + delete university_school_url(@university_school) + end + + assert_redirected_to university_schools_url + end +end diff --git a/test/fixtures/university/schools.yml b/test/fixtures/university/schools.yml new file mode 100644 index 000000000..b6652d0a1 --- /dev/null +++ b/test/fixtures/university/schools.yml @@ -0,0 +1,21 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + university: one + name: MyString + address: MyString + zipcode: MyString + city: MyString + country: MyString + latitude: 1.5 + longitude: 1.5 + +two: + university: two + name: MyString + address: MyString + zipcode: MyString + city: MyString + country: MyString + latitude: 1.5 + longitude: 1.5 diff --git a/test/models/university/school_test.rb b/test/models/university/school_test.rb new file mode 100644 index 000000000..f27e5f1bc --- /dev/null +++ b/test/models/university/school_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class University::SchoolTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/university/schools_test.rb b/test/system/university/schools_test.rb new file mode 100644 index 000000000..3055e119d --- /dev/null +++ b/test/system/university/schools_test.rb @@ -0,0 +1,57 @@ +require "application_system_test_case" + +class University::SchoolsTest < ApplicationSystemTestCase + setup do + @university_school = university_schools(:one) + end + + test "visiting the index" do + visit university_schools_url + assert_selector "h1", text: "University/Schools" + end + + test "creating a School" do + visit university_schools_url + click_on "New University/School" + + fill_in "Address", with: @university_school.address + fill_in "City", with: @university_school.city + fill_in "Country", with: @university_school.country + fill_in "Latitude", with: @university_school.latitude + fill_in "Longitude", with: @university_school.longitude + fill_in "Name", with: @university_school.name + fill_in "University", with: @university_school.university_id + fill_in "Zipcode", with: @university_school.zipcode + click_on "Create School" + + assert_text "School was successfully created" + click_on "Back" + end + + test "updating a School" do + visit university_schools_url + click_on "Edit", match: :first + + fill_in "Address", with: @university_school.address + fill_in "City", with: @university_school.city + fill_in "Country", with: @university_school.country + fill_in "Latitude", with: @university_school.latitude + fill_in "Longitude", with: @university_school.longitude + fill_in "Name", with: @university_school.name + fill_in "University", with: @university_school.university_id + fill_in "Zipcode", with: @university_school.zipcode + click_on "Update School" + + assert_text "School was successfully updated" + click_on "Back" + end + + test "destroying a School" do + visit university_schools_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "School was successfully destroyed" + end +end -- GitLab