Class: TopicCreator
Instance Attribute Summary collapse
-
#guardian ⇒ Object
readonly
Returns the value of attribute guardian.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Attributes included from HasErrors
#conflict, #forbidden, #not_found
Class Method Summary collapse
Instance Method Summary collapse
- #create ⇒ Object
-
#initialize(user, guardian, opts) ⇒ TopicCreator
constructor
A new instance of TopicCreator.
- #valid? ⇒ Boolean
Methods included from HasErrors
#add_error, #add_errors_from, #errors, #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
#guardian ⇒ Object (readonly)
Returns the value of attribute guardian.
4 5 6 |
# File 'lib/topic_creator.rb', line 4 def guardian @guardian end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
4 5 6 |
# File 'lib/topic_creator.rb', line 4 def opts @opts end |
#user ⇒ Object (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
#create ⇒ Object
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) (topic) if fields = @opts[:custom_fields] topic.custom_fields = fields end DiscourseEvent.trigger(:before_create_topic, topic, self) setup_auto_close_time(topic) (topic) save_topic(topic) create_warning(topic) watch_topic(topic) create_shared_draft(topic) UserActionManager.topic_created(topic) topic end |
#valid? ⇒ 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) = @opts[:tags].presence || [] # adds topic.errors DiscourseTagging.(guardian, topic, category, ) end DiscourseEvent.trigger(:after_validate_topic, topic, self) valid &&= topic.errors.empty? add_errors_from(topic) unless valid valid end |