Class: Thredded::PostForm

Inherits:
Object
  • Object
show all
Defined in:
app/forms/thredded/post_form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, topic:, post: nil, post_params: {}) ⇒ PostForm

Returns a new instance of PostForm.

Parameters:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/forms/thredded/post_form.rb', line 16

def initialize(user:, topic:, post: nil, post_params: {})
  @messageboard = topic.messageboard
  @topic = topic
  @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

Instance Attribute Details

#postObject (readonly)

Returns the value of attribute post.



5
6
7
# File 'app/forms/thredded/post_form.rb', line 5

def post
  @post
end

#topicObject (readonly)

Returns the value of attribute topic.



5
6
7
# File 'app/forms/thredded/post_form.rb', line 5

def topic
  @topic
end

Class Method Details

.for_persisted(post) ⇒ Object



32
33
34
# File 'app/forms/thredded/post_form.rb', line 32

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

Instance Method Details

#preview_pathObject



40
41
42
43
44
45
46
# File 'app/forms/thredded/post_form.rb', line 40

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

#saveObject



48
49
50
51
52
53
54
# File 'app/forms/thredded/post_form.rb', line 48

def save
  return false unless @post.valid?
  was_persisted = @post.persisted?
  @post.save!
  Thredded::UserTopicReadState.touch!(@post.user.id, @post) unless was_persisted
  true
end

#submit_pathObject



36
37
38
# File 'app/forms/thredded/post_form.rb', line 36

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