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

/app/controllers/admin/links_controller.rb

https://github.com/seaneshbaugh/portfolio
Ruby | 85 lines | 57 code | 27 blank | 1 comment | 2 complexity | 2fd6486d620ecc1dc7051e175563136e MD5 | raw file
  1. # frozen_string_literal: true
  2. module Admin
  3. class LinksController < AdminController
  4. def index
  5. authorize Link
  6. @search = Link.ransack(params[:q])
  7. @links = @search.result.page(params[:page]).per(25).reverse_chronological
  8. end
  9. def show
  10. @link = find_link
  11. authorize @link
  12. end
  13. def new
  14. authorize Link
  15. @link = Link.new
  16. end
  17. def edit
  18. @link = find_link
  19. authorize @link
  20. end
  21. def create
  22. authorize Link
  23. @link = Link.new(link_params)
  24. if @link.save
  25. flash[:success] = t('.success')
  26. redirect_to admin_link_url(@link), status: :see_other
  27. else
  28. flash[:error] = helpers.error_messages_for(@link)
  29. render 'new', status: :unprocessable_entity
  30. end
  31. end
  32. def update
  33. @link = find_link
  34. authorize @link
  35. if @link.update(link_params)
  36. flash[:success] = t('.success')
  37. redirect_to edit_admin_link_url(@link), status: :see_other
  38. else
  39. flash[:error] = helpers.error_messages_for(@link)
  40. render 'edit', status: :unprocessable_entity
  41. end
  42. end
  43. def destroy
  44. @link = find_link
  45. authorize @link
  46. @link.destroy
  47. flash[:success] = t('.success')
  48. redirect_to admin_links_url, status: :see_other
  49. end
  50. private
  51. def find_link
  52. Link.find(params[:id])
  53. end
  54. def link_params
  55. params.require(:link).permit(:text, :url, :description, :published_at, :visible, :tag_list)
  56. end
  57. end
  58. end