diff --git a/app/assets/images/no_image.png b/app/assets/images/no_image.png new file mode 100644 index 0000000..ef843ce Binary files /dev/null and b/app/assets/images/no_image.png differ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 503a604..4383fe5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index ce47e25..df467b7 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -3,7 +3,7 @@ class MembersController < ApplicationController # GET /members def index - @members = Member.all + @members = Member.page params[:page] end # GET /members/1 diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb new file mode 100644 index 0000000..bd9639e --- /dev/null +++ b/app/controllers/profiles_controller.rb @@ -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 diff --git a/app/controllers/teachers_controller.rb b/app/controllers/teachers_controller.rb deleted file mode 100644 index 5e7722d..0000000 --- a/app/controllers/teachers_controller.rb +++ /dev/null @@ -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 diff --git a/app/helpers/profiles_helper.rb b/app/helpers/profiles_helper.rb new file mode 100644 index 0000000..4e43050 --- /dev/null +++ b/app/helpers/profiles_helper.rb @@ -0,0 +1,2 @@ +module ProfilesHelper +end diff --git a/app/helpers/teachers_helper.rb b/app/helpers/teachers_helper.rb deleted file mode 100644 index 1015c8c..0000000 --- a/app/helpers/teachers_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module TeachersHelper -end diff --git a/app/models/member.rb b/app/models/member.rb index a7a27a4..bb9e91f 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -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" diff --git a/app/models/teacher.rb b/app/models/profile.rb similarity index 72% rename from app/models/teacher.rb rename to app/models/profile.rb index b3998bb..64d64b9 100644 --- a/app/models/teacher.rb +++ b/app/models/profile.rb @@ -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 diff --git a/app/policies/teacher_policy.rb b/app/policies/profile_policy.rb similarity index 75% rename from app/policies/teacher_policy.rb rename to app/policies/profile_policy.rb index 32fd1eb..fd30d3e 100644 --- a/app/policies/teacher_policy.rb +++ b/app/policies/profile_policy.rb @@ -1,4 +1,4 @@ -class TeacherPolicy < ApplicationPolicy +class ProfilePolicy < ApplicationPolicy def edit? (member == record.member) or member.admin? diff --git a/app/views/members/index.html.haml b/app/views/members/index.html.haml index c1e9f7f..62262a8 100644 --- a/app/views/members/index.html.haml +++ b/app/views/members/index.html.haml @@ -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 } diff --git a/app/views/members/show.html.haml b/app/views/members/show.html.haml index 2c97829..ff7a658 100644 --- a/app/views/members/show.html.haml +++ b/app/views/members/show.html.haml @@ -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| diff --git a/app/views/teachers/_form.html.haml b/app/views/profiles/_form.html.haml similarity index 65% rename from app/views/teachers/_form.html.haml rename to app/views/profiles/_form.html.haml index c445eb3..fc67cb1 100644 --- a/app/views/teachers/_form.html.haml +++ b/app/views/profiles/_form.html.haml @@ -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 diff --git a/app/views/teachers/edit.html.haml b/app/views/profiles/edit.html.haml similarity index 54% rename from app/views/teachers/edit.html.haml rename to app/views/profiles/edit.html.haml index fb59745..0e83c05 100644 --- a/app/views/teachers/edit.html.haml +++ b/app/views/profiles/edit.html.haml @@ -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' diff --git a/app/views/teachers/index.html.haml b/app/views/profiles/index.html.haml similarity index 59% rename from app/views/teachers/index.html.haml rename to app/views/profiles/index.html.haml index 85e2bff..c33eb44 100644 --- a/app/views/teachers/index.html.haml +++ b/app/views/profiles/index.html.haml @@ -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) diff --git a/app/views/teachers/new.html.haml b/app/views/profiles/new.html.haml similarity index 53% rename from app/views/teachers/new.html.haml rename to app/views/profiles/new.html.haml index 660f25c..5909f31 100644 --- a/app/views/teachers/new.html.haml +++ b/app/views/profiles/new.html.haml @@ -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' diff --git a/app/views/teachers/show.html.haml b/app/views/profiles/show.html.haml similarity index 73% rename from app/views/teachers/show.html.haml rename to app/views/profiles/show.html.haml index 16c8410..d0d1d0d 100644 --- a/app/views/teachers/show.html.haml +++ b/app/views/profiles/show.html.haml @@ -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) diff --git a/config/routes.rb b/config/routes.rb index 4afda60..e47ac11 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :teachers + resources :profiles resources :entities devise_for :members diff --git a/db/migrate/20230115120517_create_profiles.rb b/db/migrate/20230115120517_create_profiles.rb new file mode 100644 index 0000000..b796cf7 --- /dev/null +++ b/db/migrate/20230115120517_create_profiles.rb @@ -0,0 +1,17 @@ +class CreateProfiles < ActiveRecord::Migration[7.0] + def change + create_table :profiles do |t| + t.string :name , null: false + t.string :kind , null: false + t.text :bio , null: false + t.string :picture , null: false + t.references :member, null: false, foreign_key: true + + t.timestamps + end + remove_column :members, :name, :string + remove_column :members, :bio, :text + remove_column :members, :picture, :string + remove_column :members, :public, :boolean + end +end diff --git a/db/migrate/20230115120517_create_teachers.rb b/db/migrate/20230115120517_create_teachers.rb deleted file mode 100644 index 5693fcc..0000000 --- a/db/migrate/20230115120517_create_teachers.rb +++ /dev/null @@ -1,12 +0,0 @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 5e3903f..6cecaf6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -37,10 +37,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_15_120517) do end create_table "members", force: :cascade do |t| - t.string "name" - t.string "bio" - t.string "picture" - t.boolean "public" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "email", default: "", null: false @@ -56,14 +52,15 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_15_120517) do t.index ["reset_password_token"], name: "index_members_on_reset_password_token", unique: true end - create_table "teachers", force: :cascade do |t| + create_table "profiles", force: :cascade do |t| t.string "name", null: false + t.string "kind", 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" + t.index ["member_id"], name: "index_profiles_on_member_id" end create_table "thredded_categories", force: :cascade do |t| @@ -298,7 +295,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_15_120517) do end add_foreign_key "entities", "members" - add_foreign_key "teachers", "members" + add_foreign_key "profiles", "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 diff --git a/test/controllers/teachers_controller_test.rb b/test/controllers/teachers_controller_test.rb deleted file mode 100644 index 62a971c..0000000 --- a/test/controllers/teachers_controller_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -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 diff --git a/test/fixtures/teachers.yml b/test/fixtures/profiles.yml similarity index 100% rename from test/fixtures/teachers.yml rename to test/fixtures/profiles.yml diff --git a/test/models/teacher_test.rb b/test/models/teacher_test.rb index 55fa9e9..701636f 100644 --- a/test/models/teacher_test.rb +++ b/test/models/teacher_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class TeacherTest < ActiveSupport::TestCase +class ProfileTest < ActiveSupport::TestCase # test "the truth" do # assert true # end diff --git a/test/system/profiles_test.rb b/test/system/profiles_test.rb new file mode 100644 index 0000000..3908394 --- /dev/null +++ b/test/system/profiles_test.rb @@ -0,0 +1,47 @@ +require "application_system_test_case" + +class ProfilesTest < ApplicationSystemTestCase + setup do + @profile = profiles(:one) + end + + test "visiting the index" do + visit profiles_url + assert_selector "h1", text: "Profiles" + end + + test "should create profile" do + visit profiles_url + click_on "New profile" + + fill_in "Bio", with: @profile.bio + fill_in "Member", with: @profile.member_id + fill_in "Name", with: @profile.name + fill_in "Picture", with: @profile.picture + click_on "Create Profile" + + assert_text "Profile was successfully created" + click_on "Back" + end + + test "should update Profile" do + visit profile_url(@profile) + click_on "Edit this profile", match: :first + + fill_in "Bio", with: @profile.bio + fill_in "Member", with: @profile.member_id + fill_in "Name", with: @profile.name + fill_in "Picture", with: @profile.picture + click_on "Update Profile" + + assert_text "Profile was successfully updated" + click_on "Back" + end + + test "should destroy Profile" do + visit profile_url(@profile) + click_on "Destroy this profile", match: :first + + assert_text "Profile was successfully destroyed" + end +end diff --git a/test/system/teachers_test.rb b/test/system/teachers_test.rb deleted file mode 100644 index 55bffc7..0000000 --- a/test/system/teachers_test.rb +++ /dev/null @@ -1,47 +0,0 @@ -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