starting with events, tied to profiles (not accounts)

This commit is contained in:
2023-01-16 14:49:45 +02:00
parent 63299d4464
commit cb54951037
19 changed files with 307 additions and 11 deletions

View File

@ -0,0 +1,65 @@
class EventsController < ApplicationController
before_action :set_event, only: %i[ show edit update destroy ]
# GET /events
def index
@events = Event.all
end
# GET /events/1
def show
end
# GET /events/new
def new
kind = params[:profile]
kind = Profile.types.first unless Profile.types.include?(kind)
profile = current_member.profile(kind)
if(profile)
@event = Event.new profile: profile
else
redirect_to member_path(current_member), notice: "No such profile #{kind}."
end
end
# GET /events/1/edit
def edit
end
# POST /events
def create
@event = Event.new(event_params)
if @event.save
redirect_to @event, notice: "Event was successfully created."
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /events/1
def update
if @event.update(event_params)
redirect_to @event, notice: "Event was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /events/1
def destroy
@event.destroy
redirect_to events_url, notice: "Event was successfully destroyed."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
# Only allow a list of trusted parameters through.
def event_params
params.require(:event).permit(:name, :text, :start_date, :end_date, :profile_id, :picture)
end
end

View File

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

11
app/models/event.rb Normal file
View File

@ -0,0 +1,11 @@
class Event < ApplicationRecord
belongs_to :profile
validates :name , presence: true
validates :text , presence: true
validates :start_date , presence: true
validates :picture , presence: true
mount_uploader :picture, PictureUploader
end

View File

@ -2,7 +2,11 @@
# which can be viewed by anyone
class EditOwnPolicy < ApplicationPolicy
def edit?
(member == record.member) or member.admin?
return true member.admin?
owner?
end
def owner?
member == record.member
end
alias :update? :edit?
alias :destroy? :edit?

View File

@ -0,0 +1,13 @@
class EventPolicy < EditOwnPolicy
def owner?
member == record.profile.member
end
class Scope < Scope
# NOTE: Be explicit about which records you allow access to!
# def resolve
# scope.all
# end
end
end

View File

@ -0,0 +1,14 @@
= simple_form_for @event do |f|
= f.error_notification
= f.input :name
= f.input :text
.grid.grid-cols-2.gap-10
= f.input :start_date
= f.input :end_date
= f.input :profile_id , as: :hidden
= f.input :picture , as: :file
%button.mt-6.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
= f.submit 'Save'
%button.ml-20.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
= link_to 'Back', @story

View File

@ -0,0 +1,6 @@
.flex.justify-center
.column
.text-xl.m-4
Editing event
= render 'form'

View File

@ -0,0 +1,31 @@
%h1 Listing events
%table
%thead
%tr
%th Name
%th Text
%th Start date
%th End date
%th Profile
%th Picture
%th
%th
%th
%tbody
- @events.each do |event|
%tr
%td= event.name
%td= event.text
%td= event.start_date
%td= event.end_date
%td= event.profile
%td= event.picture
%td= link_to 'Show', event
%td= link_to 'Edit', edit_event_path(event)
%td= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' }
%br
= link_to 'New Event', new_event_path

View File

@ -0,0 +1,6 @@
.flex.justify-center
.column
.text-xl.m-4
New event
= render 'form'

View File

@ -0,0 +1,24 @@
%p#notice= notice
%p
%b Name:
= @event.name
%p
%b Text:
= @event.text
%p
%b Start date:
= @event.start_date
%p
%b End date:
= @event.end_date
%p
%b Profile:
= @event.profile
%p
%b Picture:
= @event.picture
= link_to 'Edit', edit_event_path(@event)
\|
= link_to 'Back', events_path

View File

@ -1,11 +1,5 @@
= form_for @member do |f|
.flex.flex-col
- if @member.errors.any?
#error_explanation
%h2= "#{pluralize(@member.errors.count, "error")} prohibited this member from being saved:"
%ul
- @member.errors.full_messages.each do |message|
%li= message
= simple_form_for @member do |f|
= f.error_notification
.grid.grid-cols-2.m-20.gap-10
.field

View File

@ -13,7 +13,7 @@
.grid.grid-cols-2.mx-20
.first
- Profile.types.each do |kind|
.grid.grid-cols-2
.grid.grid-cols-3
- if profile = @member.profile(kind)
%div
=profile.Kind
@ -23,12 +23,17 @@
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
Edit
=profile.Kind
= link_to new_event_path(profile: profile.kind) do
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
New #{profile.Kind} Event
-else
%div Create new #{kind.capitalize} profile as
= link_to new_profile_path(kind: kind) do
%button.bg-cyan-200.mr-3.inline-block.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
New
= kind.capitalize
%div Create profile first, then events
.grid.grid-cols-6.gap-4.mt-10.mx-10