Sha256: 8613a577649c8a44003ffa4c465135db0a341f5066b55a362cd53a4f090003cd

Contents?: true

Size: 1.04 KB

Versions: 18

Compression:

Stored size: 1.04 KB

Contents

require_dependency "my_forum/application_controller"

module MyForum
  class PostsController < ApplicationController
    before_filter :find_topic
    before_filter :find_forum

    def create
      post = @topic.posts.build(post_params)
      post.user = current_user
      post.save

      process_attachments(post)

      last_page = @topic.posts.count / Post::PER_PAGE
      last_page = 1 if last_page == 0
      redirect_to forum_topic_path(@forum, @topic, page: last_page)
    end

    private

    def process_attachments(post)
      return unless matches = post_params.to_s.match(/\[attachment=([0-9]+)\]/i)

      matches.captures.map(&:to_i).each do |attachment_id|
        attachment = Attachment.where(id: attachment_id).first
        attachment.update(post: post) if attachment and post.user == attachment.user
      end
    end

    def find_topic
      @topic = Topic.find(params[:topic_id])
    end

    def find_forum
      @forum = Forum.find(params[:forum_id])
    end

    def post_params
      params.require(:post).permit(:text)
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
my_forum-0.0.1.beta21 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta20 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta19 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta18 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta17 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta16 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta15 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta14 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta13 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta12 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta11 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta10 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta9 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta8 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta7 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta5 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta4 app/controllers/my_forum/posts_controller.rb
my_forum-0.0.1.beta3 app/controllers/my_forum/posts_controller.rb