story cleanup, form, policy

This commit is contained in:
Torsten 2023-01-22 13:17:27 +02:00
parent 10bdfa39cd
commit 3513b2ada4
7 changed files with 47 additions and 46 deletions

View File

@ -1,26 +1,21 @@
class StoriesController < ApplicationController class StoriesController < ApplicationController
before_action :set_story, only: %i[ show edit update destroy ] before_action :set_story, only: %i[ show edit update destroy ]
# GET /stories
def index def index
@stories = Story.all.page params[:page] @stories = Story.all.page params[:page]
end end
# GET /stories/1
def show def show
end end
# GET /stories/new
def new def new
@story = Story.new @story = Story.new
end end
# GET /stories/1/edit
def edit def edit
authorize @story authorize @story
end end
# POST /stories
def create def create
@story = Story.new(story_params) @story = Story.new(story_params)
@story.member = current_member @story.member = current_member
@ -32,8 +27,8 @@ class StoriesController < ApplicationController
end end
end end
# PATCH/PUT /stories/1
def update def update
authorize @story
if @story.update(story_params) if @story.update(story_params)
redirect_to @story, notice: "Story was successfully updated." redirect_to @story, notice: "Story was successfully updated."
else else
@ -41,8 +36,8 @@ class StoriesController < ApplicationController
end end
end end
# DELETE /stories/1
def destroy def destroy
authorize @story
@story.destroy @story.destroy
redirect_to stories_url, notice: "Story was successfully destroyed." redirect_to stories_url, notice: "Story was successfully destroyed."
end end

View File

@ -5,10 +5,11 @@ module ApplicationHelper
# different template according to the amount of text # different template according to the amount of text
def render_story(story) def render_story(story)
return "" unless story return "" unless story
puts story.text.length
text_length = story.text.length text_length = story.text.length
template = "text" template = "text"
template = "half" if text_length < 400 template = "half" if text_length < 500
template = "pic" if text_length < 200 template = "pic" if text_length < 300
render partial: "stories/#{template}" , locals: {story: story} render partial: "stories/#{template}" , locals: {story: story}
end end

View File

@ -0,0 +1,14 @@
# allows to edit/detroy own data
# which can be viewed by anyone
class EditOwnPolicy < ApplicationPolicy
def edit?
return true if member.admin?
owner?
end
def owner?
member == record.member
end
alias :update? :edit?
alias :destroy? :edit?
end

View File

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

View File

@ -0,0 +1,17 @@
%div
Story layout changes with the amount of text.
For short lext a wide picture is best. Otherwise square, and for
longer text a high picture also works.
= simple_form_for @story do |f|
= f.error_notification
= f.input :picture , as: :file , label: (@story.picture.blank? ? "Add picture" : "Change picture")
= f.input :header
= f.input :text , input_html: {rows: rows(@story.text)}
= f.input :happened , wrapper_class: "flex mt-4 align-center"
.flex.justify-between.mt-6
%button.bg-cyan-200.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
= f.submit 'Save'
= link_to @story do
%button.ml-20.rounded-lg.px-4.py-3.text-md.font-medium.border.border-gray-400
Back

View File

@ -1,16 +1,5 @@
.grid.grid-cols-3 .flex.justify-center
%div .column{class: "w-10/12 md:w-8/12 lg:w-5/12 xl:w-4/12"}
%div .text-2xl.font-bold.my-4
%h1 Editing story Edit Story
= simple_form_for @story do |f| = render 'form'
= f.error_notification
= f.input :picture , as: :file , label: (@story.picture.blank? ? "Add picture" : "Change picture")
= f.input :header
= f.input :text , input_html: {rows: rows(@story.text)}
= f.input :happened , wrapper_class: "flex mt-4 align-center"
%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
%div

View File

@ -1,20 +1,5 @@
.flex.justify-center
.grid.grid-cols-3 .column{class: "w-10/12 md:w-8/12 lg:w-5/12 xl:w-4/12"}
%div .text-2xl.font-bold.my-4
%div New Story
%h1 New story = render 'form'
= form_for @story do |f|
- if @story.errors.any?
#error_explanation
%h2= "#{pluralize(@story.errors.count, "error")} prohibited this story from being saved:"
%ul
- @story.errors.full_messages.each do |message|
%li= message
= f.input :picture , as: :file
= f.input :header
= f.input :text
= f.input :happened , class: "flex"
= f.submit 'Save'
= link_to 'Back', stories_path
%div