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.



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

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_ids ⇒ Object

Returns the value of attribute category_ids.



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

def category_ids
  @category_ids
end

#content ⇒ Object

Returns the value of attribute content.



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

def content
  @content
end

#locked ⇒ Object

Returns the value of attribute locked.



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

def locked
  @locked
end

#messageboard ⇒ Object (readonly)

Returns the value of attribute messageboard.



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

def messageboard
  @messageboard
end

#sticky ⇒ Object

Returns the value of attribute sticky.



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

def sticky
  @sticky
end

#title ⇒ Object

Returns the value of attribute title.



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

def title
  @title
end

#topic ⇒ Object

Returns the value of attribute topic.



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

def topic
  @topic
end

#user ⇒ Object (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.model_name ⇒ Object



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

def self.model_name
  Thredded::Topic.model_name
end

Instance Method Details

#categories ⇒ Object



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

def categories
  topic.messageboard.categories
end

#category_options ⇒ Object



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

def category_options
  topic.messageboard.decorate.category_options
end

#filter ⇒ Object



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

def filter
  topic.messageboard.filter
end

#post ⇒ Object



57
58
59
60
61
62
63
64
# File 'app/forms/thredded/topic_form.rb', line 57

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

#save ⇒ Object



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

def save
  return false unless valid?

  ActiveRecord::Base.transaction do
    topic.save!
    post.save!
  end
  true
end