- valid siren on create file

This commit is contained in:
Xavier J 2015-12-03 15:02:22 +01:00
parent 6e19d3ba67
commit 2b6aba16ac
5 changed files with 67 additions and 3 deletions

View file

@ -1,5 +1,6 @@
class Users::DossiersController < UsersController
before_action :authenticate_user!
before_action :check_siret, only: :create
def index
order = 'DESC'
@ -74,8 +75,7 @@ class Users::DossiersController < UsersController
redirect_to url_for(controller: :dossiers, action: :show, id: @dossier.id)
rescue RestClient::ResourceNotFound
flash.alert = t('errors.messages.invalid_siret')
redirect_to url_for new_users_dossiers_path(procedure_id: create_params[:procedure_id])
errors_valid_siret
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
@ -115,6 +115,15 @@ class Users::DossiersController < UsersController
private
def check_siret
errors_valid_siret unless Siret.new(siret: siret).valid?
end
def errors_valid_siret
flash.alert = t('errors.messages.invalid_siret')
redirect_to url_for new_users_dossiers_path(procedure_id: create_params[:procedure_id])
end
def update_params
params.require(:dossier).permit(:autorisation_donnees)
end

7
app/models/siret.rb Normal file
View file

@ -0,0 +1,7 @@
class Siret
include ActiveModel::Model
attr_accessor :siret
validates_presence_of :siret
validates :siret, siret_format: true
end

View file

@ -0,0 +1,21 @@
class SiretFormatValidator < ActiveModel::EachValidator
def validate_each(record,attribute,value)
unless value =~ /^\d{14}$/
record.errors.add(attribute, :format)
end
unless value!= nil && (luhn_checksum(value) % 10 == 0)
record.errors.add(attribute, :checksum)
end
end
private
def luhn_checksum(value)
accum = 0
value.reverse.each_char.map(&:to_i).each_with_index do |digit, index|
t = index.even? ? digit : digit * 2
t = t - 9 if t >= 10
accum += t
end
accum
end
end

View file

@ -21,7 +21,7 @@ module TPS
config.i18n.default_locale = :fr
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/validators)
config.assets.paths << Rails.root.join('app', 'assets', 'javascript')
config.assets.paths << Rails.root.join('app', 'assets', 'javascript', 'api_carto')
config.assets.paths << Rails.root.join('app', 'assets', 'javascript', 'api_carto', 'lib')

27
spec/models/siret_spec.rb Normal file
View file

@ -0,0 +1,27 @@
require 'spec_helper'
describe Siret, type: :model do
let(:valid_siret) { '41816609600051' }
let(:invalid_siret) { '111111111' }
context 'with no siret provided' do
it { is_expected.to validate_presence_of(:siret) }
end
context 'init with valid siret' do
it { is_expected.to allow_value(valid_siret).for(:siret) }
end
context 'init with invalid siret' do
it { is_expected.not_to allow_value(invalid_siret).for(:siret) }
end
context 'init with bullshit siret' do
it { is_expected.not_to allow_value('bullshit').for(:siret) }
end
context 'init with a siret that is too long' do
it { is_expected.not_to allow_value('9' * 15).for(:siret) }
end
end