story cleanup, form, policy
This commit is contained in:
parent
10bdfa39cd
commit
3513b2ada4
@ -1,26 +1,21 @@
|
||||
class StoriesController < ApplicationController
|
||||
before_action :set_story, only: %i[ show edit update destroy ]
|
||||
|
||||
# GET /stories
|
||||
def index
|
||||
@stories = Story.all.page params[:page]
|
||||
end
|
||||
|
||||
# GET /stories/1
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /stories/new
|
||||
def new
|
||||
@story = Story.new
|
||||
end
|
||||
|
||||
# GET /stories/1/edit
|
||||
def edit
|
||||
authorize @story
|
||||
end
|
||||
|
||||
# POST /stories
|
||||
def create
|
||||
@story = Story.new(story_params)
|
||||
@story.member = current_member
|
||||
@ -32,8 +27,8 @@ class StoriesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /stories/1
|
||||
def update
|
||||
authorize @story
|
||||
if @story.update(story_params)
|
||||
redirect_to @story, notice: "Story was successfully updated."
|
||||
else
|
||||
@ -41,8 +36,8 @@ class StoriesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /stories/1
|
||||
def destroy
|
||||
authorize @story
|
||||
@story.destroy
|
||||
redirect_to stories_url, notice: "Story was successfully destroyed."
|
||||
end
|
||||
|
@ -5,10 +5,11 @@ module ApplicationHelper
|
||||
# different template according to the amount of text
|
||||
def render_story(story)
|
||||
return "" unless story
|
||||
puts story.text.length
|
||||
text_length = story.text.length
|
||||
template = "text"
|
||||
template = "half" if text_length < 400
|
||||
template = "pic" if text_length < 200
|
||||
template = "half" if text_length < 500
|
||||
template = "pic" if text_length < 300
|
||||
render partial: "stories/#{template}" , locals: {story: story}
|
||||
end
|
||||
|
||||
|
14
app/policies/edit_own_policy.rb
Normal file
14
app/policies/edit_own_policy.rb
Normal 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
|
@ -1,4 +1,4 @@
|
||||
class StoryPolicy < ApplicationPolicy
|
||||
class StoryPolicy < EditOwnPolicy
|
||||
|
||||
def edit?
|
||||
(member == record.member) or member.admin?
|
||||
|
17
app/views/stories/_form.haml
Normal file
17
app/views/stories/_form.haml
Normal 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
|
@ -1,16 +1,5 @@
|
||||
.grid.grid-cols-3
|
||||
%div
|
||||
%div
|
||||
%h1 Editing story
|
||||
= 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"
|
||||
%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
|
||||
.flex.justify-center
|
||||
.column{class: "w-10/12 md:w-8/12 lg:w-5/12 xl:w-4/12"}
|
||||
.text-2xl.font-bold.my-4
|
||||
Edit Story
|
||||
= render 'form'
|
||||
|
@ -1,20 +1,5 @@
|
||||
|
||||
.grid.grid-cols-3
|
||||
%div
|
||||
%div
|
||||
%h1 New story
|
||||
= 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
|
||||
.flex.justify-center
|
||||
.column{class: "w-10/12 md:w-8/12 lg:w-5/12 xl:w-4/12"}
|
||||
.text-2xl.font-bold.my-4
|
||||
New Story
|
||||
= render 'form'
|
||||
|
Loading…
Reference in New Issue
Block a user