Class: Thredded::PrivatePostForm

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PrivatePostForm.

Parameters:



16
17
18
19
20
21
22
23
24
25
26
# File 'app/forms/thredded/private_post_form.rb', line 16

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

Instance Attribute Details

#postObject (readonly)

Returns the value of attribute post.



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

def post
  @post
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

Class Method Details

.for_persisted(post) ⇒ Object



28
29
30
# File 'app/forms/thredded/private_post_form.rb', line 28

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

Instance Method Details

#preview_pathObject



36
37
38
39
40
41
42
# File 'app/forms/thredded/private_post_form.rb', line 36

def preview_path
  if @post.persisted?
    Thredded::UrlsHelper.private_topic_private_post_preview_path(@topic, @post)
  else
    Thredded::UrlsHelper.preview_new_private_topic_private_post_path(@topic)
  end
end

#saveObject



44
45
46
47
48
49
50
# File 'app/forms/thredded/private_post_form.rb', line 44

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

#submit_pathObject



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

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