Merge pull request #1422 from tchak/cleanup

Cleanup some specs and properties delegates
This commit is contained in:
gregoirenovel 2018-02-13 18:45:42 +01:00 committed by GitHub
commit c38eab4c95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 36 additions and 47 deletions

View file

@ -5,7 +5,7 @@ class Champ < ActiveRecord::Base
belongs_to :type_de_champ, inverse_of: :champ
has_many :commentaires
delegate :libelle, :type_champ, :order_place, :mandatory, :description, :drop_down_list, to: :type_de_champ
delegate :libelle, :type_champ, :order_place, :mandatory?, :description, :drop_down_list, to: :type_de_champ
before_save :format_date_to_iso, if: Proc.new { type_champ == 'date' }
before_save :format_datetime, if: Proc.new { type_champ == 'datetime' }
@ -15,10 +15,6 @@ class Champ < ActiveRecord::Base
scope :public_only, -> { where.not(type: 'ChampPrivate').or(where(private: [false, nil])) }
scope :private_only, -> { where(type: 'ChampPrivate').or(where(private: true)) }
def mandatory?
mandatory
end
def public?
!private?
end

View file

@ -52,13 +52,16 @@ class TypeDeChamp < ActiveRecord::Base
type_champs.map { |champ| [I18n.t("activerecord.attributes.type_de_champ.type_champs.#{champ.last}"), champ.first] }
end
def field_for_list?
!(type_champ == 'textarea' || type_champ == 'header_section')
def check_mandatory
if non_fillable?
self.mandatory = false
else
true
end
end
def check_mandatory
self.mandatory = false if %w(header_section explication).include?(self.type_champ)
true
def non_fillable?
type_champ.in?(['header_section', 'explication'])
end
def private?

View file

@ -1,4 +1,4 @@
= form.text_field :value,
'data-address': 'true',
placeholder: champ.libelle,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,6 +1,6 @@
= form.label :value do
#{champ.libelle}
- if champ.mandatory
- if champ.mandatory?
%span.mandatory *
- if champ.updated_at.present?

View file

@ -1,5 +1,5 @@
= form.check_box :value,
{ required: champ.mandatory },
{ required: champ.mandatory? },
'on',
'off'

View file

@ -1,4 +1,4 @@
= form.date_field :value,
value: champ.value,
placeholder: 'JJ/MM/AAAA',
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,3 +1,3 @@
= form.select :value,
Champ.departements,
required: champ.mandatory
required: champ.mandatory?

View file

@ -8,7 +8,7 @@
placeholder: "Numéro de dossier",
autocomplete: 'off',
'data-type': 'dossier-link',
required: champ.mandatory
required: champ.mandatory?
.help-block
%p.text-info{ style: show_text_summary ? nil : 'display: none;' }

View file

@ -2,4 +2,4 @@
= form.select :value,
champ.drop_down_list.options,
disabled: champ.drop_down_list.disabled_options,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,3 +1,3 @@
= form.email_field :value,
placeholder: champ.libelle,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,5 +1,5 @@
= form.check_box :value,
{ required: champ.mandatory },
{ required: champ.mandatory? },
'on',
'off'

View file

@ -1,3 +1,3 @@
= form.number_field :value,
placeholder: champ.libelle,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,3 +1,3 @@
= form.select :value,
Champ.pays,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,3 +1,3 @@
= form.phone_field :value,
placeholder: champ.libelle,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,3 +1,3 @@
= form.select :value,
Champ.regions,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,3 +1,3 @@
= form.text_field :value,
placeholder: champ.libelle,
required: champ.mandatory
required: champ.mandatory?

View file

@ -1,5 +1,5 @@
~ form.text_area :value,
row: 6,
placeholder: champ.description,
required: champ.mandatory,
required: champ.mandatory?,
value: html_to_string(champ.value)

View file

@ -47,16 +47,16 @@ describe DropDownList do
end
context 'when multiple' do
let(:type_de_champ) { TypeDeChamp.new(type_champ: 'multiple_drop_down_list') }
let(:type_de_champ) { build(:type_de_champ_public, type_champ: 'multiple_drop_down_list') }
let(:champ) { type_de_champ.champ.build(value: '["1","2"]').decorate }
let(:champ) { Champ.new(value: '["1","2"]').decorate }
it { expect(dropdownlist.selected_options(champ)).to match(['1', '2']) }
end
context 'when simple' do
let(:type_de_champ) { TypeDeChamp.new(type_champ: 'drop_down_list') }
let(:type_de_champ) { build(:type_de_champ_public, type_champ: 'drop_down_list') }
let(:champ) { type_de_champ.champ.build(value: '1').decorate }
let(:champ) { Champ.new(value: '1').decorate }
it { expect(dropdownlist.selected_options(champ)).to match(['1']) }
end
end

View file

@ -29,14 +29,4 @@ shared_examples 'type_de_champ_spec' do
it { is_expected.to allow_value('blabla').for(:description) }
end
end
describe 'field_for_list?' do
let(:type_de_champ_yes) { create :type_de_champ_public, type_champ: 'text' }
let(:type_de_champ_no_1) { create :type_de_champ_public, type_champ: 'textarea' }
let(:type_de_champ_no_2) { create :type_de_champ_public, type_champ: 'header_section' }
it { expect(type_de_champ_yes.field_for_list?).to be_truthy }
it { expect(type_de_champ_no_1.field_for_list?).to be_falsey }
it { expect(type_de_champ_no_2.field_for_list?).to be_falsey }
end
end

View file

@ -1,12 +1,12 @@
require 'spec_helper'
describe ChampsService do
let!(:champ) { Champ.create(value: 'toto', type_de_champ: TypeDeChamp.new) }
let!(:champ_mandatory_empty) { Champ.create(type_de_champ: TypeDeChamp.new(libelle: 'mandatory', mandatory: true)) }
let!(:champ_datetime) do
champ_datetime = TypeDeChamp.new(type_champ: 'datetime')
Champ.create(type_de_champ: champ_datetime)
end
let(:type_de_champ) { create(:type_de_champ_public) }
let(:type_de_champ_mandatory) { create(:type_de_champ_public, libelle: 'mandatory', mandatory: true) }
let(:type_de_champ_datetime) { create(:type_de_champ_public, type_champ: :datetime) }
let!(:champ) { type_de_champ.champ.create(value: 'toto') }
let!(:champ_mandatory_empty) { type_de_champ_mandatory.champ.create }
let!(:champ_datetime) { type_de_champ_datetime.champ.create }
let!(:champs) { [champ, champ_mandatory_empty, champ_datetime] }
describe 'save_champs' do

View file

@ -13,7 +13,7 @@ describe 'new_gestionnaire/dossiers/champs.html.haml', type: :view do
context "there are some champs" do
let(:dossier) { create(:dossier) }
let(:avis) { create :avis, dossier: dossier, gestionnaire: gestionnaire }
let(:champ1) { create(:champ, :checkbox, value: "true") }
let(:champ1) { create(:champ, :checkbox, value: "on") }
let(:champ2) { create(:champ, :header_section, value: "Section") }
let(:champ3) { create(:champ, :explication, value: "mazette") }
let(:champ4) { create(:champ, :dossier_link, value: dossier.id) }
@ -53,7 +53,7 @@ describe 'new_gestionnaire/dossiers/champs.html.haml', type: :view do
context "with seen_at" do
let(:dossier) { create(:dossier) }
let(:champ1) { create(:champ, :checkbox, value: "true") }
let(:champ1) { create(:champ, :checkbox, value: "on") }
let(:champs) { [champ1] }
context "with a demande_seen_at after champ updated_at" do