Sha256: ac82034e8251be16b07de5f608c22f8b256ae4c71c1e6a2132a30e384083e4fc

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true
module Thredded
  class MessageboardsController < Thredded::ApplicationController
    def index
      @messageboards = thredded_current_user.thredded_can_read_messageboards
    end

    def new
      @messageboard = Messageboard.new
      authorize_creating @messageboard
    end

    def create
      @messageboard = Messageboard.new(messageboard_params)
      authorize_creating @messageboard

      if signed_in? && @messageboard.save
        Topic.transaction do
          @topic = Topic.create!(topic_params)
          @post = Post.create!(post_params)
        end

        redirect_to root_path
      else
        show_sign_in_error unless signed_in?
        render action: :new
      end
    end

    private

    def show_sign_in_error
      flash.now[:error] = 'You are not signed in. Sign in or create an account before creating your messageboard.'
    end

    def messageboard_params
      params
        .require(:messageboard)
        .permit(:description, :name, :posting_permission, :security)
    end

    def topic_params
      {
        messageboard: @messageboard,
        user: thredded_current_user,
        last_user: thredded_current_user,
        title: "Welcome to your messageboard's very first thread",
      }
    end

    def post_params
      {
        messageboard: @messageboard,
        postable: @topic,
        content: "There's not a whole lot here for now.",
        ip: request.ip,
        user: thredded_current_user,
      }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thredded-0.3.2 app/controllers/thredded/messageboards_controller.rb
thredded-0.3.1 app/controllers/thredded/messageboards_controller.rb
thredded-0.3.0 app/controllers/thredded/messageboards_controller.rb