forked from openfoodfoundation/openfoodnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unique validator for vouche code
Paranoia doesn't support unique validation including deleted records: rubysherpas/paranoia#333 We use a custom validator, ScopedUniquenessValidator to avoid the issue
- Loading branch information
Showing
6 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: false | ||
|
||
# paranoia doesn't support unique validation including deleted records: | ||
# https://github.com/rubysherpas/paranoia/pull/333 | ||
# We use a custom validator to fix the issue, so we don't need to fork/patch the gem | ||
module Vouchers | ||
class ScopedUniquenessValidator < ActiveModel::Validator | ||
def validate(record) | ||
@record = record | ||
|
||
return unless unique_voucher_code_per_enterprise? | ||
|
||
record.errors.add :code, :taken, value: @record.code | ||
end | ||
|
||
private | ||
|
||
def unique_voucher_code_per_enterprise? | ||
query = Voucher.with_deleted.where(code: @record.code, enterprise_id: @record.enterprise_id) | ||
query = query.where.not(id: @record.id) unless @record.id.nil? | ||
|
||
query.present? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
shared_examples_for 'has a unique code per enterprise' do |voucher_type| | ||
describe "code" do | ||
let(:code) { "super_code" } | ||
let(:enterprise) { create(:enterprise) } | ||
|
||
it "is unique per enterprise" do | ||
voucher = create(voucher_type, code:, enterprise:) | ||
expect(voucher).to be_valid | ||
|
||
expect_voucher_with_same_enterprise_to_be_invalid(voucher_type) | ||
|
||
expect_voucher_with_other_enterprise_to_be_valid(voucher_type) | ||
end | ||
|
||
context "with deleted voucher" do | ||
it "is unique per enterprise" do | ||
create(voucher_type, code:, enterprise:).destroy! | ||
|
||
expect_voucher_with_same_enterprise_to_be_invalid(voucher_type) | ||
|
||
expect_voucher_with_other_enterprise_to_be_valid(voucher_type) | ||
end | ||
end | ||
end | ||
|
||
def expect_voucher_with_same_enterprise_to_be_invalid(voucher_type) | ||
new_voucher = build(voucher_type, code:, enterprise: ) | ||
|
||
expect(new_voucher).not_to be_valid | ||
expect(new_voucher.errors.full_messages).to include("Code has already been taken") | ||
end | ||
|
||
def expect_voucher_with_other_enterprise_to_be_valid(voucher_type) | ||
other_voucher = build(voucher_type, code:, enterprise: create(:enterprise) ) | ||
expect(other_voucher).to be_valid | ||
end | ||
end |