2016-03-21 09:12:52 -04:00
module IssuableActions
extend ActiveSupport :: Concern
included do
2016-09-26 17:06:46 -04:00
before_action :labels , only : [ :show , :new , :edit ]
2016-03-21 09:12:52 -04:00
before_action :authorize_destroy_issuable! , only : :destroy
2016-09-06 08:49:49 -04:00
before_action :authorize_admin_issuable! , only : :bulk_update
2016-03-21 09:12:52 -04:00
end
2018-05-31 10:37:00 -04:00
def permitted_keys
[
:issuable_ids ,
:assignee_id ,
:milestone_id ,
:state_event ,
:subscription_event ,
label_ids : [ ] ,
add_label_ids : [ ] ,
remove_label_ids : [ ]
]
end
2017-11-01 13:35:14 -04:00
def show
respond_to do | format |
2017-11-07 08:34:12 -05:00
format . html
2017-11-01 13:35:14 -04:00
format . json do
render json : serializer . represent ( issuable , serializer : params [ :serializer ] )
end
end
end
def update
2017-11-22 02:50:36 -05:00
@issuable = update_service . execute ( issuable ) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-11-01 13:35:14 -04:00
respond_to do | format |
format . html do
2017-12-08 13:19:51 -05:00
recaptcha_check_if_spammable { render :edit }
2017-11-01 13:35:14 -04:00
end
format . json do
2017-12-08 13:19:51 -05:00
recaptcha_check_if_spammable ( false ) { render_entity_json }
2017-11-01 13:35:14 -04:00
end
end
rescue ActiveRecord :: StaleObjectError
render_conflict_response
end
def realtime_changes
Gitlab :: PollingInterval . set_header ( response , interval : 3_000 )
response = {
title : view_context . markdown_field ( issuable , :title ) ,
title_text : issuable . title ,
description : view_context . markdown_field ( issuable , :description ) ,
description_text : issuable . description ,
task_status : issuable . task_status
}
if issuable . edited?
2018-01-11 11:22:00 -05:00
response [ :updated_at ] = issuable . last_edited_at . to_time . iso8601
2017-11-01 13:35:14 -04:00
response [ :updated_by_name ] = issuable . last_edited_by . name
response [ :updated_by_path ] = user_path ( issuable . last_edited_by )
end
render json : response
end
2016-03-21 09:12:52 -04:00
def destroy
2017-11-29 05:27:36 -05:00
Issuable :: DestroyService . new ( issuable . project , current_user ) . execute ( issuable )
2016-03-21 09:12:52 -04:00
2016-11-18 21:19:04 -05:00
name = issuable . human_class_name
2016-03-21 09:12:52 -04:00
flash [ :notice ] = " The #{ name } was successfully deleted. "
2017-11-02 10:51:42 -04:00
index_path = polymorphic_path ( [ parent , issuable . class ] )
2017-05-12 06:23:30 -04:00
respond_to do | format |
format . html { redirect_to index_path }
format . json do
render json : {
2017-05-25 06:58:40 -04:00
web_url : index_path
2017-05-12 06:23:30 -04:00
}
end
end
2016-03-21 09:12:52 -04:00
end
2016-09-06 08:49:49 -04:00
def bulk_update
result = Issuable :: BulkUpdateService . new ( project , current_user , bulk_update_params ) . execute ( resource_name )
quantity = result [ :count ]
render json : { notice : " #{ quantity } #{ resource_name . pluralize ( quantity ) } updated " }
end
2018-02-27 19:10:43 -05:00
def discussions
2018-06-21 08:22:40 -04:00
notes = issuable . discussion_notes
2018-02-27 19:10:43 -05:00
. inc_relations_for_view
. includes ( :noteable )
. fresh
notes = prepare_notes_for_rendering ( notes )
notes = notes . reject { | n | n . cross_reference_not_visible_for? ( current_user ) }
discussions = Discussion . build_collection ( notes , issuable )
2018-04-03 12:03:00 -04:00
render json : discussion_serializer . represent ( discussions , context : self )
2018-02-27 19:10:43 -05:00
end
2016-03-21 09:12:52 -04:00
private
2018-04-03 12:03:00 -04:00
def discussion_serializer
DiscussionSerializer . new ( project : project , noteable : issuable , current_user : current_user , note_entity : ProjectNoteEntity )
end
2017-12-08 13:19:51 -05:00
def recaptcha_check_if_spammable ( should_redirect = true , & block )
2017-12-15 06:37:57 -05:00
return yield unless issuable . is_a? Spammable
2017-12-08 13:19:51 -05:00
recaptcha_check_with_fallback ( should_redirect , & block )
end
2017-01-16 13:43:03 -05:00
def render_conflict_response
respond_to do | format |
format . html do
2017-11-22 02:50:36 -05:00
@conflict = true # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-01-16 13:43:03 -05:00
render :edit
end
format . json do
render json : {
errors : [
" 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. "
]
2018-07-02 06:43:06 -04:00
} , status : :conflict
2017-01-16 13:43:03 -05:00
end
end
end
2016-09-20 10:03:41 -04:00
def labels
2017-11-22 04:15:46 -05:00
@labels || = LabelsFinder . new ( current_user , project_id : @project . id ) . execute # rubocop:disable Gitlab/ModuleWithInstanceVariables
2016-09-20 10:03:41 -04:00
end
2016-03-21 09:12:52 -04:00
def authorize_destroy_issuable!
2016-09-08 08:33:53 -04:00
unless can? ( current_user , :" destroy_ #{ issuable . to_ability_name } " , issuable )
2016-03-21 09:12:52 -04:00
return access_denied!
end
end
2016-09-06 08:49:49 -04:00
def authorize_admin_issuable!
2017-11-22 02:50:36 -05:00
unless can? ( current_user , :" admin_ #{ resource_name } " , @project ) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2016-09-06 08:49:49 -04:00
return access_denied!
end
end
2017-11-01 13:35:14 -04:00
def authorize_update_issuable!
render_404 unless can? ( current_user , :" update_ #{ resource_name } " , issuable )
end
2016-09-06 08:49:49 -04:00
def bulk_update_params
2018-05-31 10:37:00 -04:00
permitted_keys_array = permitted_keys . dup
2017-05-08 10:58:42 -04:00
if resource_name == 'issue'
2018-05-31 10:37:00 -04:00
permitted_keys_array << { assignee_ids : [ ] }
2017-05-08 10:58:42 -04:00
else
2018-05-31 10:37:00 -04:00
permitted_keys_array . unshift ( :assignee_id )
2017-05-08 10:58:42 -04:00
end
2018-05-31 10:37:00 -04:00
params . require ( :update ) . permit ( permitted_keys_array )
2016-09-06 08:49:49 -04:00
end
def resource_name
@resource_name || = controller_name . singularize
end
2017-11-01 13:35:14 -04:00
2017-11-22 02:50:36 -05:00
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-11-01 13:35:14 -04:00
def render_entity_json
if @issuable . valid?
render json : serializer . represent ( @issuable )
else
render json : { errors : @issuable . errors . full_messages } , status : :unprocessable_entity
end
end
2017-11-22 02:50:36 -05:00
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-11-01 13:35:14 -04:00
def serializer
raise NotImplementedError
end
def update_service
raise NotImplementedError
end
2017-11-02 10:51:42 -04:00
def parent
2017-11-22 02:50:36 -05:00
@project || @group # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-11-02 10:51:42 -04:00
end
2016-03-21 09:12:52 -04:00
end