Class: Thredded::TopicForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/forms/thredded/topic_form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ TopicForm

Returns a new instance of TopicForm.



11
12
13
14
15
16
17
18
19
# File 'app/forms/thredded/topic_form.rb', line 11

def initialize(params = {})
  @title = params[:title]
  @category_ids = params[:category_ids]
  @locked = params[:locked] || false
  @sticky = params[:sticky] || false
  @content = params[:content]
  @user = params[:user] || fail('user is required')
  @messageboard = params[:messageboard]
end

Instance Attribute Details

#category_idsObject

Returns the value of attribute category_ids.



6
7
8
# File 'app/forms/thredded/topic_form.rb', line 6

def category_ids
  @category_ids
end

#contentObject

Returns the value of attribute content.



6
7
8
# File 'app/forms/thredded/topic_form.rb', line 6

def content
  @content
end

#lockedObject

Returns the value of attribute locked.



6
7
8
# File 'app/forms/thredded/topic_form.rb', line 6

def locked
  @locked
end

#messageboardObject (readonly)

Returns the value of attribute messageboard.



7
8
9
# File 'app/forms/thredded/topic_form.rb', line 7

def messageboard
  @messageboard
end

#stickyObject

Returns the value of attribute sticky.



6
7
8
# File 'app/forms/thredded/topic_form.rb', line 6

def sticky
  @sticky
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'app/forms/thredded/topic_form.rb', line 6

def title
  @title
end

#topicObject

Returns the value of attribute topic.



6
7
8
# File 'app/forms/thredded/topic_form.rb', line 6

def topic
  @topic
end

#userObject (readonly)

Returns the value of attribute user.



7
8
9
# File 'app/forms/thredded/topic_form.rb', line 7

def user
  @user
end

Class Method Details

.model_nameObject



21
22
23
# File 'app/forms/thredded/topic_form.rb', line 21

def self.model_name
  Thredded::Topic.model_name
end

Instance Method Details

#categoriesObject



25
26
27
# File 'app/forms/thredded/topic_form.rb', line 25

def categories
  topic.messageboard.categories
end

#category_optionsObject



29
30
31
# File 'app/forms/thredded/topic_form.rb', line 29

def category_options
  categories.map { |cat| [cat.name, cat.id] }
end

#postObject



54
55
56
57
58
59
60
# File 'app/forms/thredded/topic_form.rb', line 54

def post
  @post ||= topic.posts.build(
    content: content,
    user: non_null_user,
    messageboard: messageboard
  )
end

#saveObject



33
34
35
36
37
38
39
40
41
42
# File 'app/forms/thredded/topic_form.rb', line 33

def save
  return false unless valid?

  ActiveRecord::Base.transaction do
    topic.save!
    post.save!
    UserTopicReadState.read_on_first_post!(user, topic) if topic.previous_changes.include?(:id)
  end
  true
end