sarting to generalize profiles

This commit is contained in:
2023-01-16 00:17:22 +02:00
parent 485c0475b7
commit 4ffc6e3c85
26 changed files with 165 additions and 207 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -2,12 +2,17 @@ class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
include Pundit::Authorization
alias :current_user :current_member #for pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
redirect_back(fallback_location: root_path)
end
end

View File

@ -3,7 +3,7 @@ class MembersController < ApplicationController
# GET /members
def index
@members = Member.all
@members = Member.page params[:page]
end
# GET /members/1

View File

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

View File

@ -1,62 +0,0 @@
class TeachersController < ApplicationController
before_action :set_teacher, only: %i[ show edit update destroy ]
# GET /teachers
def index
@teachers = Teacher.page params[:page]
end
# GET /teachers/1
def show
end
# GET /teachers/new
def new
@teacher = Teacher.new
end
# GET /teachers/1/edit
def edit
authorize @teacher
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
authorize @teacher
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
authorize @teacher
@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 ProfilesHelper
end

View File

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

View File

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

View File

@ -1,7 +1,8 @@
class Teacher < ApplicationRecord
class Profile < ApplicationRecord
belongs_to :member
validates :name , presence: true
validates :kind , presence: true
validates :bio , presence: true
validates :picture , presence: true

View File

@ -1,4 +1,4 @@
class TeacherPolicy < ApplicationPolicy
class ProfilePolicy < ApplicationPolicy
def edit?
(member == record.member) or member.admin?

View File

@ -8,8 +8,6 @@
= 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 }

View File

@ -23,14 +23,14 @@
= 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
- if @member.profile
= link_to edit_profile_path(@member.profile) 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
Edit profile profile
-else
= link_to new_teacher_path do
= link_to new_profile_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
Create profile profile
.grid.grid-cols-3.gap-4
-@member.entities.each do |entity|

View File

@ -1,13 +1,13 @@
= simple_form_for @teacher do |f|
= simple_form_for @profile 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)")
= f.input :picture , as: :file , label: (@profile.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
= link_to 'Cancel', profiles_path

View File

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

View File

@ -1,14 +1,14 @@
= paginate @teachers
= paginate @profiles
.flex.justify-center
.grid.grid-cols-4
- @teachers.each do |teacher|
- @profiles.each do |profile|
.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")
=link_to profile do
= image_for( profile , class: "h-60 w-full object-cover")
%h3.py-5.text-2xl.bg-gray-100.text-black.font-bold.text-center
= teacher.name
= profile.name
%div.h-full
.p-5.text-center
.m-2.text-sm.leading-relaxed.line-clamp-3{ prose_classes }
= shorten markdown(teacher.bio)
= shorten markdown(profile.bio)

View File

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

View File

@ -1,10 +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"
-if @profile.picture_url
= image_tag @profile.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
= @profile.name
.mb-8.text-gray-800
.prose= markdown(@teacher.bio)
.prose= markdown(@profile.bio)