module Formol class TopicsController < ApplicationController include Controllers::Nested::HasParentForum respond_to :html def show formol_authorize!(current_formol_user, :read_topic) topic.view! topic.track_for_user!(current_formol_user) @posts = topic.posts.ready_for_listing.page(params[:page]).per_page(per_page) respond_with(topic) end def new formol_authorize!(current_formol_user, :create_topic) topic.posts.build(:register_user_as_subscriber => true) unless topic.poll topic.build_poll topic.poll.options.build end respond_with(topic) end def create formol_authorize!(current_formol_user, :create_topic) topic.user = current_formol_user if topic.save respond_with(topic, :location => [forum, topic]) else unless topic.poll topic.build_poll topic.poll.options.build end respond_with(topic, :status => :unprocessable_entity) do |format| format.html { render :new } end end end def edit formol_authorize!(current_formol_user, :edit_topic, topic) respond_with(topic) end def update formol_authorize!(current_formol_user, :edit_topic, topic) if topic.update_attributes(params[:topic]) respond_with(topic, :location => [forum, topic]) else respond_with(topic) do |format| format.html { render :edit } end end end def destroy formol_authorize!(current_formol_user, :destroy_topic) topic.destroy respond_with(topic, :location => forum) end protected def topic @topic ||= if params[:id].present? if request.get? forum.topics.ready_for_breadcrumb.includes(:poll => { :options => :votes }).find(params[:id]) else # never eager load anything for update (brokes validates on associations) forum.topics.find(params[:id]) end else forum.topics.new(params[:topic]) end end end end