adding teacher profiles

This commit is contained in:
Torsten 2023-01-15 14:52:40 +02:00
parent 45f59f174b
commit 750bd76a7e
21 changed files with 288 additions and 32 deletions

View File

@ -0,0 +1,59 @@
class TeachersController < ApplicationController
before_action :set_teacher, only: %i[ show edit update destroy ]
# GET /teachers
def index
@teachers = Teacher.all
end
# GET /teachers/1
def show
end
# GET /teachers/new
def new
@teacher = Teacher.new
end
# GET /teachers/1/edit
def edit
end
# POST /teachers
def create
@teacher = Teacher.new(teacher_params)
@teacher.member = current_member
if @teacher.save
redirect_to @teacher, notice: "Successfully created Teacher profile"
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /teachers/1
def update
if @teacher.update(teacher_params)
redirect_to @teacher, notice: "Teacher Profile was updated."
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /teachers/1
def destroy
@teacher.destroy
redirect_to teachers_url, notice: "Teacher was successfully destroyed."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_teacher
@teacher = Teacher.find(params[:id])
end
# Only allow a list of trusted parameters through.
def teacher_params
params.require(:teacher).permit(:name, :bio, :picture)
end
end

View File

@ -0,0 +1,2 @@
module TeachersHelper
end

View File

@ -20,8 +20,8 @@ class Entity < ApplicationRecord
end
def self.response(path = "")
host = Rails.credentials.weather_dev
host = Rails.credentials.weather_pro if Rails.env.production?
host = Rails.application.credentials.weather_dev
host = Rails.application.credentials.weather_pro if Rails.env.production?
token = Rails.application.credentials.weather_token
begin
all = RestClient.get( "#{host}:8123/api/states#{path}" ,

View File

@ -7,11 +7,10 @@ class Member < ApplicationRecord
mount_uploader :picture, PictureUploader
has_many :entities
has_one :teacher
def admin
true
end
def admin?
true
email == "torsten@villataika.fi"
end
alias :admin :admin?
end

10
app/models/teacher.rb Normal file
View File

@ -0,0 +1,10 @@
class Teacher < ApplicationRecord
belongs_to :member
validates :name , presence: true
validates :bio , presence: true
validates :picture , presence: true
mount_uploader :picture, PictureUploader
end

View File

@ -9,7 +9,7 @@
= f.label :name, class: "block font-bold mb-2 text-gray-700 text-sm"
= f.text_field :name,
autocomplete: "name",
placeholder: "Pekka Juustonen",
placeholder: "Joona Virtanen",
class: "appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none shadow focus:shadow-outline"
.mb-4
= f.label :email, class: "block font-bold mb-2 text-gray-700 text-sm"

View File

@ -1,25 +1,16 @@
= paginate @members
.flex.justify-center
%h1 Listing members
%table
%thead
%tr
%th Name
%th Public
%th
%th
%th
%tbody
- @members.each do |member|
%tr
%td= member.name
%td= member.public
%td= link_to 'Show', member
%td= link_to 'Edit', edit_member_path(member)
%td= link_to 'Destroy', member, method: :delete, data: { confirm: 'Are you sure?' }
%br
= link_to 'New Member', new_member_path
.grid.grid-cols-4
- @members.each do |member|
.fex.flex-col.overflow-hidden.rounded-lg.border.border-gray-100.shadow-sm.m-10
=link_to member do
= image_for( member , class: "h-60 w-full object-cover")
%h3.pt-5.text-2xl.bg-gray-100.text-black.font-bold.text-center
= member.name
.p-2.text-xs.bg-gray-50.text-black.font-bold.text-center
= stayed member
%div.h-full
.p-5.text-center
.m-2.text-sm.leading-relaxed.line-clamp-3{ prose_classes }
= shorten markdown(member.bio)

View File

@ -23,6 +23,15 @@
= link_to edit_member_path(@member) do
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
Edit
- if @member.teacher
= link_to edit_teacher_path(@member.teacher) do
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
Edit teacher profile
-else
= link_to new_teacher_path do
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
Create teacher profile
.grid.grid-cols-3.gap-4
-@member.entities.each do |entity|
%div= entity.type

View File

@ -0,0 +1,13 @@
= simple_form_for @teacher do |f|
= f.error_notification
= f.input :name
= f.input :bio
= f.input :picture , as: :file , label: (@teacher.picture.blank? ? "Add picture" : "Change picture (optional)")
.flex.justify-between.mt-4
%button.bg-cyan-200{class: button_classes}
= f.submit 'Save'
%button.bg-cyan-200{class: button_classes}
= link_to 'Cancel', teachers_path

View File

@ -0,0 +1,4 @@
.flex.justify-center
.column
.text-xl.m-4 Edit your teacher profile
= render 'form'

View File

@ -0,0 +1,16 @@
= paginate @teachers
.flex.justify-center
.grid.grid-cols-4
- @teachers.each do |teacher|
.fex.flex-col.overflow-hidden.rounded-lg.border.border-gray-100.shadow-sm.m-10
=link_to teacher do
= image_for( teacher , class: "h-60 w-full object-cover")
%h3.pt-5.text-2xl.bg-gray-100.text-black.font-bold.text-center
= teacher.name
.p-2.text-xs.bg-gray-50.text-black.font-bold.text-center
= stayed teacher
%div.h-full
.p-5.text-center
.m-2.text-sm.leading-relaxed.line-clamp-3{ prose_classes }
= shorten markdown(teacher.bio)

View File

@ -0,0 +1,4 @@
.flex.justify-center
.column
.text-xl.m-4 Create your teacher profile
= render 'form'

View File

@ -0,0 +1,10 @@
.px-4.py-16.mx-auto.sm:max-w-xl.md:max-w-full.lg:max-w-screen-xl.md:px-24.lg:px-8.lg:py-20
.flex.flex-col.max-w-screen-lg.overflow-hidden.bg-white.border.rounded.shadow-sm.lg:flex-row.sm:mx-auto
.relative{:class => "lg:w-1/2"}
-if @teacher.picture_url
= image_tag @teacher.picture_url, class: "object-cover w-full lg:absolute h-80 lg:h-full"
.flex.flex-col.justify-center.p-8.lg:p-16.lg:pl-10{:class => "lg:w-1/2"}
%h5.mb-3.text-3xl.font-extrabold.leading-none.sm:text-4xl
= @teacher.name
.mb-8.text-gray-800
.prose= markdown(@teacher.bio)

View File

@ -14,7 +14,7 @@ SimpleForm.setup do |config|
# stack. The options given below are used to wrap the
# whole input.
config.wrappers :default, tag: 'div', class: '', error_class: '', valid_class: '' do |b|
config.wrappers :default, tag: 'div', class: 'mt-4', error_class: '', valid_class: '' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :teachers
resources :entities
devise_for :members

View File

@ -0,0 +1,12 @@
class CreateTeachers < ActiveRecord::Migration[7.0]
def change
create_table :teachers do |t|
t.string :name , null: false
t.text :bio , null: false
t.string :picture , null: false
t.references :member, null: false, foreign_key: true
t.timestamps
end
end
end

13
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_01_13_170732) do
ActiveRecord::Schema[7.0].define(version: 2023_01_15_120517) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -56,6 +56,16 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_170732) do
t.index ["reset_password_token"], name: "index_members_on_reset_password_token", unique: true
end
create_table "teachers", force: :cascade do |t|
t.string "name", null: false
t.text "bio", null: false
t.string "picture", null: false
t.bigint "member_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["member_id"], name: "index_teachers_on_member_id"
end
create_table "thredded_categories", force: :cascade do |t|
t.bigint "messageboard_id", null: false
t.text "name", null: false
@ -288,6 +298,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_170732) do
end
add_foreign_key "entities", "members"
add_foreign_key "teachers", "members"
add_foreign_key "thredded_messageboard_users", "thredded_messageboards", on_delete: :cascade
add_foreign_key "thredded_messageboard_users", "thredded_user_details", on_delete: :cascade
add_foreign_key "thredded_user_post_notifications", "members", column: "user_id", on_delete: :cascade

