/app/controllers/concerns/issuable_actions.rb

https://gitlab.com/mehlah/gitlab-ce · Ruby · 167 lines · 137 code · 30 blank · 0 comment · 4 complexity · 5d199c5837caf30d5ffc7e0494c88451 MD5 · raw file

  1. module IssuableActions
  2. extend ActiveSupport::Concern
  3. included do
  4. before_action :labels, only: [:show, :new, :edit]
  5. before_action :authorize_destroy_issuable!, only: :destroy
  6. before_action :authorize_admin_issuable!, only: :bulk_update
  7. end
  8. def show
  9. respond_to do |format|
  10. format.html do
  11. render show_view
  12. end
  13. format.json do
  14. render json: serializer.represent(issuable, serializer: params[:serializer])
  15. end
  16. end
  17. end
  18. def update
  19. @issuable = update_service.execute(issuable)
  20. respond_to do |format|
  21. format.html do
  22. recaptcha_check_with_fallback { render :edit }
  23. end
  24. format.json do
  25. render_entity_json
  26. end
  27. end
  28. rescue ActiveRecord::StaleObjectError
  29. render_conflict_response
  30. end
  31. def realtime_changes
  32. Gitlab::PollingInterval.set_header(response, interval: 3_000)
  33. response = {
  34. title: view_context.markdown_field(issuable, :title),
  35. title_text: issuable.title,
  36. description: view_context.markdown_field(issuable, :description),
  37. description_text: issuable.description,
  38. task_status: issuable.task_status
  39. }
  40. if issuable.edited?
  41. response[:updated_at] = issuable.updated_at
  42. response[:updated_by_name] = issuable.last_edited_by.name
  43. response[:updated_by_path] = user_path(issuable.last_edited_by)
  44. end
  45. render json: response
  46. end
  47. def destroy
  48. issuable.destroy
  49. destroy_method = "destroy_#{issuable.class.name.underscore}".to_sym
  50. TodoService.new.public_send(destroy_method, issuable, current_user) # rubocop:disable GitlabSecurity/PublicSend
  51. name = issuable.human_class_name
  52. flash[:notice] = "The #{name} was successfully deleted."
  53. index_path = polymorphic_path([@project.namespace.becomes(Namespace), @project, issuable.class])
  54. respond_to do |format|
  55. format.html { redirect_to index_path }
  56. format.json do
  57. render json: {
  58. web_url: index_path
  59. }
  60. end
  61. end
  62. end
  63. def bulk_update
  64. result = Issuable::BulkUpdateService.new(project, current_user, bulk_update_params).execute(resource_name)
  65. quantity = result[:count]
  66. render json: { notice: "#{quantity} #{resource_name.pluralize(quantity)} updated" }
  67. end
  68. private
  69. def render_conflict_response
  70. respond_to do |format|
  71. format.html do
  72. @conflict = true
  73. render :edit
  74. end
  75. format.json do
  76. render json: {
  77. errors: [
  78. "Someone edited this #{issuable.human_class_name} at the same time you did. Please refresh your browser and make sure your changes will not unintentionally remove theirs."
  79. ]
  80. }, status: 409
  81. end
  82. end
  83. end
  84. def labels
  85. @labels ||= LabelsFinder.new(current_user, project_id: @project.id).execute
  86. end
  87. def authorize_destroy_issuable!
  88. unless can?(current_user, :"destroy_#{issuable.to_ability_name}", issuable)
  89. return access_denied!
  90. end
  91. end
  92. def authorize_admin_issuable!
  93. unless can?(current_user, :"admin_#{resource_name}", @project)
  94. return access_denied!
  95. end
  96. end
  97. def authorize_update_issuable!
  98. render_404 unless can?(current_user, :"update_#{resource_name}", issuable)
  99. end
  100. def bulk_update_params
  101. permitted_keys = [
  102. :issuable_ids,
  103. :assignee_id,
  104. :milestone_id,
  105. :state_event,
  106. :subscription_event,
  107. label_ids: [],
  108. add_label_ids: [],
  109. remove_label_ids: []
  110. ]
  111. if resource_name == 'issue'
  112. permitted_keys << { assignee_ids: [] }
  113. else
  114. permitted_keys.unshift(:assignee_id)
  115. end
  116. params.require(:update).permit(permitted_keys)
  117. end
  118. def resource_name
  119. @resource_name ||= controller_name.singularize
  120. end
  121. def render_entity_json
  122. if @issuable.valid?
  123. render json: serializer.represent(@issuable)
  124. else
  125. render json: { errors: @issuable.errors.full_messages }, status: :unprocessable_entity
  126. end
  127. end
  128. def show_view
  129. 'show'
  130. end
  131. def serializer
  132. raise NotImplementedError
  133. end
  134. def update_service
  135. raise NotImplementedError
  136. end
  137. end