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


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


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

def params
  @params
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


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


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

def user_ids
  @user_ids
end

#user_namesObject


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

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


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

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

#preview_pathObject


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

def preview_path
  Thredded::UrlsHelper.preview_new_private_topic_path
end

#private_topicObject


55
56
57
58
59
60
61
62
# File 'app/forms/thredded/private_topic_form.rb', line 55

def private_topic
  @private_topic ||= Thredded::PrivateTopic.new(
    title: title,
    users: private_users,
    user: non_null_user,
    last_user: non_null_user
  )
end

#saveObject


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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
    new_topic = !private_topic.persisted?
    private_topic.save!
    post.save!
    Thredded::UserPrivateTopicReadState.read_on_first_post!(user, post) if new_topic
  end
  true
end

#submit_pathObject


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

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