PageRenderTime 1215ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/admin/posts_controller.rb

https://github.com/seaneshbaugh/portfolio
Ruby | 83 lines | 56 code | 26 blank | 1 comment | 4 complexity | e5d80ac48448c7161aa07afc22261912 MD5 | raw file
  1. # frozen_string_literal: true
  2. module Admin
  3. class PostsController < AdminController
  4. before_action :set_post, only: %i[show edit update destroy]
  5. def index
  6. authorize Post
  7. @search = Post.ransack(params[:q])
  8. @posts = @search.result.page(params[:page]).per(25).reverse_chronological
  9. end
  10. def show
  11. authorize @post
  12. end
  13. def new
  14. authorize Post
  15. @post = Post.new
  16. end
  17. def edit
  18. authorize @post
  19. end
  20. def create
  21. authorize Post
  22. @post = Post.new(post_params)
  23. @post.user = current_user if @post.user.nil?
  24. if @post.save
  25. flash[:success] = t('.success')
  26. redirect_to admin_post_url(@post), status: :see_other
  27. else
  28. flash[:error] = helpers.error_messages_for(@post)
  29. render 'new', status: :unprocessable_entity
  30. end
  31. end
  32. def update
  33. authorize @post
  34. if @post.update(post_params)
  35. flash[:success] = t('.success')
  36. redirect_to edit_admin_post_url(@post), status: :see_other
  37. else
  38. flash[:error] = helpers.error_messages_for(@post)
  39. render 'edit', status: :unprocessable_entity
  40. end
  41. end
  42. def destroy
  43. authorize @post
  44. @post.destroy
  45. flash[:success] = t('.success')
  46. redirect_to admin_posts_url, status: :see_other
  47. end
  48. private
  49. def set_post
  50. @post = Post.friendly.find(params[:id])
  51. raise ActiveRecord::RecordNotFound if @post.nil?
  52. end
  53. def post_params
  54. params.require(:post).permit(:user_id, :title, :body, :style, :script, :meta_description, :tag_list, :visible)
  55. end
  56. end
  57. end