mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Allow removing customer codes
This commit is contained in:
@@ -14,11 +14,13 @@ angular.module("admin.customers").controller "customersCtrl", ($scope, $q, Custo
|
||||
$scope.customers = data
|
||||
|
||||
$scope.checkForDuplicateCodes = ->
|
||||
customers = $scope.findByCode(this.customer.code)
|
||||
if (customers.length > 1)
|
||||
this.duplicate = true
|
||||
else
|
||||
this.duplicate = false
|
||||
delete this.customer.code unless this.customer.code
|
||||
this.duplicate = $scope.isDuplicateCode(this.customer.code)
|
||||
|
||||
$scope.isDuplicateCode = (code) ->
|
||||
return false unless code
|
||||
customers = $scope.findByCode(code)
|
||||
customers.length > 1
|
||||
|
||||
$scope.findByCode = (code) ->
|
||||
if $scope.customers
|
||||
|
||||
@@ -5,8 +5,9 @@ class Customer < ActiveRecord::Base
|
||||
belongs_to :user, class_name: Spree.user_class
|
||||
|
||||
before_validation :downcase_email
|
||||
before_validation :empty_code
|
||||
|
||||
validates :code, uniqueness: { scope: :enterprise_id, allow_blank: true, allow_nil: true }
|
||||
validates :code, uniqueness: { scope: :enterprise_id, allow_nil: true }
|
||||
validates :email, presence: true, uniqueness: { scope: :enterprise_id, message: I18n.t('validation_msg_is_associated_with_an_exising_customer') }
|
||||
validates :enterprise_id, presence: true
|
||||
|
||||
@@ -20,6 +21,10 @@ class Customer < ActiveRecord::Base
|
||||
email.andand.downcase!
|
||||
end
|
||||
|
||||
def empty_code
|
||||
self.code = nil if code.blank?
|
||||
end
|
||||
|
||||
def associate_user
|
||||
self.user = user || Spree::User.find_by_email(email)
|
||||
end
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Customer, type: :model do
|
||||
describe 'an existing customer' do
|
||||
let(:customer) { create(:customer) }
|
||||
|
||||
it "saves its code" do
|
||||
code = "code one"
|
||||
customer.code = code
|
||||
customer.save
|
||||
expect(customer.code).to eq code
|
||||
end
|
||||
|
||||
it "can remove its code" do
|
||||
customer.code = ""
|
||||
customer.save
|
||||
expect(customer.code).to be nil
|
||||
end
|
||||
end
|
||||
|
||||
describe 'creation callbacks' do
|
||||
let!(:user1) { create(:user) }
|
||||
let!(:user2) { create(:user) }
|
||||
|
||||
Reference in New Issue
Block a user