From 3720120124b08f760b6d25d4464b9f1693dbe73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A9?= Date: Fri, 17 Sep 2021 16:16:24 +0200 Subject: [PATCH] EAN to string + integer validation --- app/models/p_product_ref.rb | 8 +++++++- db/migrate/20210917122153_add_ean_to_p_product_refs.rb | 2 +- db/schema.rb | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/models/p_product_ref.rb b/app/models/p_product_ref.rb index d689bac..9ff80d1 100644 --- a/app/models/p_product_ref.rb +++ b/app/models/p_product_ref.rb @@ -18,7 +18,7 @@ class PProductRef < ApplicationRecord accepts_nested_attributes_for :p_product_ref_specs, allow_destroy: true #validates :ct_price_ht, :presence => true - + validate :ean, :is_integer has_many :p_customer_product_prices, :dependent => :destroy accepts_nested_attributes_for :p_customer_product_prices, allow_destroy: true @@ -167,4 +167,10 @@ class PProductRef < ApplicationRecord end end + def is_integer + if ean.present? && ean.match(/\A[+-]?\d+\z/).nil? + errors.add(:ean, "ne doit ĂȘtre composĂ© que de chiffres") + end + end + end diff --git a/db/migrate/20210917122153_add_ean_to_p_product_refs.rb b/db/migrate/20210917122153_add_ean_to_p_product_refs.rb index 43bf5de..128e438 100644 --- a/db/migrate/20210917122153_add_ean_to_p_product_refs.rb +++ b/db/migrate/20210917122153_add_ean_to_p_product_refs.rb @@ -1,5 +1,5 @@ class AddEanToPProductRefs < ActiveRecord::Migration[6.0] def change - add_column :p_product_refs, :ean, :integer + add_column :p_product_refs, :ean, :string end end diff --git a/db/schema.rb b/db/schema.rb index 490c94d..bebdd70 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1799,7 +1799,7 @@ ActiveRecord::Schema.define(version: 2021_09_17_124338) do t.decimal "ca_dee", precision: 10 t.decimal "cc_dee", precision: 10 t.decimal "ct_dee", precision: 10 - t.integer "ean" + t.string "ean" t.string "sorecop_comment" end