diff options
author | Douwe Maan <douwe@selenight.nl> | 2016-08-12 20:17:18 -0500 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2016-08-12 20:17:18 -0500 |
commit | 5a07b760dff04660d9c7da84852c710b1fc2f786 (patch) | |
tree | 6aa67c32c6b80dcc1c7cbfe4f9f62e57edb76b3e /app/services/projects/autocomplete_service.rb | |
parent | 5d4993d62357e438b6211247278025040f3ae382 (diff) | |
download | gitlab-ce-5a07b760dff04660d9c7da84852c710b1fc2f786.tar.gz |
Refactor slash command definition
Diffstat (limited to 'app/services/projects/autocomplete_service.rb')
-rw-r--r-- | app/services/projects/autocomplete_service.rb | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/app/services/projects/autocomplete_service.rb b/app/services/projects/autocomplete_service.rb index 477c999eff4..cb85ee6694d 100644 --- a/app/services/projects/autocomplete_service.rb +++ b/app/services/projects/autocomplete_service.rb @@ -1,7 +1,7 @@ module Projects class AutocompleteService < BaseService def issues - @project.issues.visible_to_user(current_user).opened.select([:iid, :title]) + IssuesFinder.new(current_user, project_id: project.id, state: 'opened').execute.select([:iid, :title]) end def milestones @@ -9,42 +9,34 @@ module Projects end def merge_requests - @project.merge_requests.opened.select([:iid, :title]) + MergeRequestsFinder.new(current_user, project_id: project.id, state: 'opened').execute.select([:iid, :title]) end def labels @project.labels.select([:title, :color]) end - def commands - # We don't return commands when editing an issue or merge request - # This should be improved by not enabling autocomplete at the JS-level - # following this suggestion: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5021#note_13837384 - return [] if !target || %w[edit update].include?(params[:action_name]) + def commands(noteable, type) + noteable ||= + case type + when 'Issue' + @project.issues.build + when 'MergeRequest' + @project.merge_requests.build + end - SlashCommands::InterpretService.command_definitions( + return [] unless noteable && noteable.is_a?(Issuable) + + opts = { project: project, - noteable: target, + noteable: noteable, current_user: current_user - ) - end + } + SlashCommands::InterpretService.command_definitions.map do |definition| + next unless definition.available?(opts) - private - - def target - @target ||= begin - noteable_id = params[:type_id] - case params[:type] - when 'Issue' - IssuesFinder.new(current_user, project_id: project.id, state: 'all'). - execute.find_or_initialize_by(iid: noteable_id) - when 'MergeRequest' - MergeRequestsFinder.new(current_user, project_id: project.id, state: 'all'). - execute.find_or_initialize_by(iid: noteable_id) - else - nil - end - end + definition.to_h(opts) + end.compact end end end |