Class: Thredded::PrivateTopicForm

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

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ PrivateTopicForm

Returns a new instance of PrivateTopicForm.



24
25
26
27
28
29
30
31
32
33
34
# File 'app/forms/thredded/private_topic_form.rb', line 24

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

Instance Attribute Details

#category_idsObject

Returns the value of attribute category_ids.



10
11
12
# File 'app/forms/thredded/private_topic_form.rb', line 10

def category_ids
  @category_ids
end

#contentObject

Returns the value of attribute content.



10
11
12
# File 'app/forms/thredded/private_topic_form.rb', line 10

def content
  @content
end

#lockedObject

Returns the value of attribute locked.



10
11
12
# File 'app/forms/thredded/private_topic_form.rb', line 10

def locked
  @locked
end

#paramsObject (readonly)

Returns the value of attribute params.



19
20
21
# File 'app/forms/thredded/private_topic_form.rb', line 19

def params
  @params
end

#private_topicObject

Returns the value of attribute private_topic.



10
11
12
# File 'app/forms/thredded/private_topic_form.rb', line 10

def private_topic
  @private_topic
end

#stickyObject

Returns the value of attribute sticky.



10
11
12
# File 'app/forms/thredded/private_topic_form.rb', line 10

def sticky
  @sticky
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'app/forms/thredded/private_topic_form.rb', line 10

def title
  @title
end

#userObject (readonly)

Returns the value of attribute user.



19
20
21
# File 'app/forms/thredded/private_topic_form.rb', line 19

def user
  @user
end

#user_idsObject

Returns the value of attribute user_ids.



10
11
12
# File 'app/forms/thredded/private_topic_form.rb', line 10

def user_ids
  @user_ids
end

#user_namesObject



77
78
79
80
81
82
83
84
85
# File 'app/forms/thredded/private_topic_form.rb', line 77

def user_names
  @user_names ||= Thredded.user_class.where(id: user_ids).pluck(Thredded.user_name_column).map do |name|
    if name.include?(',')
      "\"#{name}\""
    else
      name
    end
  end.join(', ')
end

Class Method Details

.model_nameObject



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

def self.model_name
  Thredded::PrivateTopic.model_name
end

Instance Method Details

#postObject



62
63
64
65
66
67
# File 'app/forms/thredded/private_topic_form.rb', line 62

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

#preview_pathObject



73
74
75
# File 'app/forms/thredded/private_topic_form.rb', line 73

def preview_path
  Thredded::UrlsHelper.preview_new_private_topic_path
end

#saveObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/forms/thredded/private_topic_form.rb', line 40

def save
  @user_ids ||= []
  @user_ids += Thredded.user_class.where(Thredded.user_name_column => parse_names(user_names)).pluck(:id)

  return false unless valid?

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

#submit_pathObject



69
70
71
# File 'app/forms/thredded/private_topic_form.rb', line 69

def submit_path
  Thredded::UrlsHelper.url_for([private_topic, only_path: true])
end