Skip to content

Commit

Permalink
Merge pull request #9478 from vpereira/autocomplete_excluded_requests
Browse files Browse the repository at this point in the history
Add autocomplete functionality to Staging::ExcludedRequests controller
  • Loading branch information
vpereira committed May 4, 2020
2 parents c6b95b1 + beb2222 commit fa07203
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Webui
module Staging
class ExcludedRequestsController < WebuiController
before_action :require_login, except: [:index]
before_action :require_login, except: [:index, :autocomplete]
before_action :set_workflow_project
before_action :set_staging_workflow
before_action :set_request_exclusion, only: [:destroy]
after_action :verify_authorized, except: [:index]
after_action :verify_authorized, except: [:index, :autocomplete]

def index
respond_to do |format|
Expand All @@ -25,12 +25,12 @@ def create

request = @staging_workflow.target_of_bs_requests.find_by(number: staging_request_exclusion[:number])
unless request
redirect_back(fallback_location: root_path, error: "Request #{params[:number]} doesn't exist or it doesn't belong to this project")
redirect_back(fallback_location: root_path, error: "Request #{staging_request_exclusion[:number]} doesn't exist or it doesn't belong to this project")
return
end
if request.staging_project
redirect_back(fallback_location: root_path,
error: "Request #{params[:number]} could not be excluded because is staged in: #{request.staging_project}")
error: "Request #{staging_request_exclusion[:number]} could not be excluded because is staged in: #{request.staging_project}")
return
end

Expand All @@ -55,6 +55,11 @@ def destroy
redirect_to excluded_requests_path(@staging_workflow.project)
end

def autocomplete
requests = @staging_workflow.autocomplete(params[:term]).pluck(:number).collect(&:to_s) if params[:term]
render json: requests || []
end

private

def set_workflow_project
Expand Down
4 changes: 4 additions & 0 deletions src/api/app/models/staging/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def self.load_users(staging_projects)
users_hash
end

def autocomplete(num)
unassigned_requests.where('CAST(bs_requests.number AS CHAR) LIKE ?', "%#{num}%")
end

private

def create_staging_projects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
%h5.modal-title#exclude-request-modal-label Exclude Request
= form_for Staging::RequestExclusion.new, url: excluded_requests_path do |f|
.modal-body
.form-group
= f.label :number, 'Request'
= f.number_field :number, required: true, class: 'form-control'
= render partial: 'webui/shared/autocomplete', locals: { html_id: 'staging_request_exclusion[number]',
label: 'Request', value: f.object.number,
data: { source: autocomplete_excluded_requests_path(staging_workflow_project) } }
.form-group
= f.label :description
= f.text_area :description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Exclude request

- if request_exclusion_policy
= render partial: 'create_dialog'
= render partial: 'create_dialog', locals: { staging_workflow_project: @staging_workflow.project }
= render partial: 'delete_dialog'

- content_for :ready_function do
Expand Down
6 changes: 5 additions & 1 deletion src/api/config/routes/webui_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@
get :preview_copy, on: :member
post :copy, on: :member
end
resources :excluded_requests, controller: 'webui/staging/excluded_requests'
resources :excluded_requests, controller: 'webui/staging/excluded_requests' do
collection do
get :autocomplete
end
end
end
end
end
11 changes: 11 additions & 0 deletions src/api/spec/models/staging/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,15 @@
it { expect(subject).to contain_exactly(bs_request_2) }
end
end

describe '#autocomplete' do
let!(:bs_request_2) do
create(:bs_request_with_submit_action,
target_package: target_package,
source_package: source_package)
end
it { expect(staging_workflow.autocomplete(bs_request_2.number)).to include(bs_request_2) }
it { expect(staging_workflow.autocomplete(bs_request.number)).not_to include(bs_request_2) }
it { expect(staging_workflow.autocomplete(-1)).to be_empty }
end
end

0 comments on commit fa07203

Please sign in to comment.