diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index fdf670d..a666c9f 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8487591..814de4c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/policies/edit_own_policy.rb b/app/policies/edit_own_policy.rb new file mode 100644 index 0000000..0882e81 --- /dev/null +++ b/app/policies/edit_own_policy.rb @@ -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 diff --git a/app/policies/story_policy.rb b/app/policies/story_policy.rb index 53b3bb4..fb142fb 100644 --- a/app/policies/story_policy.rb +++ b/app/policies/story_policy.rb @@ -1,4 +1,4 @@ -class StoryPolicy < ApplicationPolicy +class StoryPolicy < EditOwnPolicy def edit? (member == record.member) or member.admin? diff --git a/app/views/stories/_form.haml b/app/views/stories/_form.haml new file mode 100644 index 0000000..19d1fda --- /dev/null +++ b/app/views/stories/_form.haml @@ -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 diff --git a/app/views/stories/edit.html.haml b/app/views/stories/edit.html.haml index ca3044e..b1694ec 100644 --- a/app/views/stories/edit.html.haml +++ b/app/views/stories/edit.html.haml @@ -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' diff --git a/app/views/stories/new.html.haml b/app/views/stories/new.html.haml index 80c3028..bb00576 100644 --- a/app/views/stories/new.html.haml +++ b/app/views/stories/new.html.haml @@ -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'