/app/controllers/admin/forums_controller.rb

https://github.com/captproton/rboard · Ruby · 126 lines · 92 code · 18 blank · 16 comment · 6 complexity · 1424e257e37f2d8022f7b102ef42b3ee MD5 · raw file

  1. class Admin::ForumsController < Admin::ApplicationController
  2. before_filter :store_location, :only => [:index, :show]
  3. before_filter :find_forum, :except => [:new, :create, :index]
  4. before_filter :find_category
  5. # Shows all top-level forums.
  6. def index
  7. @forums = Forum.without_parent.regardless_of_active
  8. end
  9. # Initializes a new forum.
  10. def new
  11. @forum = if @category
  12. @category.forums.build
  13. else
  14. Forum.new
  15. end
  16. find_forums
  17. end
  18. # Creates a new forum.
  19. def create
  20. @forum = if @category
  21. @category.forums.build(params[:forum])
  22. else
  23. Forum.new(params[:forum])
  24. end
  25. if @forum.save
  26. flash[:notice] = t(:created, :thing => "forum")
  27. redirect
  28. else
  29. flash[:notice] = t(:not_created, :thing => "forum")
  30. find_forums
  31. render :action => "new"
  32. end
  33. end
  34. # Edits a forum.
  35. def edit
  36. # We do this so we can't make a forum a sub of itself, or any of its descendants...
  37. # As this would cause circular references which just aren't cool.
  38. find_forums
  39. end
  40. # Updates a forum.
  41. def update
  42. if @forum.update_attributes(params[:forum])
  43. flash[:notice] = t(:updated, :thing => "forum")
  44. redirect
  45. else
  46. flash[:notice] = t(:not_updated, :thing => "forum")
  47. find_forums
  48. render :action => "edit"
  49. end
  50. end
  51. # Deletes a forum.
  52. def destroy
  53. @forum.destroy
  54. flash[:notice] = t(:deleted, :thing => "forum")
  55. redirect
  56. end
  57. # Moves a forum one space up using an acts_as_list provided method.
  58. def move_up
  59. @forum.move_higher
  60. flash[:notice] = t(:moved_higher, :thing => "Forum")
  61. redirect
  62. end
  63. # Moves a forum one space down using an acts_as_list provided method.
  64. def move_down
  65. @forum.move_lower
  66. flash[:notice] = t(:moved_lower, :thing => "Forum")
  67. redirect
  68. end
  69. # Moves a forum to the top using an acts_as_list provided method.
  70. def move_to_top
  71. @forum.move_to_top
  72. flash[:notice] = t(:moved_to_top, :thing => "Forum")
  73. redirect
  74. end
  75. # Moves a forum to the bottom using an acts_as_list helper.
  76. def move_to_bottom
  77. @forum.move_to_bottom
  78. flash[:notice] = t(:moved_to_bottom, :thing => "Forum")
  79. redirect
  80. end
  81. private
  82. # Calls redirect_back_or_default to the index action.
  83. # Got tired of writing it all out for the move_* actions
  84. def redirect
  85. redirect_back_or_default(admin_forums_path)
  86. end
  87. def find_category
  88. @category = Category.find(params[:category_id]) if params[:category_id]
  89. end
  90. # Find a forum. Most of the actions in this controller need a forum object.
  91. def find_forum
  92. @forum = Forum.regardless_of_active.find(params[:id]) unless params[:id].nil?
  93. rescue ActiveRecord::RecordNotFound
  94. not_found
  95. end
  96. def find_forums
  97. @forums, @categories = if @category
  98. [@category.forums.regardless_of_active.find(:all, :order => "title ASC"), []]
  99. else
  100. [Forum.regardless_of_active.find(:all, :order => "title ASC"), Category.find(:all, :order => "name asc")]
  101. end
  102. end
  103. # Called when the forum could not be found.
  104. def not_found
  105. flash[:notice] = t(:not_found, :thing => "forum")
  106. redirect
  107. end
  108. end