Skip to content

Commit

Permalink
Allow to specify tags on validations (currently, only SEPA tag is ava…
Browse files Browse the repository at this point in the history
…ilable)
  • Loading branch information
leio10 committed Oct 4, 2017
1 parent 1d1cc02 commit 0cc8864
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/active_model/validations/iban_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def validate_each(record, attribute, value)
elsif !IbanBic.valid_check?(value)
record.errors.add(attribute, :invalid_check)
elsif !IbanBic.valid_country_check?(value)

record.errors.add(attribute, :invalid_country_check)
elsif options[:tags] && !IbanBic.has_tags?(value, options[:tags])
record.errors.add(attribute, :invalid_tag)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/iban_bic/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def parse(iban)
parts ? ActiveSupport::HashWithIndifferentAccess[parts.names.zip(parts.captures)] : nil
end

def has_tags?(iban, searched_tags)
(tags[iban[0..1]] & searched_tags).any?
end

def calculate_check(iban)
98 - "#{iban[4..-1]}#{iban[0..3]}".each_char.map do |char|
case char
Expand Down Expand Up @@ -56,6 +60,14 @@ def parser
].freeze
end

def tags
@tags ||= Hash[
::YAML.load_file(configuration.iban_meta_path).map do |country, meta|
[country, meta["tags"]&.split&.map(&:to_sym) || []]
end
].freeze
end

def bics
configuration.static_bics? ? static_bics : dynamic_bics
end
Expand Down
4 changes: 4 additions & 0 deletions lib/iban_bic/models/bic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

class Bic < ActiveRecord::Base
self.table_name = IbanBic.configuration.bics_table_name

after_commit do
IbanBic.clear_cache
end
end
15 changes: 14 additions & 1 deletion spec/validators/iban_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
subject(:validatable_model) { validatable.new(iban: iban) }

let(:validatable) do
options = validator_options
Class.new do
def self.model_name
ActiveModel::Name.new(self, nil, "Validatable")
Expand All @@ -17,12 +18,13 @@ def self.model_name

attribute :iban

validates :iban, iban: true
validates :iban, iban: options
end
end
let(:iban) { "ES#{iban_digits}00030000#{country_digits}0000000000" }
let(:iban_digits) { "87" }
let(:country_digits) { "30" }
let(:validator_options) { true }

it { is_expected.to be_valid }

Expand All @@ -46,4 +48,15 @@ def self.model_name
let(:iban) { "3432" }
it { is_expected.to be_invalid }
end

describe "when tags option is present" do
let(:validator_options) { { tags: tags } }
let(:tags) { [:sepa] }
it { is_expected.to be_valid }

context "when tag is not present for the country" do
let(:tags) { [:other] }
it { is_expected.to be_invalid }
end
end
end

0 comments on commit 0cc8864

Please sign in to comment.