Sha256: 78a8bb6d5cb8b7b59f42b5e389a520212fc5eb57c785b74ae2595154d7b216c3

Contents?: true

Size: 1.43 KB

Versions: 18

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Thredded
  class PostForm
    attr_reader :post, :topic
    delegate :id,
             :persisted?,
             :content,
             :content=,
             to: :@post

    # @param user [Thredded.user_class]
    # @param topic [Topic]
    # @param post [PrivatePost]
    # @param post_params [Hash]
    def initialize(user:, topic:, post: nil, post_params: {})
      @messageboard = topic.messageboard
      @topic = topic
      @post = post ? post : topic.posts.build
      user ||= Thredded::NullUser.new

      if post_params.include?(:quote_post)
        post_params[:content] =
          Thredded::ContentFormatter.quote_content(post_params.delete(:quote_post).content)
      end
      @post.attributes = post_params.merge(
        user: (user unless user.thredded_anonymous?),
        messageboard: topic.messageboard
      )
    end

    def self.for_persisted(post)
      new(user: post.user, topic: post.postable, post: post)
    end

    def submit_path
      Thredded::UrlsHelper.url_for([@messageboard, @topic, @post, only_path: true])
    end

    def preview_path
      if @post.persisted?
        Thredded::UrlsHelper.messageboard_topic_post_preview_path(@messageboard, @topic, @post)
      else
        Thredded::UrlsHelper.preview_new_messageboard_topic_post_path(@messageboard, @topic)
      end
    end

    def save
      return false unless @post.valid?
      @post.save!
      true
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
threddedDANIEL-0.14.5 app/forms/thredded/post_form.rb
thredded-0.14.4 app/forms/thredded/post_form.rb
thredded-0.14.3 app/forms/thredded/post_form.rb
thredded-0.14.2 app/forms/thredded/post_form.rb
thredded-0.14.1 app/forms/thredded/post_form.rb
thredded-0.14.0 app/forms/thredded/post_form.rb
thredded-0.13.8 app/forms/thredded/post_form.rb
thredded-0.13.7 app/forms/thredded/post_form.rb
thredded-0.13.6 app/forms/thredded/post_form.rb
thredded-0.13.5 app/forms/thredded/post_form.rb
thredded-0.13.4 app/forms/thredded/post_form.rb
thredded-0.13.3 app/forms/thredded/post_form.rb
thredded-0.13.2 app/forms/thredded/post_form.rb
thredded-0.13.1 app/forms/thredded/post_form.rb
thredded-0.13.0 app/forms/thredded/post_form.rb
thredded-0.12.4 app/forms/thredded/post_form.rb
thredded-0.12.3 app/forms/thredded/post_form.rb
thredded-0.12.2 app/forms/thredded/post_form.rb