mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Controller method to allow enterprise users to search for other users which share management of their enterprises
This commit is contained in:
19
app/controllers/spree/admin/search_controller_decorator.rb
Normal file
19
app/controllers/spree/admin/search_controller_decorator.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
Spree::Admin::SearchController.class_eval do
|
||||
def known_users
|
||||
binding.pry
|
||||
if exact_match = Spree.user_class.find_by_email(params[:q])
|
||||
@users = [exact_match]
|
||||
else
|
||||
@users = spree_current_user.known_users.ransack({
|
||||
:m => 'or',
|
||||
:email_start => params[:q],
|
||||
:ship_address_firstname_start => params[:q],
|
||||
:ship_address_lastname_start => params[:q],
|
||||
:bill_address_firstname_start => params[:q],
|
||||
:bill_address_lastname_start => params[:q]
|
||||
}).result.limit(10)
|
||||
end
|
||||
render :users
|
||||
|
||||
end
|
||||
end
|
||||
35
spec/controllers/spree/admin/search_controller_spec.rb
Normal file
35
spec/controllers/spree/admin/search_controller_spec.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Admin::SearchController do
|
||||
include AuthenticationWorkflow
|
||||
context "Distributor Enterprise User" do
|
||||
let!(:owner) { create_enterprise_user( email: "test1@email.com" ) }
|
||||
let!(:manager) { create_enterprise_user( email: "test2@email.com" ) }
|
||||
let!(:random) { create_enterprise_user( email: "test3@email.com" ) }
|
||||
let!(:enterprise) { create(:enterprise, owner: owner, users: [owner, manager]) }
|
||||
before { login_as_enterprise_user [enterprise] }
|
||||
|
||||
describe 'searching for known users' do
|
||||
describe "when search query is not an exact match" do
|
||||
before do
|
||||
spree_get :known_users, q: "test"
|
||||
end
|
||||
|
||||
it "returns a list of users that I share management of enteprises with" do
|
||||
expect(assigns(:users)).to include owner, manager
|
||||
expect(assigns(:users)).to_not include random
|
||||
end
|
||||
end
|
||||
|
||||
describe "when search query exactly matches the email of a user in the system" do
|
||||
before do
|
||||
spree_get :known_users, q: "test3@email.com"
|
||||
end
|
||||
|
||||
it "returns that user, regardless of the relationship between the two users" do
|
||||
expect(assigns(:users)).to eq [random]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user