Class: TopicCreator

Inherits:
Object
  • Object
show all
Includes:
HasErrors
Defined in:
lib/topic_creator.rb

Instance Attribute Summary collapse

Attributes included from HasErrors

#conflict, #errors, #forbidden, #not_found

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasErrors

#add_error, #add_errors_from, #rollback_from_errors!, #rollback_with!, #validate_child

Constructor Details

#initialize(user, guardian, opts) ⇒ TopicCreator

Returns a new instance of TopicCreator.



12
13
14
15
16
17
# File 'lib/topic_creator.rb', line 12

def initialize(user, guardian, opts)
  @user = user
  @guardian = guardian
  @opts = opts
  @added_users = []
end

Instance Attribute Details

#guardianObject (readonly)

Returns the value of attribute guardian.



4
5
6
# File 'lib/topic_creator.rb', line 4

def guardian
  @guardian
end

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/topic_creator.rb', line 4

def opts
  @opts
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/topic_creator.rb', line 4

def user
  @user
end

Class Method Details

.create(user, guardian, opts) ⇒ Object



8
9
10
# File 'lib/topic_creator.rb', line 8

def self.create(user, guardian, opts)
  self.new(user, guardian, opts).create
end

Instance Method Details

#createObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/topic_creator.rb', line 44

def create
  topic = Topic.new(setup_topic_params)

  validate_visibility!(topic)
  setup_tags(topic)

  if fields = @opts[:custom_fields]
    topic.custom_fields.merge!(fields)
  end

  DiscourseEvent.trigger(:before_create_topic, topic, self)

  setup_auto_close_time(topic)
  process_private_message(topic)
  save_topic(topic)
  create_warning(topic)
  watch_topic(topic)
  create_shared_draft(topic)
  UserActionManager.topic_created(topic)

  topic
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/topic_creator.rb', line 19

def valid?
  topic = Topic.new(setup_topic_params)
  # validate? will clear the error hash
  # so we fire the validation event after
  # this allows us to add errors
  valid = topic.valid?

  validate_visibility(topic)

  category = find_category
  if category.present? && guardian.can_tag?(topic)
    tags = @opts[:tags].presence || []

    # adds topic.errors
    DiscourseTagging.validate_category_tags(guardian, topic, category, tags)
  end

  DiscourseEvent.trigger(:after_validate_topic, topic, self)
  valid &&= topic.errors.empty?

  add_errors_from(topic) unless valid

  valid
end