Skip to content

Commit

Permalink
Merge pull request #3674 from hefan/promotion_user_picker
Browse files Browse the repository at this point in the history
Return API Users with distinct result when using Ransack
  • Loading branch information
kennyadsl authored Jul 14, 2020
2 parents 8841b69 + fb65b54 commit 1aa0288
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/app/controllers/spree/api/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# frozen_string_literal: true

class Spree::Api::UsersController < Spree::Api::ResourceController
def index
user_scope = model_class.accessible_by(current_ability, :read)
if params[:ids]
ids = params[:ids].split(",").flatten
@users = user_scope.where(id: ids)
else
@users = user_scope.ransack(params[:q]).result
end

@users = paginate(@users.distinct)
respond_with(@users)
end

private

attr_reader :user
Expand Down
15 changes: 15 additions & 0 deletions api/spec/requests/spree/api/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ module Spree
expect(response.status).to eq(422)
expect(json_response).to eq({ "error" => "Cannot delete record." })
end

it "returns distinct search results" do
distinct_user = create(:user, email: '[email protected]')
distinct_user.addresses << create(:address)
distinct_user.addresses << create(:address)
get spree.api_users_path, params: {
q: {
m: 'or',
email_start: 'distinct_test',
firstname_or_lastname_start: 'distinct_test'
}
}
expect(json_response['count']).to eq(1)
expect(json_response['users'].first['email']).to eq distinct_user.email
end
end
end
end

0 comments on commit 1aa0288

Please sign in to comment.