summaryrefslogtreecommitdiff
path: root/app/services/notes/build_service.rb
blob: 7d5dca5282ce305a664621514450a0ab8fb62e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Notes
  class BuildService < ::BaseService
    def execute
      in_reply_to_discussion_id = params.delete(:in_reply_to_discussion_id)
      new_discussion = params.delete(:new_discussion)

      if project && in_reply_to_discussion_id.present?
        discussion = project.notes.find_discussion(in_reply_to_discussion_id)

        unless discussion
          note = Note.new
          note.errors.add(:base, 'Discussion to reply to cannot be found')
          return note
        end

        params.merge!(discussion.reply_attributes)
      elsif new_discussion
        # TODO: Remove when we use a selectbox instead of a submit button
        params[:type] = DiscussionNote.name
      end

      note = Note.new(params)
      note.project = project
      note.author = current_user

      note
    end
  end
end