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.



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

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.



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

def category_ids
  @category_ids
end

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#lockedObject

Returns the value of attribute locked.



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

def locked
  @locked
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#private_topicObject

Returns the value of attribute private_topic.



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

def private_topic
  @private_topic
end

#stickyObject

Returns the value of attribute sticky.



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

def sticky
  @sticky
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

#user_idsObject

Returns the value of attribute user_ids.



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

def user_ids
  @user_ids
end

#user_namesObject



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

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



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

def self.model_name
  Thredded::PrivateTopic.model_name
end

Instance Method Details

#postObject



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

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

#preview_pathObject



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

def preview_path
  Thredded::UrlsHelper.preview_new_private_topic_path
end

#saveObject



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

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



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

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