Add DecimalNumberChamp

This commit is contained in:
Paul Chavard 2018-10-31 19:48:03 +01:00
parent 681a2e660d
commit 45bfb2fd47
8 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,7 @@
class Champs::DecimalNumberChamp < Champ
validates :value, numericality: { allow_nil: true }
def value_for_export
value.to_f
end
end

View file

@ -8,6 +8,7 @@ class TypeDeChamp < ApplicationRecord
date: 'date',
datetime: 'datetime',
number: 'number',
decimal_number: 'decimal_number',
checkbox: 'checkbox',
civilite: 'civilite',
email: 'email',

View file

@ -0,0 +1,2 @@
class TypesDeChamp::DecimalNumberTypeDeChamp < TypesDeChamp::TypeDeChampBase
end

View file

@ -0,0 +1,7 @@
class Champs::DecimalNumberChampSerializer < ChampSerializer
def value
if object.value.present?
object.value.to_f
end
end
end

View file

@ -0,0 +1,4 @@
= form.number_field :value,
step: :any,
placeholder: champ.libelle,
required: champ.mandatory?

View file

@ -10,6 +10,7 @@ fr:
date: 'Date'
datetime: 'Date et Heure'
number: 'Nombre entier'
decimal_number: 'Nombre décimal'
checkbox: 'Case à cocher'
civilite: 'Civilité'
email: 'Email'

View file

@ -18,6 +18,9 @@ FactoryBot.define do
factory :type_de_champ_number do
type_champ { TypeDeChamp.type_champs.fetch(:number) }
end
factory :type_de_champ_decimal_number do
type_champ { TypeDeChamp.type_champs.fetch(:decimal_number) }
end
factory :type_de_champ_checkbox do
type_champ { TypeDeChamp.type_champs.fetch(:checkbox) }
end

View file

@ -0,0 +1,25 @@
require 'spec_helper'
describe Champs::DecimalNumberChamp do
subject { Champs::DecimalNumberChamp.new(value: value) }
describe '#valid?' do
context 'when the value is integer number' do
let(:value) { 2 }
it { is_expected.to be_valid }
end
context 'when the value is decimal number' do
let(:value) { 2.6 }
it { is_expected.to be_valid }
end
context 'when the value is not a number' do
let(:value) { 'toto' }
it { is_expected.to_not be_valid }
end
end
end