Add DecimalNumberChamp
This commit is contained in:
parent
681a2e660d
commit
45bfb2fd47
8 changed files with 50 additions and 0 deletions
7
app/models/champs/decimal_number_champ.rb
Normal file
7
app/models/champs/decimal_number_champ.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class Champs::DecimalNumberChamp < Champ
|
||||
validates :value, numericality: { allow_nil: true }
|
||||
|
||||
def value_for_export
|
||||
value.to_f
|
||||
end
|
||||
end
|
|
@ -8,6 +8,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
date: 'date',
|
||||
datetime: 'datetime',
|
||||
number: 'number',
|
||||
decimal_number: 'decimal_number',
|
||||
checkbox: 'checkbox',
|
||||
civilite: 'civilite',
|
||||
email: 'email',
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
class TypesDeChamp::DecimalNumberTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
|
@ -0,0 +1,7 @@
|
|||
class Champs::DecimalNumberChampSerializer < ChampSerializer
|
||||
def value
|
||||
if object.value.present?
|
||||
object.value.to_f
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
= form.number_field :value,
|
||||
step: :any,
|
||||
placeholder: champ.libelle,
|
||||
required: champ.mandatory?
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
25
spec/models/champs/decimal_number_champ_spec.rb
Normal file
25
spec/models/champs/decimal_number_champ_spec.rb
Normal 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
|
Loading…
Reference in a new issue