Class: PostMover

Inherits:
Object
  • Object
show all
Defined in:
app/models/post_mover.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_topic, user, post_ids, move_to_pm: false) ⇒ PostMover

Returns a new instance of PostMover.



10
11
12
13
14
15
# File 'app/models/post_mover.rb', line 10

def initialize(original_topic, user, post_ids, move_to_pm: false)
  @original_topic = original_topic
  @user = user
  @post_ids = post_ids
  @move_to_pm = move_to_pm
end

Instance Attribute Details

#destination_topicObject (readonly)

Returns the value of attribute destination_topic.



4
5
6
# File 'app/models/post_mover.rb', line 4

def destination_topic
  @destination_topic
end

#original_topicObject (readonly)

Returns the value of attribute original_topic.



4
5
6
# File 'app/models/post_mover.rb', line 4

def original_topic
  @original_topic
end

#post_idsObject (readonly)

Returns the value of attribute post_ids.



4
5
6
# File 'app/models/post_mover.rb', line 4

def post_ids
  @post_ids
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'app/models/post_mover.rb', line 4

def user
  @user
end

Class Method Details

.move_typesObject



6
7
8
# File 'app/models/post_mover.rb', line 6

def self.move_types
  @move_types ||= Enum.new(:new_topic, :existing_topic)
end

Instance Method Details

#to_new_topic(title, category_id = nil, tags = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/post_mover.rb', line 33

def to_new_topic(title, category_id = nil, tags = nil)
  @move_type = PostMover.move_types[:new_topic]

  post = Post.find_by(id: post_ids.first)
  raise Discourse::InvalidParameters unless post
  archetype = @move_to_pm ? Archetype.private_message : Archetype.default

  topic =
    Topic.transaction do
      new_topic =
        Topic.create!(
          user: post.user,
          title: title,
          category_id: category_id,
          created_at: post.created_at,
          archetype: archetype,
        )
      DiscourseTagging.tag_topic_by_names(new_topic, Guardian.new(user), tags)
      move_posts_to new_topic
      watch_new_topic
      update_topic_excerpt new_topic
      new_topic
    end
  enqueue_jobs(topic)
  topic
end

#to_topic(id, participants: nil, chronological_order: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/post_mover.rb', line 17

def to_topic(id, participants: nil, chronological_order: false)
  @move_type = PostMover.move_types[:existing_topic]
  @chronological_order = chronological_order

  topic = Topic.find_by_id(id)
  if topic.archetype != @original_topic.archetype &&
       [@original_topic.archetype, topic.archetype].include?(Archetype.private_message)
    raise Discourse::InvalidParameters
  end

  Topic.transaction { move_posts_to topic }
  add_allowed_users(participants) if participants.present? && @move_to_pm
  enqueue_jobs(topic)
  topic
end