View File

@ -0,0 +1,48 @@
require "test_helper"
class TeachersControllerTest < ActionDispatch::IntegrationTest
setup do
@teacher = teachers(:one)
end
test "should get index" do
get teachers_url
assert_response :success
end
test "should get new" do
get new_teacher_url
assert_response :success
end
test "should create teacher" do
assert_difference("Teacher.count") do
post teachers_url, params: { teacher: { bio: @teacher.bio, member_id: @teacher.member_id, name: @teacher.name, picture: @teacher.picture } }
end
assert_redirected_to teacher_url(Teacher.last)
end
test "should show teacher" do
get teacher_url(@teacher)
assert_response :success
end
test "should get edit" do
get edit_teacher_url(@teacher)
assert_response :success
end
test "should update teacher" do
patch teacher_url(@teacher), params: { teacher: { bio: @teacher.bio, member_id: @teacher.member_id, name: @teacher.name, picture: @teacher.picture } }
assert_redirected_to teacher_url(@teacher)
end
test "should destroy teacher" do
assert_difference("Teacher.count", -1) do
delete teacher_url(@teacher)
end
assert_redirected_to teachers_url
end
end

13
test/fixtures/teachers.yml vendored Normal file
View File

@ -0,0 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
bio: MyString
picture: MyString
member: one
two:
name: MyString
bio: MyString
picture: MyString
member: two

View File

@ -0,0 +1,7 @@
require "test_helper"
class TeacherTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,47 @@
require "application_system_test_case"
class TeachersTest < ApplicationSystemTestCase
setup do
@teacher = teachers(:one)
end
test "visiting the index" do
visit teachers_url
assert_selector "h1", text: "Teachers"
end
test "should create teacher" do
visit teachers_url
click_on "New teacher"
fill_in "Bio", with: @teacher.bio
fill_in "Member", with: @teacher.member_id
fill_in "Name", with: @teacher.name
fill_in "Picture", with: @teacher.picture
click_on "Create Teacher"
assert_text "Teacher was successfully created"
click_on "Back"
end
test "should update Teacher" do
visit teacher_url(@teacher)
click_on "Edit this teacher", match: :first
fill_in "Bio", with: @teacher.bio
fill_in "Member", with: @teacher.member_id
fill_in "Name", with: @teacher.name
fill_in "Picture", with: @teacher.picture
click_on "Update Teacher"
assert_text "Teacher was successfully updated"
click_on "Back"
end
test "should destroy Teacher" do
visit teacher_url(@teacher)
click_on "Destroy this teacher", match: :first
assert_text "Teacher was successfully destroyed"
end
end