Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gregoirenovel committed Sep 19, 2018
2 parents a4d689e + cceb885 commit fad67ab
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
8 changes: 3 additions & 5 deletions app/helpers/procedure_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ def procedure_lien(procedure)
end

def procedure_libelle(procedure)
parts = [procedure.libelle]
if procedure.brouillon?
parts << '(brouillon)'
end
parts.join(' ')
parts = procedure.brouillon? ? [content_tag(:span, 'démarche non publiée', class: 'badge')] : []
parts << procedure.libelle
safe_join(parts, ' ')
end

def procedure_modal_text(procedure, key)
Expand Down
31 changes: 22 additions & 9 deletions app/validators/siret_format_validator.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
class SiretFormatValidator < ActiveModel::EachValidator
def validate_each(record,attribute,value)
if !(value =~ /^\d{14}$/)
def validate_each(record, attribute, value)
if !format_is_valid(value)
record.errors.add(attribute, :format)
end
if value.nil? || (luhn_checksum(value) % 10 != 0)

if !luhn_passed(value)
record.errors.add(attribute, :checksum)
end
end

private

LA_POSTE_SIREN = '356000000'

def format_is_valid(value)
value.match?(/^\d{14}$/)
end

def luhn_passed(value)
# Do not enforce Luhn for La Poste SIRET numbers, the only exception to this rule
siret_is_attached_to_la_poste(value) || (luhn_checksum(value) % 10 == 0)
end

def siret_is_attached_to_la_poste(value)
value[0..8] == LA_POSTE_SIREN
end

def luhn_checksum(value)
accum = 0
value.reverse.each_char.map(&:to_i).each_with_index do |digit, index|
value.reverse.each_char.map(&:to_i).map.with_index do |digit, index|
t = index.even? ? digit : digit * 2
t = t - 9 if t >= 10
accum += t
end
accum
t < 10 ? t : t - 9
end.sum
end
end
43 changes: 32 additions & 11 deletions spec/models/siret_spec.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
require 'spec_helper'

describe Siret, type: :model do
let(:valid_siret) { '41816609600051' }
let(:invalid_siret) { '111111111' }
subject { Siret.new(siret: siret) }

context 'with no siret provided' do
it { is_expected.to validate_presence_of(:siret) }
let(:siret) { '' }

it { is_expected.to be_invalid }
end

context 'init with valid siret' do
it { is_expected.to allow_value(valid_siret).for(:siret) }
context 'with a siret that contains letters' do
let(:siret) { 'A1B1C6D9E0F0G1' }

it { is_expected.to be_invalid }
end

context 'init with invalid siret' do
it { is_expected.not_to allow_value(invalid_siret).for(:siret) }
context 'with a siret that is too short' do
let(:siret) { '1234567890' }

it { is_expected.to be_invalid }
end

context 'init with bullshit siret' do
it { is_expected.not_to allow_value('bullshit').for(:siret) }
context 'with a siret that is too long' do
let(:siret) { '12345678901234567890' }

it { is_expected.to be_invalid }
end

context 'init with a siret that is too long' do
it { is_expected.not_to allow_value('9' * 15).for(:siret) }
context 'with a lunh-invalid siret' do
let(:siret) { '41816609600052' }

it { is_expected.to be_invalid }
end

context 'with a lunh-invalid La Poste siret' do
let(:siret) { '35600000018723' }

it { is_expected.to be_valid }
end

context 'with a valid siret' do
let(:siret) { '41816609600051' }

it { is_expected.to be_valid }
end
end

0 comments on commit fad67ab

Please sign in to comment.